Search code examples
phparraysregexpreg-match-alltext-extraction

Isolate values in string which are separated by commas or spaces and may also be wrapped in double quotes


I'm trying to preg_split() a string of tags delimited by commas, double quotes, or spaces.

This is the code I have so far. The idea is to make it as easy as possible for the user to input tags without the use of a javascript solution (which I may go to later).

$tagfield = 'Tag Tag2, Tag3 "Tag" "A Tag"';

$tags = preg_split('/[^(.)^a-zA-Z0-9]+/', $tagfield, NULL, PREG_SPLIT_NO_EMPTY);

The output I'm getting is:

array (
    0 => 'Tag',
    1 => 'Tag2',
    2 => 'Tag3',
    3 => 'Tag',
    4 => 'A',
    5 => 'Tag',
)

My desired output would be:

array (
    0 => 'Tag',
    1 => 'Tag2',
    2 => 'Tag3',
    3 => 'Tag',
    4 => 'A Tag',
)

I'm not quite sure how I should grab a two-word tag.


Solution

  • Why not a preg_match_all?

    preg_match_all('/([a-zA-Z0-9]+)|(?:"([a-zA-Z0-9 ]+)")/i', 'Tag Tag2, Tag3 "Tag" "A Tag"', $result);
    

    Edit:

    I accept the unelegantless part of the solution, however it does not return the double quotes. Here is the code I mentioned before. Please, paste the more elegant one you find :)

    <?php
    preg_match_all('/([a-zA-Z0-9]+)|(?:"([a-zA-Z0-9 ]+)")/i', 'Tag Tag2, Tag3 "Tag" "A Tag"', $result);
    $result=array_filter(array_map('array_filter', $result));
    print_r(array_merge($result[1], $result[2]));
    ?>
    

    Output:

    Array
    (
        [0] => Tag
        [1] => Tag2
        [2] => Tag3
        [3] => Tag
        [4] => A Tag
    )