ltrim strips more than needed

前端 未结 5 1176
春和景丽
春和景丽 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:29

    If you know that the first two characters should be stripped, use:

    $str = substr($str, 2);
    

    ltrim($str, $chars) removes every occurence from $chars from the left side of the string.

    If you only want to strip the first two characters when these equals ax, you would use:

    if (substr($str, 0, 2) == 'ax') $str = substr($str, 2);
    
    • Manual page on ltrim()
    • Manual page on substr()

提交回复
热议问题