30

我已经尝试了一些事情来完成我这样做的最后一部分:

$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]})

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

4

9 回答 9

64

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

$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.

它更快,尽管它对这样的事情并没有太大的影响。如果您经常需要知道 100,000 字串上的最后一个字,您可能应该以不同的方式处理它。

于 2013-09-04T11:47:35.833 回答
9

这应该适合你:

$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);
于 2013-09-04T11:55:08.723 回答
5

这取决于您尝试做什么(从您的描述中很难理解)但是要从字符串中获取最后一个单词,您可以执行以下操作:

$split = explode(" ", $string);

echo $split[count($split)-1];

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

于 2013-09-04T11:47:32.727 回答
1

那里有一个通用函数来从字符串中获取最后一个单词

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 );
于 2013-12-25T12:29:30.940 回答
1

如果你想用跨度包装最后一个单词:

<?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>
于 2020-05-23T13:19:11.567 回答
1

现有的解决方案都可以正常工作,但我想要一个单线。explode()将句子拆分成单词,但试图将其直接传递到array_pop()end()给出“只有变量应该通过引用传递”的通知。array_slice()救援:

$string = 'Sim-only 500 | Internet 2500';
echo array_slice(explode(' ', $string), -1)[0];
于 2020-11-12T18:04:47.637 回答
0

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

$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

于 2020-11-02T07:16:47.597 回答
0

使用正则表达式的单行解决方案:

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

这会抑制以空格结尾的所有内容,并且由于正则表达式始终“尽可能长时间”匹配,因此它将匹配除最后一个单词之外的所有内容。这对于使用任何“空格”字符(制表符、换行符...)也有好处。

但最好的性能/资源消耗解决方案是:

substr($string, strrpos($string, ' ') + 1);
于 2020-12-29T13:10:00.363 回答
0

有一个单线解决方案。

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

将回归世界

于 2021-12-27T22:40:50.643 回答