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"
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);
strtr. Was about to type out 3 lines using strpos + substr when I saw your answer. Add an ltrim in case there are multiple spaces and you're golden.strlen showed the white space interesting, added your solution to the answer thanks!function remove_word($sentence)
{
$words=array_shift(explode(' ', $sentence));
return implode(' ', $words);
}
?
return implode(' ', array_shift(explode(' ', $sentence)));? i was never quite sure with php.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.
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.
$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.