preg_replace只在外部标记? (…我们不是在说完整的“ html解析”,只是降价了一点))>

问题描述 投票:3回答:6

在OCCASIONAL标记“ <...>”中,对某些文本进行高亮显示的最简单方法是什么?

CLARIFICATION

:我希望保留现有标签!
$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>");

哪个显示为:

“这本质上是纯文本,除了一些用某些简化的[[markdown

规则生成的html标签之外:see here...但是不要在锚标记(即<a href=markdown.html>)中添加文本。

我已经听说过不使用正则表达式解析html的论点,但是这里我们实质上是在谈论纯文本,除了对某些markdown代码进行最少的解析。

[在OCCASIONAL标签“

”内的文本以外的某些文本上加亮显示的最简单方法是什么?澄清:我希望保留现有标签! $ t = preg_replace(“ /(markdown)/” ......>

php html preg-replace markdown markup
6个回答
3
投票
实际上,这似乎可以正常工作:

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


1
投票
您可以使用preg_split将字符串分成

tag


0
投票
您可以使用Highlight keywords in a paragraph在每个''处将字符串拆分成一个数组,然后遍历该数组并仅替换不以'>'开头的条目。然后,您可以使用Regex / DOMDocument - match and replace text not in a link将数组合并为字符串。

0
投票
此正则表达式应删除所有HTML开头和结尾标记:preg_split()

0
投票
实际上这不是很有效,但是对我有用

0
投票
首先在标签后替换任何字符串,但强制您的字符串在标签后:
© www.soinside.com 2019 - 2024. All rights reserved.