【发布时间】:2013-04-13 20:35:51
【问题描述】:
下面的函数和它说的差不多。它会在它找到的第二个段落标记之后将一串 html 插入内容中。
我需要稍微修改一下,让它只计算不在其他标签内的段落标签。换句话说,只有顶级段落标签。
用正则表达式有什么办法吗?
function my_html_insert($content){
$InsertAfterParagraph = 2;
if(substr_count(strtolower($content), '</p>') < $InsertAfterParagraph )
{
return $content .= myFunction($my_insert=1);
}
else
{
$replaced_content = preg_replace_callback('#(<p[\s>].*?</p>\n)#s', 'my_p_callback', $content);
}
return $replaced_content;
}
function my_p_callback($matches)
{
static $count = 0;
$ret = $matches[1];
$pCount = get_option('my_p_count');
if (++$count == $pCount){
$ret .= myFunction($my_insert=1);
}
return $ret;
}
【问题讨论】:
-
为什么不解析 HTML?
-
正则表达式似乎更简单/更快(如果可能的话)
-
几乎从来没有这样。正则表达式不足以解析任意 HTML。
-
这就是我不要求它解析的原因。数一数。
-
正确计数意味着正确解析:上面的正则表达式在 HTML cmets 上会很高兴地失败,并且因为缺少
</p>,这是可选的。然而,大多数正则表达式引擎 足够强大,可以匹配任何右递归上下文无关语法(参见 PCRE 或 Perl 中的(?(DEFINE)(?<rule>pattern)))。 正确地做起来既不实用也不容易。这就是为什么使用现成的解析器是解决问题的最佳方法。