Skip to content
Advertisement

PHP ltrim() function is giving a hard time

I used the ltrim() function in php to trim “:new” from “:newname” but it is also trimming the “n” of name and returning “ame”.

Here is the code :

<?php
echo ltrim(":newname",":new"); 
?> 

Which returns

ame

Is there anything I can do ? Or any other function for the specefic task mentioned above ?

I already has tried using “:newName” instead of “:newname” but I can’t do that every time I need to trim only “:new” and the string after “:new” can vary so can’t do capitalizing the next letter.

Advertisement

Answer

The problem is that the second argument is a character mask as stated in the documenation – meaning that it strips all the characters defined in that string.

As a simple workaround you could use preg_replace:

<?php
$str = preg_replace('/^:new/', '', ':newname');
echo $str; // prints name
?> 
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement