Re: Split string

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

 



Jim Lucas wrote:

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 :(

This should work for you.

$myString = "Here is my first, attempt!";
$answer = preg_split('|[, ]|', $myString, 2);

Ok having re-read the preg_split manual page and now found a nice crib sheet for 'pattern' I can understand that. Although it is not obvious from the manual what gets returned if there are two comma's or spaces. I would have expected I think that everything after the second match would be lost? As you only asked to return the first 2 strings, but the last string does contain all the remaining characters :) ( There is a note about it in the comments, but it would be nice to include that sort of fine detail in the main explanation.

I've already found the other problem with this simple search selection. Originally I just wanted to do "Jones,A" and the above would work fine. But some customers said 'We are used to "Jones A" in X - can you do that'. Of cause anything is possible in software, hence the original question, except where they have actually typed 'JONES A' as the Surname ;)

--
Lester Caine - G8HFL
-----------------------------
Contact - http://home.lsces.co.uk/lsces/wiki/?page=contact
L.S.Caine Electronic Services - http://home.lsces.co.uk
MEDW - http://home.lsces.co.uk/ModelEngineersDigitalWorkshop/
Firebird Foundation Inc. - http://www.firebirdsql.org/index.php

--
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