ltrim strips more than needed

前端 未结 5 1119
春和景丽
春和景丽 2021-01-25 01:36

Is there a way to limit exactly what ltrim removes in PHP.

I had a the following outcome:

$str = \"axxMyString\";
$newStr = ltrim($str, \"ax\");
// resu         


        
5条回答
  •  隐瞒了意图╮
    2021-01-25 02:20

    I think the confusion has come in because ltrim receives a list of characters to remove, not a string. Your code removes all occurrences of a or x not the string ax. So axxxaxxaMyString becomes MyString and axxaiaxxaMyString becomes iaxxaMyString.

    If you know how many characters you need to remove, you can use substr() otherwise for more complicated patterns, you can use regular expressions via preg_replace().

提交回复
热议问题