strstr php or alternative part string

2019-08-01 02:48发布

问题:

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!

回答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));
}