为什么这总是显示字符串8,尽管使用了trim()函数?它应该从两边删除空白空间吗?
$text = " Raveen ";
echo "Before trim: ".strlen($text); //prints 8
trim($text);
echo "<br>After trim: ".strlen($text); //now prints 6
发布于 2016-04-20 07:51:44
您不是在回显新变量,而是接受一个变量,给它一个新的形式,而不是将它赋值给任何东西。
$text = " Raveen ";
echo "Before trim: ".strlen($text); //prints 8
echo "<br>After trim: ".strlen(trim($text)); //now prints 6
这就是你想要它工作的方式。或者类似于:
$text = " Raveen ";
echo "Before trim: ".strlen($text); //prints 8
$text = trim($text);
echo "<br>After trim: ".strlen($text); //now prints 6
我确信你知道Trim功能是如何工作的,所以指向它的链接是没有用的。尽管如此,看看一些简单的例子让你走上正确的轨道也没什么坏处
发布于 2016-04-20 07:50:56
函数返回修剪后的字符串文档这里,因此代码如下所示:
$text = " Raveen ";
echo "Before trim: ".strlen($text); //prints 8
$trimed_text = trim($text);
echo "<br>After trim: ".strlen($trimed_text); //now prints 6
发布于 2016-04-20 07:54:13
函数移除空白,但不更改它返回的结果为字符串的原始字符串。您的代码应该如下所示:
$text = " Raveen ";
echo "Before trim: ".strlen($text); //prints 8
echo "<br>After trim: ".strlen(trim($text)); //now prints 6
https://stackoverflow.com/questions/36737119
复制相似问题