Word Boundary Regular Expression Unless Inside HTML Tag

时光怂恿深爱的人放手 提交于 2019-12-24 10:48:12

问题


I have a regular expression using word boundaries that works exceedingly well...

~\b('.$value.')\b~i

...save for the fact that it matches text inside HTML tags (i.e. title="This is blue!"). It's a problem because I'm doing text substitution on anything the regex matches, then making tooltips appear using those title tags. So, as you can imagine, it's substituting text inside the title and breaking the HTML of the tooltip. For example, what should be:

<span class="blue" title="This is blue!">Aqua</span>

...ends up becoming...

<span class="blue" title="This is <span class=" blue"="">Royal Blue</span>"&gt;Aqua</span>

My use of strip_tags didn't solve the issue; I think what I need is a better regular expression which simply will not match content ending in blue"> ('blue' in this case being placeholder for any other color in the array I'm comparing it against).

Can anyone append what I need to the regular expression? Or do you have a better solution?


回答1:


Regex replaces often seem like the solution but they can have a lot of ill side-effects, and not really accomplish what you want. Look into DOMDocument models instead (as some commenters have suggested).

But if you insist on using regex, here's a good post on SO. It uses two passes to accomplish what you want.




回答2:


Davey, resurrecting this question because apart from the Dom solution, there is a better regex solution than the one mentioned so far. It's a simple solution that requires a single step.

The general solution is

<[^>]*>(*SKIP)(*F)|blue

Here's a demo

Any content within <> tags is simply skipped. Content in between tags, such as blue is matched, which sounds like it fits your needs.

In the expression, replace "blue" for what you like.

Reference

  1. How to match pattern except in situations s1, s2, s3
  2. How to match a pattern unless...


来源:https://stackoverflow.com/questions/17141364/word-boundary-regular-expression-unless-inside-html-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!