Scooter5150
Scooter5150

Reputation: 167

Split string on commas found immediately after a phone number

Trying to split a string, but I don't want to "remove" what I'm searching for...

The string Looks like this:

MDVB, 94010, (555) 555-5555, KHII, 94015, (555) 555-5555, POONHY, 94010, (555) 555-5555,

I want to split the string after the phone number, but I don't want to remove the number.

Right now, I have this:

preg_split("/\(?  (\d{3})?  \)?  (?(1)  [\-\s] ) \d{3}-\d{4}/x", $string)

But that outputs:

Array
(
    [0] => MDVB, 94010, 
    [1] => KHII, 94015, 
    [2] => POONHY, 94010, 
)

I thought preg_split() was the thing to use. Is there something else I should be using?

Upvotes: 0

Views: 1097

Answers (5)

mickmackusa
mickmackusa

Reputation: 47934

preg_split() is not a reliable tool for validation. If you need validation, use preg_match_all(). The fact that your phone numbers may have varied leading formats is irrelevant to the splitting process -- identify the end of the phone number, use \K to forget previously matched characters, then match the unneeded delimiting comma and optional space. Demo

$string = "MDVB, 94010, (555) 555-5555, KHII, 94015, (555) 555-5555, POONHY, 94010, (555) 555-5555,";

var_export(
    preg_split('/\d{3}-\d{4}\K, ?/', $string, 0, PREG_SPLIT_NO_EMPTY)
);

Output:

array (
  0 => 'MDVB, 94010, (555) 555-5555',
  1 => 'KHII, 94015, (555) 555-5555',
  2 => 'POONHY, 94010, (555) 555-5555',
)

Upvotes: 0

anubhava
anubhava

Reputation: 785286

You should better use preg_match_all like this:

preg_match_all("/(.*?(?:\(? (?:\d{3})? \)? [\-\s] )? \d{3}-\d{4})[^,]*(?:,|$)\s*/x",
               $string, $arr );
print_r($arr[1]);

OUTPUT:

Array
(
    [0] => MDVB, 94010, (555) 555-5555
    [1] => KHII, 94015, (555) 555-5555
    [2] => POONHY, 94010, (555) 555-5555
)

Upvotes: 0

Igor Parra
Igor Parra

Reputation: 10348

$string = "MDVB, 94010, (555) 555-5555, KHII, 94015, (555) 555-5555, POONHY, 94010, (555) 555-5555,";
preg_match_all("/[A-Za-z]+, \d+, \(\d{3}\) \d{3}-\d{4}/", $string, $matches, PREG_SET_ORDER);

results:

var_dump($matches);

array (
  0 => 
  array (
    0 => 'MDVB, 94010, (555) 555-5555',
  ),
  1 => 
  array (
    0 => 'KHII, 94015, (555) 555-5555',
  ),
  2 => 
  array (
    0 => 'POONHY, 94010, (555) 555-5555',
  ),
)

Upvotes: 0

jasonlfunk
jasonlfunk

Reputation: 5249

You should use preg_match_all() to extract portions of your string.

<?php
$string = "MDVB, 94010, (555) 555-5555, KHII, 94015, (555) 555-5555, POONHY, 94010, (555) 555-5555,";

$res = preg_match_all("/([A-Z]+,\s+\d+,\s+\(\d{3}\) \d{3}-\d{4})/",$string,$matches);
print_r($matches);
?>

Outputs:

Array
(
    [0] => Array
        (
            [0] => MDVB, 94010, (555) 555-5555
            [1] => KHII, 94015, (555) 555-5555
            [2] => POONHY, 94010, (555) 555-5555
        )

    [1] => Array
        (
            [0] => MDVB, 94010, (555) 555-5555
            [1] => KHII, 94015, (555) 555-5555
            [2] => POONHY, 94010, (555) 555-5555
        )

)

Upvotes: 2

drew010
drew010

Reputation: 69967

You could use the PREG_SPLIT_DELIM_CAPTURE option which will cause the parenthesized expression in the delimiter pattern to be captured and returned as well.

$parts = preg_split("/\(?  (\d{3})?  \)?  (?(1)  [\-\s] ) \d{3}-\d{4}/x",
                    $string,
                    null,
                    PREG_SPLIT_DELIM_CAPTURE);

Resulting array:

array(7) {
  [0]=>
  string(13) "MDVB, 94010, "
  [1]=>
  string(3) "555"
  [2]=>
  string(15) ", KHII, 94015, "
  [3]=>
  string(3) "555"
  [4]=>
  string(17) ", POONHY, 94010, "
  [5]=>
  string(3) "555"
  [6]=>
  string(1) ","
}

I believe this is the behavior you were looking for.

Upvotes: 0

Related Questions