繁体   English   中英

PHP strstr()函数替换

[英]PHP strstr() function replacement

以下是我在PHP 5.3.10上使用strstr在本地主机上的示例

<?php
$string  = '25_testing';
$test = strstr($string, '_', true); // As of PHP 5.3.0
echo $test; // prints 25
?>

好吧,我将我的文件上传到我的托管服务器上,但它们正在运行PHP 5.2,因此函数strstr($string, '_', true)不起作用。 有没有我可以使用的替代方案,以获得相同的结果?

尝试这个...

首先,在“_”(包括其自身)之后返回字符串,然后将其替换为空。 不是很好,但它的工作原理;)

<?php
    $string = "25_testing";
    echo str_replace(stristr($string,"_"),"",$string);
?>

要么

<?php
    $string = "25_testing";
    echo str_replace(strstr($string,"_"),"",$string);
?>

您可以结合使用strpossubstr

<?php
$string  = '25_testing';
$test = substr($string, 0, strpos('_')); //maybe check if strpos does not return FALSE
echo $test; 
?>

删除true ,并将针的长度从结果的末尾切掉。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM