I have a string like WordPress shortcodes and need to extract the attributes. :
[prefix action="foo" class="bar"][/prefix]
I need to keep exactly this structure that means I can't i.e. remove quotes to become action=foo
right now i used WordPress regex patterns:
// Grabs [prefix foo="bar"][/prefix] from the $content
$pattern = '/\[(\[?)(' . PREFIX . ')(?![\w-])([^\]\/]*(?:\/(?!\])[^\]\/]*)*?)(?:(\/)\]|\](?:([^\[]*+(?:\[(?!\/\2\])[^\[]*+)*+)\[\/\2\])?)(\]?)/';
preg_match_all($pattern, $content, $matches, PREG_SET_ORDER);
this is the result:
Array
(
[0] => [prefix action="foo" class="bar"][/prefix]
[1] =>
[2] => prefix
[3] => action="foo" class="bar"
[4] =>
[5] =>
[6] =>
)
Then I Loop through regex matches
foreach( $matches as $block )
{
/*
* I'm not working on WordPress platform,
* But I used function shortcode_parse_atts
*/
$attr = shortcode_parse_atts( $block[3] );
results:
Array
(
[action] => "foo"
[class] => "bar"
)
and in the end extract($attr)
. but as you can see $class
and $action
values are between double quotes. so i need to get ride of those quotes.
In short:
I want $class = "bar"
becomes $class = bar
In case you think why i don't use str_replace
or preg_replace
, I did but it did not removed quotes somehow ( which i don't understand why )