Reputation: 12176
I'm writing a parser, trying to automate a way that I can pass any argument as a param like follows:
$content = '{loop for=products showPagination="true" paginationPosition="both" wrapLoop="true" returnDefaultNoResults="true" noResultsHeading="Nothing Found" noResultsHeadingSize="2" noResultsParagraph="We have not found any products in this category, please try another."}{/loop}';
preg_match_all('/([a-zA-Z]+)=([\/\.\"a-zA-Z0-9&;,_-]+)/', str_replace('"', '"', $content), $attr);
if (!is_array($attr)) return array();
for ($z = 0; $z < count($attr[1]); $z++) if (isset($attr['1'][$z])) $attrs[$attr['1'][$z]] = trim($attr['2'][$z], '"');
echo json_encode($attrs);
My Isssue is that my loop & regex is splitting out whitespace and I can't figure out how to alter it so that it doesn't.
I've tried adding \w into the right hand side of the = sign, but no luck.
RESULT
{"for":"products","showPagination":"true","paginationPosition":"both","wrapLoop":"true","returnDefaultNoResults":"true","noResultsHeading":"Nothing","noResultsHeadingSize":"2","noResultsParagraph":"We"}
You'll notice that the last two params both stop after the first word.
Upvotes: 1
Views: 40
Reputation: 174776
I suggest you to change the preg_match_all
function like below.
preg_match_all('/([a-zA-Z]+)=("[^"]*"|\S+)/', str_replace('"', '"', $content), $attr);
It will greedily matches all the double quoted contents first. If there isn't any double quotes block, then it will match one or more non-space characters.
Output:
{"for":"products","showPagination":"true","paginationPosition":"both","wrapLoop":"true","returnDefaultNoResults":"true","noResultsHeading":"Nothing Found","noResultsHeadingSize":"2","noResultsParagraph":"We have not found any products in this category, please try another."}
Upvotes: 1