我一定不能完全理解PHP';s trim()函数


I must not completely understand PHP's trim() function

我注意到我的代码输出中有一些奇怪的结果,我追溯到trim((。我在phptester.net上测试并验证了我的结果(请继续验证(。下面的小脚本是如何给出所显示的结果的?

$x = "d1d1d1";
define("REP", "xqzxqjb1");
echo trim($x, REP); //the output is the string 'd1d1d'
//Same result if $x = 'xqzxqjb1d1d1d1xqzxqjb1' OR $x = 'd1d1d1xqzxqjb1' OR $x = 'xqzxqjb1d1d1d1';

为什么其中任何一个的输出都不是"d1d1d1"?

为什么不应该这样?

trim((-从字符串的开始和结束处去除空白(或其他字符(

trim('d1d1d1', 'xqzxqjb1');
                  ^---remove these chars from the string

x、 q,z,x,q,j,b-不在字符串中,忽略
1-出现在字符串的END处,剥去它:

php > echo trim('d1d1d1', "xqzxqjb1");
d1d1d
     ^---see, no 1

因为trim将从字符串末尾删除任意数量的任何字符,而不是(正如您所认为的(整个字符串。

在所有情况下,最终都会使用d1d1d,因为它是去掉REP中的字符后字符串的中心部分。