Re: Split string

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




On Wed, May 2, 2007 4:10 pm, Stut wrote:
> Richard Lynch wrote:
>> On Wed, May 2, 2007 4:55 am, Lester Caine wrote:
>>> 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, ",");
>>>>
>>>> $part1 = substr($myString, 0, $pos);
>>>> $part2 = substr($myString, $pos);
>>> I'm glad I asked now, that is a lot nicer than trying to get the
>>> preg_split
>>> thing working :)
>>
>> Hnmmm.  Okay, I'll take a shot at it:
>>
>> $answer = preg_split('|[, ]|', $myString);
>> var_dump($answer);
>
> Will give more than 2 parts for strings containing both or multiples.

Yeah, I definitely missed the limit 2 requirement when I read through
the OP.

Though I don't think the 2 bit was the tricky part of the PREG... :-)

> Now please irradiate your hands.

I think they had that done to them way back when, and that's why I
post so much...  You want me to do it again?

:-)

-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux