PHP ltrim behavior with character list

后端 未结 2 1992
深忆病人
深忆病人 2020-11-30 14:23

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

相关标签:
2条回答
  • 2020-11-30 15:01

    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');
    
    0 讨论(0)
  • 2020-11-30 15:24

    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;
    ?>
    
    0 讨论(0)
提交回复
热议问题