I've got string like
$content = "Some content some content
[images ids="10,11,20,30,40"]
Some content";
I want to remove [images ids="10,11,20,30,40"] part from it and get ids part as php array(10,11,20,30,40)
Is it possible with regex?
I've got string like
$content = "Some content some content
[images ids="10,11,20,30,40"]
Some content";
I want to remove [images ids="10,11,20,30,40"] part from it and get ids part as php array(10,11,20,30,40)
Is it possible with regex?
You can use this:
$txt = <<<LOD
Some content some content
[images ids="10,11,20,30,40"]
Some content
LOD;
$result = array();
$txt = preg_replace_callback('~(?:\[images ids="|\G(?!^))([0-9]+)(?:,|"])~',
function ($m) use (&$result) { $result[] = $m[1]; }, $txt);
echo $txt . '<br>' . print_r($result, true);
The \G anchor can be very useful in this situation since it's a kind of anchor that means "at the start of the string or contiguous to a precedent match".
However, nothing in the pattern checks if there is a last number followed by "]. If you need to do that, you must add a lookahead to the pattern:
~(?:\[images ids="|\G(?!^))([0-9]+)(?=(?:,[0-9]+)*"])(?:,|"])~
\G feature is like a philosopher's stone, you search it for a long time, but it's very powerful.\G in one of your previous answers sometime back. Thanks!