php regex to match outside of html tags -


i making preg_replace on html page. pattern aimed add surrounding tag words in html. however, regular expression modifies html tags. example, when try replace text:

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

so yasar reads <span class="selected-word">yasar</span> , regular expression replaces yasar in alt attribute of anchor tag. current preg_replace() using looks this:

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

how can make regular expression, doesn't match inside html tag?

you can use assertion that, have ensure searched words occur somewhen after >, or before <. latter test easier accomplish lookahead assertions can variable length:

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

see http://www.regular-expressions.info/lookaround.html nice explanation of assertion syntax.


Comments

Popular posts from this blog

python - Selenium remoteWebDriver (& SauceLabs) Firefox moseMoveTo action exception -

html - How to custom Bootstrap grid height? -

Ansible warning on jinja2 braces on when -