获取字符串的最后一个单词


Get the last word of a string

我尝试了一些方法来获得最后一部分我做到了:

$string = 'Sim-only 500 | Internet 2500';
preg_replace("Sim-Only ^([1-9]|[1-9][0-9]|[1-9][0-9][0-9][0-9])$ | Internet ","",$string
AND
preg_match("/[^ ]*$/","",{abo_type[1]})

第一个不起作用,第二个返回一个数组,但返回一个真正需要的字符串。

如果你在句子的最后一个单词后面,为什么不这样做呢?

$string = '​Sim-only 500 ​| Internet 2500';
$pieces = explode(' ', $string);
$last_word = array_pop($pieces);
echo $last_word;

我不建议使用正则表达式,因为这是不必要的,除非出于某种原因你真的想这样做。

$string = 'Retrieving the last word of a string using PHP.';
preg_match('/[^ ]*$/', $string, $results);
$last_word = $results[0]; // $last_word = PHP.

如果资源/效率/开销是一个问题,那么使用substr()方法将比这两种方法都好。

$string = 'Retrieving the last word of a string using PHP.';
$last_word_start = strrpos($string, ' ') + 1; // +1 so we don't include the space in our result
$last_word = substr($string, $last_word_start); // $last_word = PHP.

它更快,尽管在这样的事情上并没有太大的区别。如果你一直需要知道100000个单词串中的最后一个单词,你可能应该用不同的方式来做。

这应该适用于您:

$str = "fetch the last word from me";
$last_word_start = strrpos ( $str , " ") + 1;
$last_word_end = strlen($str) - 1;
$last_word = substr($str, $last_word_start, $last_word_end);

这取决于你试图做什么(从你的描述中很难理解),但要从字符串中获得最后一个单词,你可以做:

$split = explode(" ", $string);
echo $split[count($split)-1];

有关详细信息,请参阅如何获取字符串的最后一个单词。

现有的解决方案都能很好地工作,但我想要一个单一的解决方案。explode()会把一个句子分成单词,但试图把它直接传给array_pop()end()会给出"0";只有变量应该通过引用"传递;注意array_slice()救援:

$string = 'Sim-only 500 | Internet 2500';
echo array_slice(explode(' ', $string), -1)[0];

您可以使用一个通用函数从字符串中获取最后一个单词

public function get_last_words($amount, $string)
{
    $amount+=1;
    $string_array = explode(' ', $string);
    $totalwords= str_word_count($string, 1, 'àáãç3');
    if($totalwords > $amount){
        $words= implode(' ',array_slice($string_array, count($string_array) - $amount));
    }else{
        $words= implode(' ',array_slice($string_array, count($string_array) - $totalwords));
    }
    return $words;
}
$string = '​Sim-​only 500 | Internet 2500​';
echo get_last_words(1,  $string );

如果您想用span:包装最后一个单词

<?php
/**
 * Wrap last word with span
 * @author: Elron
 * https://stackoverflow.com/questions/18612872/get-the-last-word-of-a-string
 */
function wrap_last_word($string) {
    // Breaks string to pieces
    $pieces = explode(" ", $string);
    // Modifies the last word
    $pieces[count($pieces)-1] = '<span class="is-last-word">' . $pieces[count($pieces)-1] . '</span>';
    // Returns the glued pieces
    return implode(" ", $pieces);
}
wrap_last_word('hello this is wrapped');
// returns this:
// hello this is <span class="is-last-word">wrapped</span>

以下是从字符串中获取最后一个单词的另一种方法

$my_string = "fetch the last word from me";
 
// Explode the string into an array
$my_string = explode(" ", $my_string);
// target the last word with end() function 
$my_string = end($my_string);
echo $my_string;

结果me

有一个单行解决方案。

basename(str_replace(' ', '/', 'hello world'));

将返回世界

使用regexp:的单行解决方案

preg_replace('/.*'s/', '', $string);

这抑制了以空格结尾的所有内容,并且由于regexp总是匹配";尽可能长";,这将匹配除最后一句话以外的所有内容。这也具有与任何";空间";字符(制表符、换行符…)。

但最好的性能/资源消耗解决方案是(如果只有一个词,则不起作用):

substr($string, strrpos($string, ' ') + 1);

只需使用此函数

function last_word($string)
{
    $pieces = explode(' ', $string);
    return array_pop($pieces);
}