1

At the moment I'm using strstr() for getting the first part of a string. I have my project running on 3 servers. 1. development server 2. test server 3. live server

the strstr() works fine on the development server and test server, but the moment I place my project live, the strstr prints nothing. for example:

I use this code:

//$product["titel2"] = "apple - &#60fruit&#60";
 $product["titel2"]=(strstr($product["titel2"], "&#60domino"))?strstr($product["titel2"], "&#60fruit",true):$product["titel2"];
 $str_product_titel = stripText(!empty($product["titel"]) && preg_match("/^<!--/",stripText($product["titel"]))==0?$product["titel"]."":$product["titel2"]);

On the first and second server I get this: apple On my 3rd server I got nothing.

Is there another way to remove the last part so I only get apple? (I don't want to do it on the "-" because there is a change that I will have other strings with multiple "-" in it. for example: apple - cake - &#60fruit&#60

(I know fruit is also written as: <fruit>, but with striptags, or striptext It wouldn't work).

So can someone help me with this little annoying problem? Thank you so much in advance!

2
  • I think the explode on "-" is the best, you can return the only FIRST element in the array. Commented Dec 27, 2011 at 8:57
  • 1
    Keeping your implementation aside, what exactly is your original requirement? Commented Dec 27, 2011 at 8:57

1 Answer 1

1

As you can see here the last parameter of strstr($haystack, $needle, $before_needle) (you uses it in your first non-commented line) is available only in PHP 5.3.0+. Check your PHP version on your three setups.

Anyway, you can emulate it in older PHP version using something like:

function my_strstr($haystack, $needle, $before_needle = false) {
    if (!$before_needle) return strstr($haystack, $needle);
    else return substr($haystack, 0, strpos($haystack, $needle));
}
Sign up to request clarification or add additional context in comments.

2 Comments

This worked perfectly! Thank you so much. I think I would never have found the solution.
You must always check official documentation when working with different versions of PHP. Lots of feature and function parameters have been added since PHP 5.2 and 5.3, but not all hosting services keep their servers updated :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.