Php preg_是否仅替换外部标签?(…我们不是在谈论完整的html解析,只是有点降价)
突出显示某些文本(不包括临时标记“”中的文本)的最简单方法是什么 澄清:我希望保留现有标记Php preg_是否仅替换外部标签?(…我们不是在谈论完整的html解析,只是有点降价),php,html,preg-replace,markdown,markup,Php,Html,Preg Replace,Markdown,Markup,突出显示某些文本(不包括临时标记“”中的文本)的最简单方法是什么 澄清:我希望保留现有标记 $t = preg_replace( "/(markdown)/", "<strong>$1</strong>", "This is essentially plain text apart from a few html tags generated with some simplified markdown rules: <a href=markdown.htm
$t =
preg_replace(
"/(markdown)/",
"<strong>$1</strong>",
"This is essentially plain text apart from a few html tags generated with some
simplified markdown rules: <a href=markdown.html>[see here]</a>");
$t=
预更换(
“/(降价)/”,
“$1”,
“这基本上是纯文本,除了一些html标记外,还生成了一些
简化降价规则:);
应显示为:
“这基本上是纯文本,除了使用一些简化的标记规则生成的一些html标记外:”
。。。但不要弄乱锚定标记内的文本(即
)
我听说过不使用正则表达式解析html的论点,但这里我们主要讨论的是纯文本,除了一些标记代码的最小解析。这个正则表达式应该去掉所有html开始和结束标记:
/()+/
您可以将其与preg_replace一起使用,如下所示:
$test = "Hello <strong>World!</strong>";
$regex = "/(<.*?>)+/";
$result = preg_replace($regex,"",$test);
$test=“你好世界!”;
$regex=“/()+/”;
$result=preg_replace($regex,“,$test);
您可以使用preg_split()
在每个“”处将字符串拆分为一个数组,然后在该数组中循环并仅替换不以“>”开头的条目。之后,使用内爆()
将数组合并为字符串实际上这不是很有效,但对我来说很有效
$your_string = '...';
$search = 'markdown';
$left = '<strong>';
$right = '</strong>';
$left_Q = preg_quote($left, '#');
$right_Q = preg_quote($right, '#');
$search_Q = preg_quote($search, '#');
while(preg_match('#(>|^)[^<]*(?<!'.$left_Q.')'.$search_Q.'(?!'.$right_Q.')[^>]*(<|$)#isU', $your_string))
$your_string = preg_replace('#(^[^<]*|>[^<]*)(?<!'.$left_Q.')('.$search_Q.')(?!'.$right_Q.')([^>]*<|[^>]*$)#isU', '${1}'.$left.'${2}'.$right.'${3}', $your_string);
echo $your_string;
$your_string='…';
$search='markdown';
$left=“”;
$right='';
$left_Q=preg_quote($left,#’);
$right_Q=preg_quote($right,#’);
$search_Q=preg_quote($search,#');
while(preg#u match('#(>|^)[^[^[^]*$)#isU','${1}'.$left.${2}.$right.${3}',$your_string);
echo$你的_字符串;
您可以将字符串拆分为标记/不使用以下方式标记零件:
但请注意,这并不是最好的解决方案。您最好使用适当的HTML解析器,如PHP的DOM库。例如,请参见以下相关问题:
<?php
$item="markdown";
$t="This is essentially plain text apart from a few html tags generated
with some simplified markdown rules: <a href=markdown.html>[see here]</a>";
//_____1. apply emphasis_____
$t = preg_replace("|($item)|","<strong>$1</strong>",$t);
// "This is essentially plain text apart from a few html tags generated
// with some simplified <strong>markdown</strong> rules: <a href=
// <strong>markdown</strong>.html>[see here]</a>"
//_____2. remove emphasis if WITHIN opening and closing tag____
$t = preg_replace("|(<[^>]+?)(<strong>($item)</strong>)([^<]+?>)|","$1$3$4",$t);
// this preserves the text before ($1), after ($4)
// and inside <strong>..</strong> ($2), but without the tags ($3)
// "This is essentially plain text apart from a few html tags generated
// with some simplified <strong>markdown</strong> rules: <a href=markdown.html>
// [see here]</a>"
?>
像
$item=“odd | string”
这样的字符串可能会导致一些问题,但我无论如何不会使用这种字符串…(可能需要HtmEntitys(…)或类似的…首先替换标记后的任何字符串,但强制您的字符串位于标记后:
$t=preg\u replace(“|”(>[^我应该说得更清楚:我希望保留现有的标记!(请参见上面的编辑)很抱歉,我感到困惑。你想突出显示html元素之外的所有文本吗?为什么不搜索所有标记并用uniq id替换它们(或在开始处带有*的自动递增数字。然后运行标记向下preg__替换,然后运行str_替换将它们全部放回。在输出中,为什么标记向下突出显示?因为它发生在a
标记的参数中?相关:
for ($i=0, $n=count($parts); $i<$n; $i+=2) {
$parts[$i] = preg_replace("/(markdown)/", "<strong>$1</strong>", $parts[$i]);
}
$str = implode('', $parts);
<?php
$item="markdown";
$t="This is essentially plain text apart from a few html tags generated
with some simplified markdown rules: <a href=markdown.html>[see here]</a>";
//_____1. apply emphasis_____
$t = preg_replace("|($item)|","<strong>$1</strong>",$t);
// "This is essentially plain text apart from a few html tags generated
// with some simplified <strong>markdown</strong> rules: <a href=
// <strong>markdown</strong>.html>[see here]</a>"
//_____2. remove emphasis if WITHIN opening and closing tag____
$t = preg_replace("|(<[^>]+?)(<strong>($item)</strong>)([^<]+?>)|","$1$3$4",$t);
// this preserves the text before ($1), after ($4)
// and inside <strong>..</strong> ($2), but without the tags ($3)
// "This is essentially plain text apart from a few html tags generated
// with some simplified <strong>markdown</strong> rules: <a href=markdown.html>
// [see here]</a>"
?>