Question

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

Was it helpful?

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;

?>

OTHER TIPS

Use DOMDocument to get all <code> tags;

// in this example $dom is an instance of DOMDocument
$code_tags = $dom->getElementsByTagName('code');

if ($code_tags) {
    foreach ($code_tags as $node) {
        // [...]
    }
    // [...]
}

i know this is a little bit late, but you can call the htmlspecialchars function first and then when outputting call the htmlspecialchars_decode function

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top