Php 带有wordpress短代码的正则表达式
我正在尝试查找字符串中的所有短代码,如下所示:Php 带有wordpress短代码的正则表达式,php,regex,Php,Regex,我正在尝试查找字符串中的所有短代码,如下所示: [a_col] One [/a_col] outside [b_col] Two [/b_col] [c_col] Three [/c_col] 我需要内容(如“三”)和上校的信(a、b或c) 这是我使用的表达式 preg_match_all('#\[(a|b|c)_col\](.*)\[\/\1_col\]#m', $string, $hits); 但是$hits只包含最后一个 内容可以有任何字符,甚至“[”或“]”
[a_col] One
[/a_col]
outside
[b_col]
Two
[/b_col] [c_col] Three [/c_col]
我需要内容(如“三”)和上校的信(a、b或c)
这是我使用的表达式
preg_match_all('#\[(a|b|c)_col\](.*)\[\/\1_col\]#m', $string, $hits);
但是$hits只包含最后一个
内容可以有任何字符,甚至“[”或“]”
编辑:
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]
)
)
我想得到“外面”以及可以是任何字符串(除了这些科尔)。我该如何处理,或者应该在第二步中解析它?这将捕获内容中的任何内容以及属性,并允许内容中的任何字符
<?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);
?>
输出:
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 <代码>是<代码> > $匹配(2)< /代码>中的第一个元素。当然,将迭代属性数组并对每个属性执行此操作
preg_match_all('#([^="\'\s]+)[\t ]*=[\t ]*("|\')(.*?)\2#', $atts, $att_matches);
这提供了一个数组,其中名称存储在$att_matches[1]
中,其对应值存储在$att_matches[3]
中。使用(.|\n)*)
而不是(.*)
来捕获多行
<?php
$string = "
[a_col] One
[/a_col]
[b_col]
Two
[/b_col] [c_col] Three [/c_col]";
preg_match_all('#\[(a|b|c)_col\]((.|\n)*)\[\/\1_col\]#m', $string, $hits);
echo "<textarea style='width:90%;height:90%;'>";
print_r($hits);
echo "</textarea>";
?>
我在这里没有可以测试的环境,但是您可以使用“向后看”和“向前看”断言以及“向后参考”来匹配内容周围的标记。像这样的
(?<=\[(\w)\]).*(?=\[\/\1\])
(?谢谢,这完全符合我的需要。它也为属性做了准备。我添加了一些关于属性的信息。Hanks man!工作如预期,但我将使用@AramKocharyanYes的解决方案,他的答案非常彻底!甚至可以预先解决未来可能出现的问题。我将使用他的答案:-)Wordpress已经有了这方面的功能,你不需要重新发明轮子。我打赌它比任何建议的答案都有效,因为wordpress知道什么是短代码,什么不是短代码,以及封装的短代码应该按什么顺序处理。注意。非常正确,如果可能的话,我计划尝试重新构造我的插件以使用内置短代码,但不幸的是,我的插件需要从文章中提取短代码,将其格式化,然后将其放回,并且这必须按设定的顺序进行。希望我能找到一种方法,但现在我是手动操作的。同意,但这是为自定义编辑器设计的,我需要这种方法