1

I have the below code in my WordPress functions.php file. Its aim is to check all post content on save to see if <code></code> blocks exist and then perform a htmlspecialchars operation on the content inside the tags.

// Encode htmlspecialchars when saving posts
function FilterCodeOnSave( $content, $post_id ) {

    // test data
    $textToScan = $content;

    // 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
    foreach($matches[1] as $match){
        $replace = htmlspecialchars($match, ENT_NOQUOTES);
        // now replace the previously found CODE block
        $textToScan = str_replace($match, $replace, $textToScan);
    }

    // output result
    return $textToScan;
}

The code works absolutely fine for instances where <code></code> blocks have no class. My problem is that I use <code></code> tags both with and without CSS classes and I need the htmlspecialchars operation to apply to all code tags whether they have a class or not.

I need to say something like "find <code(with or without anything here)>" so that the search string will find both plain code tags and code tags that have a class, for example <code class="language-html"></code>.

Hope this makes sense.

Also, I'm aware regex isn't a recommended solution by many on here so if you have a better way of achieving the outcome then please feel free to suggest.

Many Thanks, James

2 Answers 2

1

You should change your regex to this maybe :

$search = "~<code\s[^>]*.(.*?)<\/code>~is";

or

$search = "~<code\s.*?>(.*?)</code>~is";
Sign up to request clarification or add additional context in comments.

1 Comment

I knew this would be simple... PHP newbie here! Thanks again aelor, I'll mark this as the answer when the time limit expires.
1

What about ?

// the regex pattern (case insensitive & multiline)
$search = "~<code.*?>(.*?)</code>~is";

3 Comments

wont work unless you make that .* lazy like this .*?
I would Maxime but I don't have enough reputation I'm afraid. Thanks again though.
@Maxime he means that .*? works, so better correct your answer before someone downvotes it :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.