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

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -