1

I want to split a string, using either explode or preg_split most likely, to create two arrays. The first is what was split up. The seconds is the delimiters. The only two delimiters would be either "AND" or "OR".

For example:

$string = "Name=John AND State=GA OR State=CA";

I want to capture not only the 'Name=John,State=GA,State=CA' but also the delimiters between each.

For this example, the two seperate arrays would be:

array (
        [0] => Name=John
        [1] => State=GA
        [2] => State=CA   
)

and

array (
        [0] => AND
        [1] => OR
)

From here I can massage the data to comply with what I want, in the end to build a query. If there's a better way of going about this I'm all ears. Thanks for any and all help!

ad2387
  • 551
  • 2
  • 11
  • 21

3 Answers3

1

Use PHP Regular Expression. The preg_match_all() function in your case:

Live Demo

Code:

$input = "Name=John AND State=GA OR State=CA";

preg_match_all("/[a-zA-Z]+\s*=\s*[a-zA-Z]+/", $input, $output_1);
$output_1 = $output_1[0];


preg_match_all("/AND|OR/", $input, $output_2);
$output_2 = $output_2[0];

print_r($output_1);
print_r($output_2);

Output:

Array
(
    [0] => Name=John
    [1] => State=GA
    [2] => State=CA
)
Array
(
    [0] => AND
    [1] => OR
)
rullof
  • 7,124
  • 6
  • 27
  • 36
0

If every other piece in that string is delimiter (AND|OR), there is no need to use regex. Exploding that string to array by space character and then just picking every other item to one array and others to another array. Like this:

<?php
$string = "Name=John AND State=GA OR State=CA";
$a = explode(' ', $string);

$foo = array();
$bar = array();
$len = count($a);

for($i = 0; $i < $len; $i++) {
    if($i % 2 === 0) {
        $foo[] = $a[$i];
    }
    else {
        $bar[] = $a[$i];
    }
}

print_r($foo);
print_r($bar);

https://eval.in/87538

And if the there are always the same amount of items in that string, there is no need to loop through it, just assign items 0, 2 and 4 to one array and 1 and 3 to other array.

Harri
  • 2,692
  • 2
  • 21
  • 25
  • Thanks for the comment. That actually is how I initially coded it before posting (actually did a preg_split on spaces but same concept with the % 2). The problem is that there is the option for there to be spaces between 'Name=John'. Could be 'Name= John', 'Name =John', etc. Thanks for the response though. The above answer is working great so I'll go with it. – ad2387 Jan 09 '14 at 13:10
0

The very real possibility of a state value being Oregon (OR), means that @rullof's answer is not reliable.

By consuming values in a single pass of the input string, the task can be stabilized.

The two groups can be extracted via array_column().

Code: (Demo)

$string = "Name=John AND State=OR OR State=CA";

if (preg_match_all(
        '~([a-zA-Z]+=\S+)(?: ([A-Z]{2,3}))?~',
        $string,
        $out,
        PREG_SET_ORDER
    )
) {
    echo "data = ";
    var_export(array_column($out, 1));
    echo "\nconjunctions = ";
    var_export(array_column($out, 2));
}

Output:

data = array (
  0 => 'Name=John',
  1 => 'State=OR',
  2 => 'State=CA',
)
conjunctions = array (
  0 => 'AND',
  1 => 'OR',
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136