I am trying to extract the text between two strings with double quotes and square brackets. [gallery ids="
and "]
The format I have is:
[gallery ids="55,57,56,58,59"]
Which I would like to turn in to
55,57,56,58,59
I have tried every variety of pattern I've come across but have had no luck. Can anyone tell me what pattern would achieve this using PHP's regex functions?
That should work for you:
$string = '[gallery ids="55,57,56,58,59"]';
if (preg_match('/\[gallery\sids="([^"]+)"\]/', $string, $m)) {
echo $m[1];
}
or if you want to match more than one string like that in the text, then just use preg_match_all
:
if (preg_match_all('/\[gallery\sids="([^"]+)"\]/', $string, $m)) {
print_r($m[1]);
}