Created
September 19, 2016 09:47
-
-
Save martinsanne/5f0482bece7fe7f4c7001f526ecd4c5f to your computer and use it in GitHub Desktop.
Regex replace text outside brackets only
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$text = 'I love eating CO2 for breakfast. <a href="http://www.co2health.com" class="co2" id="co2">CO2 health effects</a>. Here is a pic of CO2 <img src="http://site.com/co2.jpg" alt="CO2">'; | |
$text = preg_replace('~<[^>]*>(*SKIP)(*F)|CO2~', 'CO<sub>2</sub>', $text); | |
echo $text; | |
/* | |
Output: | |
I love eating CO<sub>2</sub> for breakfast. <a href="http://www.co2health.com" class="co2" id="co2">CO<sub>2</sub> health effects</a>. Here is a pic of CO<sub>2</sub> <img src="http://site.com/co2.jpg" alt="CO2"> | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment