PDA

View Full Version : String Splitters



Sliker_Hawk
February 18th, 2006, 09:06 AM
A new movie...
Uh, anyway, say I have a string like this:

$num = "Hello 2890. How are you?";

I want that to be split up so that the number is in a seperate string, as such:

$num_num = "2890";

Now, I havn't tried this, but if it's right, just tell me and I'll be ashamed for not trying it first; could I do that with a function like this:

$num_num = preg_split("(.?)*[0-9]+(.?)*", $num);

What confuses me about that is that both the '(.?)*'s would be saved into the string aswell. I think... Anyway, can anyone enlighten me?

edit: or preg_split(".?*([0-9])+.?*", $num);
That way the '.?*'s wouldn't be counted as an output thingy... I'll shut up. I havn't done this for a while, so I'm a bit rusty...

Sliker_Hawk
February 19th, 2006, 08:36 AM
So I worked out a way of doing it, but it's slightly more complex than I'd like:

$date_year = explode(" ", $date);
$date_split = explode(",", $date);
$date_split = explode(" ", $date_split[0]);

That seperates a "February 15, 2006" so that date_year[2] is 2006, date_split[1] is 15 and date_split[0] is February.

There has to be a better way of doing it...

grappaFruit
February 23rd, 2006, 09:07 AM
A simple solution (on the fly):
$date='February 23, 2006';
$date=explode(' ',str_replace(',','',$date));

echo $date[0]; // echoes February
echo $date[1]; //echoes 23
echo $date[2]; //echoes 2006