PDA

View Full Version : Help with regular expression



keweli
22 Oct 2011, 04:37 AM
I am using the split() function and I need a regular expression I can't quite remember how to do.

I want to check if there is a space, a tab, OR space AND tab. One of the following.

The following treats it as 2 different things.
$array = split ("[ \t]", $line);

So if the line = Hello(space)(tab)World

It will return 3 array nodes which are "Hello", "", World

I want it to only return "Hello", "World" no matter if the line is:

line = Hello(space)World
or
line = Hello(tab)World
or
line = Hello(space)(tab)World

Any ideas? thanks

hyperion
02 Nov 2011, 12:10 AM
function splitBySpace($text){
return explode(' ', preg_replace('{\s+}', ' ', $text));
}

It works for any number of spaces, tabs and even new lines.