strstr php或替代零件字符串 [英] strstr php or alternative part string

查看:96
本文介绍了strstr php或替代零件字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

strstr()在开发服务器和测试服务器上都可以正常工作,但是当我将项目置于活动状态时,strstr不会打印任何内容.例如:

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:

我使用以下代码:

//$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.

还有另一种方法可以去除最后一部分,所以我只能拿到苹果吗? (我不想在-"上执行此操作,因为发生了更改,我将在其中添加其他带有多个-"的字符串.例如:apple-cake-&#60fruit&#60

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

(我知道水果也被写成:< fruit>,但是带有剥离标签或striptext,这是行不通的).

(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!

推荐答案

如您所见

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.

无论如何,您可以使用类似以下内容的旧PHP版本进行仿真:

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

这篇关于strstr php或替代零件字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆