Splitting strings in PHP and get last part

前端 未结 13 2281
无人及你
无人及你 2020-12-08 04:39

I need to split a string in PHP by \"-\" and get the last part.

So from this:

abc-123-xyz-789

I expect to get

相关标签:
13条回答
  • 2020-12-08 04:57

    You can do it like this:

    $str = "abc-123-xyz-789";
    $arr = explode('-', $str);
    $last = array_pop( $arr );
    echo $last; //echoes 789
    
    0 讨论(0)
  • 2020-12-08 05:02

    As has been mentioned by others, if you don't assign the result of explode() to a variable, you get the message:

    E_STRICT: Strict standards: Only variables should be passed by reference

    The correct way is:

    $words = explode('-', 'hello-world-123');
    $id = array_pop($words); // 123
    $slug = implode('-', $words); // hello-world
    
    0 讨论(0)
  • 2020-12-08 05:02

    You can do it like this:

    $str = "abc-123-xyz-789";
    $last = array_pop( explode('-', $str) );
    echo $last; //echoes 789
    
    0 讨论(0)
  • 2020-12-08 05:05

    To satisfy the requirement that "it needs to be as fast as possible" I ran a benchmark against some possible solutions. Each solution had to satisfy this set of test cases.

    $cases = [
        'aaa-zzz'                     => 'zzz',
        'zzz'                         => 'zzz',
        '-zzz'                        => 'zzz',
        'aaa-'                        => '',
        ''                            => '',
        'aaa-bbb-ccc-ffffd-eee-fff-zzz' => 'zzz',
    ];
    

    Here are the solutions:

    function test_substr($str, $delimiter = '-') {
        $idx = strrpos($str, $delimiter);
        return $idx === false ? $str : substr($str, $idx + 1);
    }
    
    function test_end_index($str, $delimiter = '-') {
        $arr = explode($delimiter, $str);
        return $arr[count($arr) - 1];
    }
    
    function test_end_explode($str, $delimiter = '-') {
        $arr = explode($delimiter, $str);
        return end($arr);
    }
    
    function test_end_preg_split($str, $pattern = '/-/') {
        $arr = preg_split($pattern, $str);
        return end($arr);
    }
    

    Here are the results after each solution was run against the test cases 1,000,000 times:

    test_substr               : 1.706 sec
    test_end_index            : 2.131 sec  +0.425 sec  +25%
    test_end_explode          : 2.199 sec  +0.493 sec  +29%
    test_end_preg_split       : 2.775 sec  +1.069 sec  +63%
    

    So turns out the fastest of these was using substr with strpos. Note that in this solution we must check strpos for false so we can return the full string (catering for the zzz case).

    0 讨论(0)
  • 2020-12-08 05:07

    Just check whether or not the delimiting character exists, and either split or don't:

    if (strpos($potentiallyDelimitedString, '-') !== FALSE) {
      found delimiter, so split
    }
    
    0 讨论(0)
  • 2020-12-08 05:07

    You can use array_pop combined with explode

    Code:

    $string = 'abc-123-xyz-789';
    $output = array_pop(explode("-",$string));
    echo $output;
    

    DEMO: Click here

    0 讨论(0)
提交回复
热议问题