preg_replace只有OUTSIDE标签? (...我们没有说全部'HTML解析',只是一点点降价) [英] preg_replace only OUTSIDE tags ? (... we're not talking full 'html parsing', just a bit of markdown)

查看:115
本文介绍了preg_replace只有OUTSIDE标签? (...我们没有说全部'HTML解析',只是一点点降价)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在OCCASIONAL标记< ...>内应用突出显示某些文字的最简单方法是什么?


$ b

CLARIFICATION :我想要现有的标签PRESERVED!

  $ t = 
preg_replace(
/(markdown )/,
strong$ 1< / strong>,
除了用一些
简化的降价规则生成的几个html标签之外,这基本上是纯文本:< a href = markdown.html> [see here]< / a>);

其中应显示为:

除了用一些简化的 markdown 规则生成的几个html标记之外,这基本上是纯文本: see here



...但是不要将锚文本标签内的文本(即< a href = markdown.html> )。



我听说过不用正则表达式解析html的参数,但在这里我们主要谈论纯文本除了对某些markdown代码进行最小限度的解析外。

解决方案

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

 <?php 
$ item =markdown;
$ t =除了几个生成
的html标签以及一些简化的降价规则之外,这基本上是纯文本:< a href = markdown.html> [see here]< / a>;

// _____ 1。应用重点_
$ t = preg_replace(|($ item)|,< strong> $ 1< / strong>,$ t);

//除了一些生成
//带有一些简化的< strong> markdown< / strong>规则的html标记之外,这基本上是纯文本:< a href =
//< strong> markdown< / strong> .html> [see here]< / a>

// _____ 2。如果在打开和关闭标记____内移除重点, ?] + GT)|, $ 1 $ 3 $ 4,$吨);

//这保留了($ 1)之前,($ 4)
//和里面的文本< strong> ..< / strong> ($ 2),但没有标签($ 3)

//除了一些html标签生成
//带有一些简化的< strong>标记< /强大>规则:< a href = markdown.html>
// [请参阅此处]< / a>

?>

类似于 $ item =odd | string会导致一些问题,但我不会使用那种字符串......(可能需要htmlentities(...)或类似的东西......)

What is the easiest way of applying highlighting of some text excluding text within OCCASIONAL tags "<...>"?

CLARIFICATION: I want the existing tags PRESERVED!

$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>");

Which should display as:

"This is essentially plain text apart from a few html tags generated with some simplified markdown rules: see here"

... BUT NOT MESS UP the text inside the anchor tag (i.e. <a href=markdown.html> ).

I've heard the arguments of not parsing html with regular expressions, but here we're talking essentially about plain text except for minimal parsing of some markdown code.

解决方案

Actually, this seems to work ok:

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

?>

A string like $item="odd|string" would cause some problems, but I won't be using that kind of string anyway... (probably needs htmlentities(...) or the like...)

这篇关于preg_replace只有OUTSIDE标签? (...我们没有说全部'HTML解析',只是一点点降价)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆