phphtmlspecialchars

How to use htmlspecialchars only on <code></code> tags.


I'm using WordPress and would like to create a function that applies the PHP function htmlspecialchars only to code contained between <code></code> tags. I appreciate this may be fairly simple but I'm new to PHP and can't find any references on how to do this.

So far I have the following:

function FilterCodeOnSave( $content, $post_id ) {
    return htmlspecialchars($content, ENT_NOQUOTES);
}

Obviously the above is very simple and performs htmlspecialchars on the entire content of my page. I need to limit the function to only apply to the HTML between code tags (there may be multiple code tags on each page).

Any help would be appreciated.

Thanks, James


Solution

  • EDIT: updated to avoid multiple CODE tags

    Try this:

    <?php
    
    // test data
    $textToScan = "Hi <code>test12</code><br>
        Line 2 <code><br>
        Test <b>Bold</b><br></code><br>
        Test
        ";
    
    // debug:
    echo $textToScan . "<hr>";
    
    
    // the regex pattern (case insensitive & multiline
    $search = "~<code>(.*?)</code>~is";
    
    // first look for all CODE tags and their content
    preg_match_all($search, $textToScan, $matches);
    //print_r($matches);
    
    // now replace all the CODE tags and their content with a htmlspecialchars() content
    foreach($matches[1] as $match){
        $replace = htmlspecialchars($match);
        // now replace the previously found CODE block
        $textToScan = str_replace($match, $replace, $textToScan);
    }
    
    // output result
    echo $textToScan;
    
    ?>