1

目前我正在使用 strstr() 获取字符串的第一部分。我的项目在 3 台服务器上运行。1. 开发服务器 2. 测试服务器 3. 实时服务器

strstr() 在开发服务器和测试服务器上工作正常,但是当我将项目上线时,strstr 什么也没打印。例如:

我使用这段代码:

//$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"]);

在第一台和第二台服务器上,我得到了这个:apple 在我的第三台服务器上,我什么也没得到。

有没有另一种方法可以删除最后一部分,所以我只得到苹果?(我不想在“-”上这样做,因为有一个变化,我将有其他带有多个“-”的字符串。例如:apple - cake - <fruit<

(我知道fruit也写为:<fruit>,但带有striptags,或striptext 是行不通的)。

那么有人可以帮我解决这个烦人的小问题吗?非常感谢您!

4

1 回答 1

1

正如您在此处strstr($haystack, $needle, $before_needle)看到的那样(您在第一个非注释行中使用它)的最后一个参数仅在 PHP 5.3.0+ 中可用。检查您的三个设置的 PHP 版本。

无论如何,您可以使用以下方法在旧 PHP 版本中模拟它:

function my_strstr($haystack, $needle, $before_needle = false) {
    if (!$before_needle) return strstr($haystack, $needle);
    else return substr($haystack, 0, strpos($haystack, $needle));
}
于 2011-12-27T08:57:42.110 回答