php正则表达式匹配html标签之外 [英] php regex to match outside of html tags

查看:39
本文介绍了php正则表达式匹配html标签之外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 html 页面上制作 preg_replace.我的模式旨在为 html 中的某些单词添加周围的标签.但是,有时我的正则表达式会修改 html 标签.例如,当我尝试替换此文本时:

I am making a preg_replace on html page. My pattern is aimed to add surrounding tag to some words in html. However, sometimes my regular expression modifies html tags. For example, when I try to replace this text:

<a href="example.com" alt="yasar home page">yasar</a>

这样 yasar 读取 <span class="selected-word">yasar</span> ,我的正则表达式也替换了锚的 alt 属性中的 yasar标签.我使用的当前 preg_replace() 看起来像这样:

So that yasar reads <span class="selected-word">yasar</span> , my regular expression also replaces yasar in alt attribute of anchor tag. Current preg_replace() I am using looks like this:

preg_replace("/(asf|gfd|oyws)/", '<span class=something>${1}</span>',$target);

如何制作正则表达式,使其不匹配 html 标签内的任何内容?

How can I make a regular expression, so that it doesn't match anything inside a html tag?

推荐答案

您可以为此使用断言,因为您只需要确保搜索的词出现在 > 之后的某个时间,或者在任何 < 之前.后一个测试更容易完成,因为前瞻断言可以是可变长度的:

You can use an assertion for that, as you just have to ensure that the searched words occur somewhen after an >, or before any <. The latter test is easier to accomplish as lookahead assertions can be variable length:

/(asf|foo|barr)(?=[^>]*(<|$))/

另见 http://www.regular-expressions.info/lookaround.html 以获得很好的解释断言语法.

See also http://www.regular-expressions.info/lookaround.html for a nice explanation of that assertion syntax.

这篇关于php正则表达式匹配html标签之外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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