Remove a string from the beginning of a string

后端 未结 11 2053
忘了有多久
忘了有多久 2020-11-28 06:30

I have a string that looks like this:

$str = \"bla_string_bla_bla_bla\";

How can I remove the first bla_; but only if it\'s fo

11条回答
  •  情书的邮戳
    2020-11-28 07:19

    Nice speed, but this is hard-coded to depend on the needle ending with _. Is there a general version? – toddmo Jun 29 at 23:26

    A general version:

    $parts = explode($start, $full, 2);
    if ($parts[0] === '') {
        $end = $parts[1];
    } else {
        $fail = true;
    }
    

    Some benchmarks:

    On my quite old home PC:

    $ php bench.php
    

    Outputs:

       strpos+strlen : 0.158388 s
             explode : 0.126772 s
    

提交回复
热议问题