我想使用 PHP 删除字符串中的第一个单词。 尝试搜索但找不到我能理解的答案。
例如:“White Tank Top”因此变成“Tank Top”
谢谢
无需爆炸或数组操作,可以使用函数strstr:
echo strstr("White Tank Top"," ");
//Tank Top
更新:感谢@Sid 要删除多余的空白,您可以执行以下操作:
echo substr(strstr("White Tank Top"," "), 1);
您可以将
preg_replace
函数与正则表达式 ^(\w+\s)
一起使用,它将匹配字符串本身的第一个单词:
$str = "White Tank Top";
$str = preg_replace("/^(\w+\s)/", "", $str);
var_dump($str); // -> string(8) "Tank Top"
function remove_word($sentence)
{
$words=array_shift(explode(' ', $sentence));
return implode(' ', $words);
}
?
$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));
}
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;
}
尝试一下这个功能,希望它对您有用。
确保您选择的方法能够正确处理空字符串、单个单词(不含空格)和多个单词。
我个人会使用
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| ✅ | 💩 | ✅ | ✅ |
------------------------------------------------------------------------
| explodeSlice| ✅ | 💩 | ✅ | ✅ |
------------------------------------------------------------------------
| substrStrstr| ✅ | ✅ | ✅ | ✅ |
------------------------------------------------------------------------
| ltrimStrstr| ✅ | ✅ | ✅ | ✅ |
------------------------------------------------------------------------
| pregReplace| ✅ | ✅ | ✅ | ✅ |
请注意,
substrStrpos('White')
和explodeSlice('White')
都会返回White
。
如果不能保证字符串中有空格,请小心选择在这种情况下不会失败的技术。
如果使用
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
匹配一个空白字符。