PHP ltrim behavior with character list

两盒软妹~` 提交于 2019-11-27 05:22:21
Alec Gorge

The second argument to ltrim is a list of characters to remove from the left side of the string.

If you did

<?php
    ltrim('lllliiiiaaaaaatttttt', 'mailto:');
?>

You would get an empty string as the return value.

Try this instead:

<?php
    $email = 'mailto:bob@example.com';
    $fixedEmail = substr($email, 0, 7) == 'mailto:' ? substr($email, 7) : $email;
?>

ltrim() works differently that it trims all the given characters from the left until a non-matching character is found. Therefore when you put an "i" after colon in the email address, the first unmatched character is "n" and so it trims everything before it.

If your intent is to remove "mailto:", you may simply try:

str_replace('mailto:', '', 'mailto:info@example.com');
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!