Regex to Match Outside of HTML Tags for Selective Tagging
To prevent matches within HTML tags while using preg_replace to add tags to specific words in an HTML page, it is crucial to define a regular expression that excludes these areas.
Original Pattern:
preg_replace("/(asf|gfd|oyws)/", '<span>
Weakness:
The pattern above will also match instances of the target words within HTML tags, which is undesirable.
Enhanced Pattern:
/(asf|foo|barr)(?=[^>]*(<|$))/
Breakdown:
How it Works:
This pattern matches the target words only if they are not immediately followed by the closing HTML angle bracket. It effectively restricts the matching to outside of HTML tags, preventing unintentional modifications within them.
Example:
Consider the following HTML:
<p>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:</p> <pre class="brush:php;toolbar:false"><a href="example.com" alt="yasar home page">yasar</a>
Using the enhanced pattern, the target word "yasar" will be matched and tagged, while the instance within the "alt" attribute of the anchor tag will be left untouched:
<p>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:</p> <pre class="brush:php;toolbar:false"><a href="example.com" alt="yasar home page">yasar</a>So that yasar reads
The above is the detailed content of How to Use Regex to Avoid Modifying Text Inside HTML Tags During Replacement?. For more information, please follow other related articles on the PHP Chinese website!