Question

I wish to split a string.

  • It comprises of ref no., name, and city

example: 2E4 766 06 7982 647 5 Joesph J Sanchez Zuni

  • 2E4 766 06 7982 647 5 is ref no.
  • Joesph J Sanchez is name
  • Zuni is city

Separating Name and City is difficult but I am trying to separate Ref No and Name(NAme City). I formed a regular expression and tested it on: http://www.switchplane.com/awesome/preg-match-regular-expression-tester/?pattern=%22%5Ba-zA-Z%5D%5Ba-z%5Cs.%5D%22&subject=2E4+766+06+7982+647+5+Joesph+J+Sanchez+Zuni

I formed it by thinking Name will always start in Capital Letters and will be followed by a small alphabet or space or dot

But when I use

$keywords = preg_split("[a-zA-Z][a-z\s.]", $strBreak['cust_ref']);

it doesn't work.

Please guide.

Was it helpful?

Solution

Regex:

'#(?P<ref>.+\d) (?P<name>\w+ [A-Z ]*\w+) (?P<city>.+)#'
  • First capture anything before the name which ends in a single digit. I'm unsure if this is correct because of lack of examples / format for ref no. If this is incorrect remove the space between ".+" and "\d". Store with key 'ref' in array.
  • Capture a name with 0 or more middle names. Store with key 'name' in array.
  • Capture anything after the name as city name. Store with key 'city' in array.

Try this:

$vars = array(
    '2E4 766 06 7982 647 5 Joesph Sanchez Zuni',
    '2E4 766 06 7982 647 5 Joesph J Sanchez Zuni',
    '2E4 766 06 7982 647 5 Joesph J G Sanchez Zuni',
    '2E4 766 06 7982 647 5 Joesph Sanchez Los Angeles',
    '2E4 766 06 7982 647 5 Joesph J Sanchez Los Angeles',
    '2E4 766 06 7982 647 5 Joesph J G Sanchez Los Angeles',
    '2E4 766 06 7982 647 5 Joesph Sanchez St. Morel',
    '2E4 766 06 7982 647 5 Joesph J Sanchez St. Morel',
    '2E4 766 06 7982 647 5 Joesph J G Sanchez St. Morel',
);
$matches = array();

foreach ($vars as $var) {
    if (preg_match('#(?P<ref>.+ \d) (?P<name>\w+ [A-Z ]*\w+) (?P<city>.+)#', $var, $matches)) {
        echo 'Ref: ', $matches['ref'], '. Name: ', $matches['name'], '. City: ', $matches['city'], "\n";
    } else {
        echo "No match for $var\n";
    }
}

Result:

Ref: 2E4 766 06 7982 647 5. Name: Joesph Sanchez. City: Zuni
Ref: 2E4 766 06 7982 647 5. Name: Joesph J Sanchez. City: Zuni
Ref: 2E4 766 06 7982 647 5. Name: Joesph J G Sanchez. City: Zuni
Ref: 2E4 766 06 7982 647 5. Name: Joesph Sanchez. City: Los Angeles
Ref: 2E4 766 06 7982 647 5. Name: Joesph J Sanchez. City: Los Angeles
Ref: 2E4 766 06 7982 647 5. Name: Joesph J G Sanchez. City: Los Angeles
Ref: 2E4 766 06 7982 647 5. Name: Joesph Sanchez. City: St. Morel
Ref: 2E4 766 06 7982 647 5. Name: Joesph J Sanchez. City: St. Morel
Ref: 2E4 766 06 7982 647 5. Name: Joesph J G Sanchez. City: St. Morel
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top