1

这是我如何在 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)不起作用。有没有我可以使用的替代方法来获得相同的结果?

4

3 回答 3

2

尝试这个...

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

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

或者

<?php
    $string = "25_testing";
    echo str_replace(strstr($string,"_"),"",$string);
?>
于 2012-02-28T15:23:42.407 回答
1

您可以使用strpos和的组合substr

<?php
$string  = '25_testing';
$test = substr($string, 0, strpos('_')); //maybe check if strpos does not return FALSE
echo $test; 
?>
于 2012-02-28T15:16:01.243 回答
0

放下true,并从结果的末端切下针的长度。

于 2012-02-28T15:14:05.767 回答