1

I have the following string which I need to be bale to extract the parameters from in order to process the next part of the code. I have manage to do this by doing multiple preg_match_all, but it is not very efficient and/or dynamic.

Example strings (the source might contain multiples): <==OBJECTSTART==>type=>sqltable,objectid=>4000001,options=>1|5|2<==OBJECTEND==>

<==OBJECTSTART==>type=>sqltable,objectid=>4000002,options=>3|8|5<==OBJECTEND==>

What I have so far I have go to the following for a regex expression:

/<==OBJECTSTART==>((.?),)(.?)<==OBJECTEND==>/

This gives me the information before the first comma but I have tried the usual + and * to give me a repeat iteration but no luck.

ideally I am looking for an array of objects that looks like the following

[0]=>

[type]=sqltable
[objected]=4000001
[options]=1|5|2

[1]=>

[type]=sqltable
[objected]=4000002
[options]=3|8|5

thanks in advance!

1
  • A solution using regular expressions to parse a string formatted as yours is typically error-prone and not very maintainable. It's often easier to manually parse the string with splits and such. That said, /([^,=>]+)=>([^,<=>]+)/g appears to match the keys and corresponding values correctly. If you use it one line at a time, you should be able to build up your array. Commented Mar 1, 2015 at 23:01

2 Answers 2

1

This may not be the most elegant way of parsing this, but I think it gets the job done:

$str = <<<EOS
<==OBJECTSTART==>type=>sqltable,objectid=>4000001,options=>1|5|2<==OBJECTEND==>
<==OBJECTSTART==>type=>sqltable,objectid=>4000002,options=>3|8|5<==OBJECTEND==>
EOS;

foreach (explode(PHP_EOL, $str) as $line) {
    $line = preg_replace('/<==OBJECTSTART==>(.*)<==OBJECTEND==>/', '\1', $line);
    $pairs = explode(',', $line);

    $data = array();
    foreach ($pairs as $pair) {
        list ($key, $value) = explode('=>', $pair);
        $data[$key] = $value;
    }
    $result[] = $data;
}
print_r($result);

Output:

Array
(
    [0] => Array
        (
            [type] => sqltable
            [objectid] => 4000001
            [options] => 1|5|2
        )

    [1] => Array
        (
            [type] => sqltable
            [objectid] => 4000002
            [options] => 3|8|5
        )

)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks this seem to do the trick and slightly modified for my needs I have created a reusable function that we get the object details process the object and replace the original place holder(s) with the result(s) before outputting the new file.
1

First extract that is between <==OBJECTSTART==> and <==OBJECTEND==>

  $str= substr($str, strlen("<==OBJECTSTART==>"), -1 * strlen("<==OBJECTEND==>"));

Then get all pairs attribute/value

  preg_match_all("#([^\=]*)\=>([^\,<]*)#",str,values);

Finally, combines the keys and the values in a array

  $result= array_combine( $values[1], $values[2] );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.