I’m trying to find all shortcodes within a string which looks like this:
[a_col] One
[/a_col]
outside
[b_col]
Two
[/b_col] [c_col] Three [/c_col]
I need the content (eg “Three”) and the letter from the col (a, b or c)
Here’s the expression I’m using
preg_match_all('#\[(a|b|c)_col\](.*)\[\/\1_col\]#m', $string, $hits);
but $hits contains only the last one.
The content can have any character even “[” or “]”
EDIT:
I would like to get “outside” as well which can be any string (except these cols). How can I handle that or should I parse this in a second step?
This will capture anything in the content, as well as attributes, and will allow any characters in the content.
EDIT:
You may want to then trim the matches, since it appears there may be some whitespace. Alternatively, you can use regex for removing the whitespace in the content:
OUTPUT:
It might also be helpful to use this for capturing the attribute names and values stored in
$matches[2]. Consider$attsto be the first element in$matches[2]. Of course, would iterate over the array of attributes and perform this on each.This gives an array where the names are stored in
$att_matches[1]and their corresponding values are stored in$att_matches[3].