1

I need help with a regular expression in PHP. I have one string containing a lot of data and the format could be like this.

key=value,e4354ahj\,=awet3,asdfa\=asdfa=23f23

So I have 2 delimiters , and = where , is the set of key and value. The thing is that key and value can contain the same symbols , and = but they will always be escaped. So I cant use explode. I need to use preg_split but I am no good at regular expressions.

Could someone give me a hand with this one?

2 Answers 2

4

You need to use negative lookbehind:

// 4 backslashes because they are in a PHP string, so PHP translates them to \\
// and then the regex engine translates the \\ to a literal \
$keyValuePairs = preg_split('/(?<!\\\\),/', $input);

This will split on every , that is not escaped, so you get key-value pairs. You can do the same for each pair to separate the key and value:

list($key, $value) = preg_split('/(?<!\\\\)=/', $pair);

See it in action.

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

Comments

1

@Jon's answer is awesome. I though of providing a solution by matching the string:

preg_match_all('#(.*?)(?<!\\\\)=(.*?)(?:(?<!\\\\),|$)#', $string, $m);
// You'll find the keys in $m[1] and the values in $m[2]
$array = array_combine($m[1], $m[2]);
print_r($array);

Output:

Array
(
    [key] => value
    [e4354ahj\,] => awet3
    [asdfa\=asdfa] => 23f23
)

Explanation:

  • (.*?)(?<!\\\\)= : match anything and group it until = not preceded by \
  • (.*?)(?:(?<!\\\\),|$) : match anything and group it until , not preceded by \ or end of line.

2 Comments

This works fine and is convenient, nice touch. The only drawback is that code like this must be accompanied by generous amounts of comments.
@Jon I was writing a preg_split solution but you were faster :-)

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.