php 如何从php字符串中删除第一个单词

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6823133/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:23:24  来源:igfitidea点击:

how to remove first word from a php string

phpword

提问by Monkeyalan

I'd like to remove the first word from a string using PHP. Tried searching but couldn't find an answer that I could make sense of.

我想使用 PHP 从字符串中删除第一个单词。尝试搜索,但找不到我能理解的答案。

eg: "White Tank Top" so it becomes "Tank Top"

例如:“白色背心”所以它变成了“背心”

Thanks

谢谢

回答by amosrivera

No need for explode or array manipulation, you can use function strstr:

不需要爆炸或数组操作,您可以使用函数strstr

echo strstr("White Tank Top"," ");
//Tank Top

UPDATE: Thanks to @Sid To remove the extra white space you can do:

更新:感谢@Sid 要删除额外的空白,您可以执行以下操作:

echo substr(strstr("White Tank Top"," "), 1);

回答by Alex

You can use the preg_replacefunction with the regex ^(\w+\s)that will match the first word of a string per se:

您可以将该preg_replace函数与^(\w+\s)将匹配字符串本身的第一个单词的正则表达式一起使用:

$str = "White Tank Top";
$str = preg_replace("/^(\w+\s)/", "", $str);
var_dump($str); // -> string(8) "Tank Top"

回答by symcbean

function remove_word($sentence)
{
 $words=array_shift(explode(' ', $sentence));
 return implode(' ', $words);
}

?

?

回答by PeeHaa

$string = 'White Tank Top';

$split = explode(' ', $string);
if (count($split) === 1) {
    // do you still want to drop the first word even if string only contains 1 word?
    // also string might be empty
} else {
    // remove first word
    unset($split[0]);
    print(implode(' ', $split));
}

回答by Chirag Pipariya

function remove_word($sentence)
{
    $exp = explode(' ', $sentence);
    $removed_words = array_shift($exp);
    if(count($exp)>1){
        $w = implode(' ', $exp);
    }else{
        $w = $exp[0];
    }
    return $w;
}

Try this function i hope it's work for you .

试试这个功能,我希望它对你有用。

回答by mickmackusa

If you are not guaranteed to have a space in your string, be careful to choose a technique that won't fail on such cases.

如果不能保证字符串中有空格,请小心选择不会在这种情况下失败的技术。

If using explode()be sure to limit the explosions for best efficiency.

如果使用explode()一定要限制爆炸以获得最佳效率。

Demonstration:

示范

$strings = ["White", "White Tank", "White Tank Top"];
foreach ($strings as $string) {
    echo "\n{$string}:";
    echo "\n-\t" , substr($string, 1 + (strpos($string, ' ') ?: -1));

    $explodeOnce = explode(' ', $string, 2);
    echo "\n-\t" , end($explodeOnce);              

    echo "\n-\t" , substr(strstr($string, " "), 1);

    echo "\n-\t" , ltrim(strstr($string, " "));

    echo "\n-\t" , preg_replace('~^\S+\s~', '', $string);
}

Output:

输出:

White:
-   White
-   White
-                       // strstr() returned false
-                       // strstr() returned false  
-   White
White Tank:
-   Tank
-   Tank
-   Tank
-   Tank
-   Tank
White Tank Top:
-   Tank Top
-   Tank Top
-   Tank Top
-   Tank Top
-   Tank Top

My preference is the regex technique because it is stable in all cases above and is a single function call. Note that there is no need for a capture group because the fullstring match is being replaced. ^matches the start of the string, \S+matches one or more non-whitespace characters and \smatches one whitespace character.

我更喜欢正则表达式技术,因为它在上述所有情况下都是稳定的,并且是单个函数调用。请注意,不需要捕获组,因为正在替换全字符串匹配。^匹配字符串的开头,\S+匹配一个或多个非空白字符并\s匹配一个空白字符。