Michael Ecklund
Michael Ecklund

Reputation: 1256

How to search and replace matched text with PHP unless found within an HTML tag?

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.

Upvotes: 2

Views: 268

Answers (1)

designbypete
designbypete

Reputation: 169

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)

function replace_text_wps($text){
    $replace = array(
        // used mid-line
        ' YOUR_TEXT ' => ' <span class="YOUR_CLASS">YOUR_TEXT</span> ',
        ' YOUR_TEXT ' => ' <span class="YOUR_CLASS">YOUR_TEXT</span> ',
        ' YOUR_TEXT ' => ' <span class="YOUR_CLASS">YOUR_TEXT</span> ',
        // used at end of lines
        ' YOUR_TEXT' => ' <span class="YOUR_CLASS">YOUR_TEXT</span>',
        ' YOUR_TEXT' => ' <span class="YOUR_CLASS">YOUR_TEXT</span>',
        ' YOUR_TEXT' => ' <span class="YOUR_CLASS">YOUR_TEXT</span>',
        // used inside html tags like headers
        '>YOUR_TEXT' => '><span class="YOUR_CLASS">YOUR_TEXT</span>',
        '>YOUR_TEXT' => '><span class="YOUR_CLASS">YOUR_TEXT</span>',
        '>YOUR_TEXT' => '><span class="YOUR_CLASS">YOUR_TEXT</span>',
        //used directly after html tags
        '> YOUR_TEXT' => '> <span class="YOUR_CLASS">YOUR_TEXT</span>',
        '> YOUR_TEXT' => '> <span class="YOUR_CLASS">YOUR_TEXT</span>',
        '> YOUR_TEXT' => '> <span class="YOUR_CLASS">YOUR_TEXT</span>',
        //exclude alt tags on images, title attributes etc
        '"YOUR_TEXT' => '"YOUR_TEXT',
        'YOUR_TEXT"' => 'YOUR_TEXT"',
        '"YOUR_TEXT' => '"YOUR_TEXT',
        'YOUR_TEXT"' => 'YOUR_TEXT"'
    );
    $text = str_replace(array_keys($replace), $replace, $text);
    return $text;
}
add_filter('the_content', 'replace_text_wps');
add_filter('the_excerpt', 'replace_text_wps');
add_filter('the_title', 'replace_text_wps');

exclude html attributes in str_replace

Upvotes: 2

Related Questions