Re: Split string

From: Date: Wed, 02 May 2007 10:05:52 +0000
Subject: Re: Split string
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-254222@lists.php.net to get a copy of this message
Fredrik Thunberg wrote:
Lester Caine skrev:
Can someone with a few more working grey cells prompt me with the correct command to split a string. The entered data is names, but I need to split the text up to the first space or comma into one string, and the rest of the string into a second. It's the 'first either space or comma' that eludes me at the moment :( In know it's probably obvious, but it always is when you know the answer.
$myString = "John Doe"; $pos = strpos($myString, " ") < strpos($myString, ",") ? strpos($myString, " ") : strpos($myString, ",");
Suggest the following as being more efficient (half the strpos calls)... $pos = min(strpos($myString, " "), strpos($myString, ",")); Alternatively you could use split to break the string into the two parts, which is probably more efficient... list($part1, $part2) = split('[ ,]', $myString); -Stut

« previous php.general (#254222) next »