Super Quick PHP Question - Quick Answer!

Zxcs

[H]ard|Gawd
Joined
Sep 11, 2004
Messages
2,007
Ok, I have a string of words, $message.

Lets say $message = "hello I am good today"

I want to cut the first word off of the string, so I can make another string, $othermessage which will be "I am good today"

I've tried splitting $message into words ($word) and then done substr($message,$word[0]) but it doesn't seem to work.

Any ideas? Thanks!
 

carl67lp

Supreme [H]ardness
Joined
Oct 17, 2001
Messages
4,554
My O'Reilly PHP Cookbook suggests using "explode" for this problem.

Code:
$words = explode(' ', 'A space-separated list of words');
$words would then contain {"A", "space-separated", "list", "of", "words"}. Since that's an array, you can do all sorts of fun things with it.
 

lomn75

Purple Ace
Joined
Jun 26, 2000
Messages
6,613
even quicker:
Code:
list($junk, $substr) = explode(' ', 'A space-separated list of words', 2);
Code:
$junk == 'A'
$substr == 'space-separated list of words'
 

tim_m

i'm so nice
Joined
Feb 10, 2003
Messages
5,540
to only chop off the first word and retain the rest (including if for some reason there are multiple spaces between words), i would use preg_split with the limit parameter. explode also has a limit paramater
PHP:
$str = "hello I am good today";
$arr = preg_split('!\s+!', $str, 2);
--or--
$arr = explode(' ', $str, 2);
echo $arr[1]; // should print "I am good today"
i haven't tested this but i think it will work. the preg_split way accounts for the possibility of more than one space being present.
 

Icheb

Limp Gawd
Joined
Dec 8, 2004
Messages
191
PCREs have way too much overhead for this. Just use the aforementioned way of explode() and you're fine.
 

tim_m

i'm so nice
Joined
Feb 10, 2003
Messages
5,540
i'll overhead you!!! :p

or not. this is simply a distinction that while in this case may be ok to ignore, should be mentioned
PHP:
$str = "Hello  there.  How are you   today?";
$arr = explode(' ', $str);
print_r($arr);
/*
outputs
Array
(
    [0] => Hello
    [1] => 
    [2] => there.
    [3] => 
    [4] => How
    [5] => are
    [6] => you
    [7] => 
    [8] => 
    [9] => today?
)
*/

$arr = explode(' ', $str, 2);
print_r($arr);
/*
outputs
Array
(
    [0] => Hello
    [1] =>  there.  How are you   today?
)
*/

$arr = preg_split('!\s+!', $str);
print_r($arr);
/*
outputs
Array
(
    [0] => Hello
    [1] => there.
    [2] => How
    [3] => are
    [4] => you
    [5] => today?
)
*/
 

lomn75

Purple Ace
Joined
Jun 26, 2000
Messages
6,613
tim_m said:
i'll overhead you!!! :p
I thought about mentioning this, then realized the multi-space scenario would work as above.

However, you could always trim() the initial string and the result of explode(). Less overhead for the win! :p
 

SoRnMaN

Gawd
Joined
Jan 22, 2002
Messages
831
Actually, prolly the best way to do this is by this method:

Code:
<?php
$message = "hello I am good today";
$message = substr($message, (strpos(trim($message), " ") + 1));
echo $message;
?>
strpos() and substr() are much faster than explode and other options.
 
Top