Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP ltrim behavior with character list

Tags:

php

trim

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:[email protected]','mailto:');

the above function will return [email protected]

but if I put i after colon.. for example

ltrim('mailto:[email protected]','mailto:');

this one will return [email protected]

Can anybody explain what is happening?

like image 570
Hasanavi Avatar asked Mar 05 '26 05:03

Hasanavi


1 Answers

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:[email protected]';
    $fixedEmail = substr($email, 0, 7) == 'mailto:' ? substr($email, 7) : $email;
?>
like image 54
Alec Gorge Avatar answered Mar 07 '26 19:03

Alec Gorge