strstr php or alternative part string

两盒软妹~` 提交于 2019-12-24 09:37:02

问题


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


来源:https://stackoverflow.com/questions/8642685/strstr-php-or-alternative-part-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!