Having this string
$string = '[gallery link="file" ids="501,502,503,504,505,506,507,508,509"]';
How could I extract the first id in ids?
So far I have succeeded to extract all the ids, then used split;
$output = preg_match_all('/\[gallery.+ids=[\'"](.+?)[\'"]\]/', $string, $matches);
list($extracted) = split(',', $matches[1][0]);
There must be something simpler using only regex, right?
Thanks :)
You could try the below regex to match the first id in id's,
\[gallery.+ids=\"\K[^,]*
OR
\[gallery.+ids=\"\K\d+
Your PHP code would be,
<?php
$string = '[gallery link="file" ids="501,502,503,504,505,506,507,508,509"]';
$pattern = '~\[gallery.+ids="\K([^,]*)~';
if (preg_match($pattern, $string, $m)) {
$yourmatch = $m[0];
echo $yourmatch;
}
?> //=> 501