php 如何在PHP中获取句子的第一个单词?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2476789/
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
How to get the first word of a sentence in PHP?
提问by ali
I want to extract the first word of a variable from a string. For example, take this input:
我想从字符串中提取变量的第一个单词。例如,采用以下输入:
<?php $myvalue = 'Test me more'; ?>
The resultant output should be Test, which is the first word of the input.
How can I do this?
结果输出应该是Test,这是输入的第一个单词。我怎样才能做到这一点?
回答by salathe
There is a string function (strtok) which can be used to split a string into smaller strings (tokens) based on some separator(s). For the purposes of this thread, the first word (defined as anything before the first space character) of Test me morecan be obtained by tokenizingthe string on the space character.
有一个字符串函数(strtok)可用于根据某些分隔符将字符串拆分为较小的字符串(标记)。就本线程而言,第一个单词(定义为第一个空格字符之前的任何内容)Test me more可以通过对空格字符上的字符串进行标记来获得。
<?php
$value = "Test me more";
echo strtok($value, " "); // Test
?>
For more details and examples, see the strtok PHP manual page.
有关更多详细信息和示例,请参阅strtok PHP 手册页。
回答by codaddict
回答by Yacoby
If you have PHP 5.3
如果你有 PHP 5.3
$myvalue = 'Test me more';
echo strstr($myvalue, ' ', true);
note that if $myvalueis a string with one word strstrdoesn't return anything in this case. A solution could be to append a space to the test-string:
请注意,在这种情况下,如果$myvalue是一个包含一个单词的字符串,strstr则不会返回任何内容。解决方案可能是在测试字符串后附加一个空格:
echo strstr( $myvalue . ' ', ' ', true );
That will always return the first word of the string, even if the string has just one word in it
这将始终返回字符串的第一个单词,即使字符串中只有一个单词
The alternative is something like:
替代方案是这样的:
$i = strpos($myvalue, ' ');
echo $i !== false ? $myvalue : substr( $myvalue, 0, $i );
Or using explode, which has so many answers using it I won't bother pointing out how to do it.
或者使用爆炸,它有很多使用它的答案,我不会费心指出如何去做。
回答by AntonioCS
You could do
你可以做
echo current(explode(' ',$myvalue));
回答by Lakshmi Rawat Singhania
Even though it is little late, but PHP has one better solution for this:
尽管为时已晚,但 PHP 对此有一个更好的解决方案:
$words=str_word_count($myvalue, 1);
echo $words[0];
回答by wheelmaker
Similar to accepted answer with one less step:
类似于接受的答案,少一步:
$my_value = 'Test me more';
$first_word = explode(' ',trim($my_value))[0];
//$first_word == 'Test'
回答by Lupuz
Just in case you are not sure the string starts with a word...
以防万一您不确定字符串是否以单词开头...
$input = ' Test me more ';
echo preg_replace('/(\s*)([^\s]*)(.*)/', '', $input); //Test
回答by rekha_sri
Using split function also you can get the first word from string.
使用 split 函数还可以从字符串中获取第一个单词。
<?php
$myvalue ="Test me more";
$result=split(" ",$myvalue);
echo $result[0];
?>
回答by Ulf
<?php
$value = "Hello world";
$tokens = explode(" ", $value);
echo $tokens[0];
?>
Just use explode to get every word of the input and output the first element of the resulting array.
只需使用 expand 获取输入的每个单词并输出结果数组的第一个元素。

