preg_replace 只替换外部标签?(...我们不是在谈论完整的“html 解析”,只是一些 markdown)

ajo*_*ajo 6 html php markup markdown preg-replace

应用突出显示某些文本(不包括偶尔标记“<...>”中的文本)的最简单方法是什么?

澄清:我希望保留现有标签!

$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>");
Run Code Online (Sandbox Code Playgroud)

应该显示为:

“除了使用一些简化的Markdown规则生成的一些 html 标签之外,这本质上是纯文本:请参见此处

...但不要弄乱锚标记内的文本(即<a href=markdown.html>)。

我听说过不使用正则表达式解析 html 的论点,但这里我们本质上讨论的是纯文本,除了一些 Markdown 代码的最小解析之外。

ajo*_*ajo 4

实际上,这似乎工作正常:

<?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>"

?>
Run Code Online (Sandbox Code Playgroud)

像这样的字符串$item="odd|string"会导致一些问题,但无论如何我都不会使用那种字符串...(可能需要 htmlentities(...) 或类似的...)