How can I search and replace all occurrences of a word or phrase of a string, unless it’s been located within an HTML tag?
Currently have this:
<?php
public function add_acronyms($content){
if(strpos($content, get_bloginfo('description')) !== false){
$content = str_replace(get_bloginfo('description'), '<acronym title="'.get_bloginfo('name').'">'.get_bloginfo('description').'</acronym>', $content);
if(strpos($content, '="<acronym title="'.get_bloginfo('name').'">'.get_bloginfo('description').'</acronym>') !== false){
$content = str_replace('="<acronym title="'.get_bloginfo('name').'">'.get_bloginfo('description').'</acronym>', get_bloginfo('description'), $content);
}
}
return $content;
}
?>
But that will only work if the word or phrase is found at the start of an HTML attribute.
found this while looking for something similar. my answer isn’t the most elegant way of accomplishing it but it might help you- my question and answer are here- however as stated, there’s almost certainly a better way to achieve this
In my instance, trying to replace a word was breaking some href tags, so i used the following code (this is in WordPress by the way)
exclude html attributes in str_replace