How to remove the first character of string in PHP?

后端 未结 13 2302
一整个雨季
一整个雨季 2020-12-02 06:00
$str=\':this is a applepie :) \';

How to use PHP, Remove the first character :

13条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-02 06:34

    Exec time for the 3 answers :

    Remove the first letter by replacing the case

    $str = "hello";
    $str[0] = "";
    // $str[0] = false;
    // $str[0] = null;
    // replaced by �, but ok for echo
    

    Exec time for 1.000.000 tests : 0.39602184295654 sec


    Remove the first letter with substr()

    $str = "hello";
    $str = substr($str, 1);
    

    Exec time for 1.000.000 tests : 5.153294801712 sec


    Remove the first letter with ltrim()

    $str = "hello";
    $str= ltrim ($str,'h');
    

    Exec time for 1.000.000 tests : 5.2393000125885 sec


    Remove the first letter with preg_replace()

    $str = "hello";
    $str = preg_replace('/^./', '', $str);
    

    Exec time for 1.000.000 tests : 6.8543920516968 sec

提交回复
热议问题