24

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"

Thanks

pigrammer
  • 2,603
  • 1
  • 11
  • 24
Monkeyalan
  • 241
  • 1
  • 2
  • 3

6 Answers6

66

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);
amosrivera
  • 26,114
  • 9
  • 67
  • 76
  • 12
    This is better than the array manipulation answers, however this will leave the space character too. This can be cut out by doing echo substr(strstr("White Tank Top"," "), 1); – Sid Jul 25 '11 at 22:37
  • 1
    +1 for simplicity, I keep forgetting about `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. – Fanis Hatzidakis Jul 25 '11 at 22:38
  • @Sid white space at the beginning you mean? i just gave it a check and it worked fine for me can you verify? – amosrivera Jul 25 '11 at 22:39
  • @Fanis i totally get you, with so many different functions for string manipulation in php is easy to forget. – amosrivera Jul 25 '11 at 22:40
  • @amosrivera like @Sid said it will include the space it's matching on. You won't see it easily by echoing out on a web page but do a `strlen` or run it in a debugger and you'll spot it :) – Fanis Hatzidakis Jul 25 '11 at 22:42
  • 1
    @Sid @Fanis `strlen` showed the white space interesting, added your solution to the answer thanks! – amosrivera Jul 25 '11 at 22:45
8

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"
Alex
  • 151
  • 1
  • 6
3
function remove_word($sentence)
{
 $words=array_shift(explode(' ', $sentence));
 return implode(' ', $words);
}

?

symcbean
  • 47,736
  • 6
  • 59
  • 94
  • could you also condense that down into `return implode(' ', array_shift(explode(' ', $sentence)));`? i was never quite sure with php. – Patrick Perini Jul 25 '11 at 22:32
  • This solution is a memory hog. If the string has lots of spaces the arrays will become large. amosrivera's solution is much better (though not quite complete as it leaves the space character too). – Sid Jul 25 '11 at 22:36
  • @Patrick Perini: no - array_shift() returns the first element in the array - not the modified array – symcbean Jul 26 '11 at 08:29
  • @Sid: first, amosrivera's answer is semantically different (consider what happens with a string containing no spaces) also I would hardly call this a memory hog - I would expect it to use more memory than amosrivera's answer - but without testing each method I can't tell which has a bigger effective footprint on the system. – symcbean Jul 26 '11 at 08:33
  • Hog or not, this answer is missing an explanation. The point is, your snippet performs too many explosions. Please see my limited explosion. – mickmackusa Nov 21 '19 at 10:07
1
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 .

Chirag Pipariya
  • 445
  • 5
  • 13
1
$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));
}
PeeHaa
  • 71,436
  • 58
  • 190
  • 262
-3

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.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136