这将捕获内容中的任何内容以及属性,并允许内容中的任何字符。
<?php
$input = '[a_col some="thing"] One[/a_col]
[b_col] Two [/b_col]
[c_col] [Three] [/c_col] ';
preg_match_all('#\[(a|b|c)_col([^\[]*)\](.*?)\[\/\1_col\]#msi', $input, $matches);
print_r($matches);
?>
编辑:
您可能需要再修剪一下比赛,因为它似乎可能会有一些空白。另外,您也可以使用正则表达式的内容删除空格:
preg_match_all('#\[(a|b|c)_col([^\[]*)\]\s*(.*?)\s*\[\/\1_col\]#msi', $input, $matches);
OUTPUT:
Array
(
[0] => Array
(
[0] => [a_col some="thing"] One[/a_col]
[1] => [b_col] Two [/b_col]
[2] => [c_col] [Three] [/c_col]
)
[1] => Array
(
[0] => a
[1] => b
[2] => c
)
[2] => Array
(
[0] => some="thing"
[1] =>
[2] =>
)
[3] => Array
(
[0] => One
[1] => Two
[2] => [Three]
)
)
它也可能会有所帮助使用此捕获存储在$matches[2]
属性名称和值。考虑$atts
是$matches[2]
中的第一个元素。当然,将迭代属性数组并在每个属性上执行此操作。
preg_match_all('#([^="\'\s]+)[\t ]*=[\t ]*("|\')(.*?)\2#', $atts, $att_matches);
这给出其中名称存储在$att_matches[1]
和它们相应的值被存储在$att_matches[3]
阵列。
WordPress已经有了一个功能,你不需要重新发明轮子。我敢打赌,它比任何建议的答案都更好,因为wordpress知道短代码是什么,什么不是 - 以及封装代码的处理顺序。保重。 – hakre 2012-02-03 10:45:50
非常真实,我计划尝试重构我的插件以尽可能在短代码中使用构建,但不幸的是,我的插件需要将短代码从帖子中提取出来,然后将其格式化,然后将其重新放入,并且必须发生这种情况按照设定的顺序。希望我找出一种方法,但现在我手动执行。 – 2012-02-03 10:47:52
同意,但这是一个自定义编辑器,我需要这种方式 – Xaver 2012-02-03 10:50:42