php 获取字符串的最后一个单词
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18612872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the last word of a string
提问by Roy van Wensen
I have tried a few things to get a last part out I done this:
我已经尝试了一些方法来完成我完成的最后一部分:
$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]})
The first one won't work and the second returns an array but a realy need string.
第一个不起作用,第二个返回一个数组但是一个真正需要的字符串。
回答by MLeFevre
If you're after the last word in a sentence, why not just do something like this?
如果你在一个句子中的最后一个词之后,为什么不做这样的事情呢?
$string = '?Sim-only 500 ?| Internet 2500';
$pieces = explode(' ', $string);
$last_word = array_pop($pieces);
echo $last_word;
I wouldn't recommend using regular expressions as it's unnecessary, unless you really want to for some reason.
我不建议使用正则表达式,因为它是不必要的,除非您出于某种原因真的想这样做。
$string = 'Retrieving the last word of a string using PHP.';
preg_match('/[^ ]*$/', $string, $results);
$last_word = $results[0]; // $last_word = PHP.
The substr()
method they gave is probably abit better
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.
it is faster, although it really doesn't make that much of a difference on things like this. If you're constantly needing to know the last word on a 100,000 word string, you should probably be going about it in a different way.
它更快,尽管它在这样的事情上并没有太大的区别。如果您经常需要知道 100,000 个单词字符串中的最后一个单词,您可能应该以不同的方式进行处理。
回答by hunter
This should work for you:
这应该适合你:
$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);
回答by ranieuwe
It depends on what you try to do (it is hard to understand from your description) but to get the last word from a string you can do:
这取决于您尝试做什么(从您的描述中很难理解)但是要从字符串中获取最后一个单词,您可以执行以下操作:
$split = explode(" ", $string);
echo $split[count($split)-1];
See How to obtain the last word of a stringfor more information.
有关详细信息,请参阅如何获取字符串的最后一个单词。
回答by M Khalid Junaid
There you go a generic function to get last words from string
那里有一个通用函数来从字符串中获取最后一句话
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 );
回答by Elron
If you want to wrap the last word with a span:
如果你想用 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>