I am able to split strings in the format key:value;
using the following code:
$inside = "key1:value1;key2:value2;key3:value3;";
preg_match_all("/([^:]+):([^;]+);/s", $inside, $pairs);
What I would like to do is allow for the occurrence of the colon and semi-colon character in the values by introducing an escape character e.g. \;
any colon or semi-colon immediately preceded by a backslash would be ignored.
Bonus points if within the same regex, the escaped characters can then be stored in the array of matches unescaped without having to run everything through str_replace
. Thanks for any help you can offer.
preg_match_all(
'/( # Match and capture...
(?: # either:
\\\\. # an escaped character
| # or:
[^\\\\:] # any character except : or \
)+ # one or more times
) # End of capturing group 1
: # Match a colon
((?:\\\\.|[^\\\\;])+); # Same for 2nd part with semicolons
/x',
$inside, $pairs);
does this. It doesn't remove the backslashes, though. You can't do that in a regex itself; for this, you'd need a callback function.
To match the final element even if it doesn't end with a delimiter change the ;
to (?:;|$)
(same for the :
). And to return empty elements as well change the +
to a *
.
You can do:
$inside = "key\:1:value\;1;key2:value2;key3:value3;";
$pairs = preg_split('/(?<!\\\\);/',$inside,-1,PREG_SPLIT_NO_EMPTY );
foreach($pairs as $pair) {
list($k,$v) = preg_split('/(?<!\\\\):/',$pair);
// $k and $v have the key and value respectively.
}
来源:https://stackoverflow.com/questions/8445634/php-regex-split-on-unescaped-delimiter