Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
473 views
in Technique[技术] by (71.8m points)

trim - PHP ltrim behavior with character list

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...