PHP ltrim行为与字符列表 [英] PHP ltrim behavior with character list
问题描述
我试图使用php ltrim函数实现从字符串中剥离一些开头部分.它可以正常工作直到在冒号后加上一个i字符.如果在冒号之后找到i,则只需忽略i字符.我知道可以用substr或任何其他方式来完成它,但是我想知道为什么它会随着修剪发生.例如.
I was trying to achieve stripping off some beginning part from a string using php ltrim function. It works fine until it get a i character after colon : . if it find i after colon it simply ignore the i character. I know it can be done with substr or any other way but I want to know why its happening with trim. For example.
ltrim('mailto:bob@example.com','mailto:');
上面的函数将返回bob@example.com
the above function will return bob@example.com
但是如果我把我放在冒号后面..
but if I put i after colon.. for example
ltrim('mailto:info@example.com','mailto:');
这将返回nfo@example.com
this one will return nfo@example.com
有人可以解释发生了什么事吗?
Can anybody explain what is happening?
推荐答案
ltrim的第二个参数是要从字符串左侧删除的字符列表.
The second argument to ltrim is a list of characters to remove from the left side of the string.
如果您这样做
<?php
ltrim('lllliiiiaaaaaatttttt', 'mailto:');
?>
您将获得一个空字符串作为返回值.
You would get an empty string as the return value.
尝试以下方法:
<?php
$email = 'mailto:bob@example.com';
$fixedEmail = substr($email, 0, 7) == 'mailto:' ? substr($email, 7) : $email;
?>
这篇关于PHP ltrim行为与字符列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!