How to obtain the last word of a string

后端 未结 8 2218
囚心锁ツ
囚心锁ツ 2020-12-03 19:53

we have that string:

\"I like to eat apple\"

How can I obtain the result \"apple\" ?

相关标签:
8条回答
  • 2020-12-03 20:33
    <?php
    // your string
    $str = 'I like to eat apple';
    
    // used end in explode, for getting last word
    $str_explode=end(explode("|",$str));
    echo    $str_explode;
    
    ?>
    

    Output will be apple.

    0 讨论(0)
  • 2020-12-03 20:34
    // Your string
    $str = "I like to eat apple";
    // Split it into pieces, with the delimiter being a space. This creates an array.
    $split = explode(" ", $str);
    // Get the last value in the array.
    // count($split) returns the total amount of values.
    // Use -1 to get the index.
    echo $split[count($split)-1];
    
    0 讨论(0)
  • 2020-12-03 20:35

    Try this:

    $array = explode(' ',$sentence);
    $last = $array[count($array)-1];
    
    0 讨论(0)
  • 2020-12-03 20:38
    $str = 'I like to eat apple';
    echo substr($str, strrpos($str, ' ') + 1); // apple
    
    0 讨论(0)
  • 2020-12-03 20:40

    a bit late to the party but this works too

    $last = strrchr($string,' ');
    

    as per http://www.w3schools.com/php/func_string_strrchr.asp

    0 讨论(0)
  • 2020-12-03 20:47

    Try:

    $str = "I like to eat apple";
    end((explode(" ",$str));
    
    0 讨论(0)
提交回复
热议问题