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

发布于 2024-11-25 22:41:00 字数 87 浏览 5 评论 0原文

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

例如:“White Tank Top”因此变成“Tank Top”

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

罗罗贝儿 2024-12-02 22:41:00

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

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

更新:谢谢到@Sid 要删除多余的空白,您可以执行以下操作:

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

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

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

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

echo substr(strstr("White Tank Top"," "), 1);
鸠书 2024-12-02 22:41:00

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

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

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

$str = "White Tank Top";
$str = preg_replace("/^(\w+\s)/", "", $str);
var_dump($str); // -> string(8) "Tank Top"
隔纱相望 2024-12-02 22:41:00
function remove_word($sentence)
{
 $words=array_shift(explode(' ', $sentence));
 return implode(' ', $words);
}

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

?

掩饰不了的爱 2024-12-02 22:41:00
$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));
}
$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));
}
凹づ凸ル 2024-12-02 22:41:00

确保您选择的方法能够正确处理空字符串、单个单词(不含空格)和多个单词。

我个人会使用 preg_replace() 因为它是最直接的。

供您考虑:(演示

function substrStrpos($text) {
    return substr($text, 1 + (strpos($text, ' ') ?: -1));
}

function explodeSlice($text) {
    $explodeOnce = explode(' ', $text, 2);
    return array_pop($explodeOnce);
}

function substrStrstr($text) {
    return substr(strstr($text, " "), 1);
}

function ltrimStrstr($text) {
    return ltrim(strstr($text, " "));
}

function pregReplace($text) {
    return preg_replace('~^\S+\s*~', '', $text);
}

测试和预期结果:

$tests = [
    '' => '',
    'White' => '',
    'White Tank' => 'Tank',
    'White Tank Top' => 'Tank Top',
];

结果:

|   func \ tests|  [empty]  |  White  |  White Tank  |  White Tank Top  |
------------------------------------------------------------------------
| substrStrpos| ✅ |

Make sure that you opt for an approach which will appropriately handle an empty string, a lone word (with no spaces), and multiple words.

I personally would use preg_replace() because it is most direct.

For your consideration: (Demo)

function substrStrpos($text) {
    return substr($text, 1 + (strpos($text, ' ') ?: -1));
}

function explodeSlice($text) {
    $explodeOnce = explode(' ', $text, 2);
    return array_pop($explodeOnce);
}

function substrStrstr($text) {
    return substr(strstr($text, " "), 1);
}

function ltrimStrstr($text) {
    return ltrim(strstr($text, " "));
}

function pregReplace($text) {
    return preg_replace('~^\S+\s*~', '', $text);
}

Tests and expected results:

$tests = [
    '' => '',
    'White' => '',
    'White Tank' => 'Tank',
    'White Tank Top' => 'Tank Top',
];

Outcomes:

|   func \ tests|  [empty]  |  White  |  White Tank  |  White Tank Top  |
------------------------------------------------------------------------
|   substrStrpos|    ✅    |    ????   |      ✅      |        ✅       |
------------------------------------------------------------------------
|   explodeSlice|    ✅    |    ????   |      ✅      |        ✅       |
------------------------------------------------------------------------
|   substrStrstr|    ✅    |    ✅   |      ✅      |        ✅       |
------------------------------------------------------------------------
|    ltrimStrstr|    ✅    |    ✅   |      ✅      |        ✅       |
------------------------------------------------------------------------
|    pregReplace|    ✅    |    ✅   |      ✅      |        ✅       |

Note that substrStrpos('White') and explodeSlice('White') will both return White.

屋顶上的小猫咪 2024-12-02 22:41:00
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;
}

尝试一下这个功能,希望它对您有用。

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 .

煮酒 2024-12-02 22:41:00

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

如果使用explode(),请务必限制爆炸以获得最佳效率。

演示

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

输出:

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

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

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.

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 \s matches one whitespace character.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文