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

查看:261
本文介绍了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天全站免登陆