Having this string
$string = '';
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('/(.+?)['"]]/', $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,
OR
DEMO
Your PHP code would be,
Get the matched group from index 1.
Here is DEMO
OR try with Positive Lookbehind
Here is DEMO
Sample code:
Whatever you are trying here looks weird. You don’t need regular expressions to get the shortcode params. Instead use the default, built-in function from WordPress.
Example from codex.wordpress.org:
See: WordPress Codex – The Shortcode API
Update – get the first ID: