Вопрос

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

Это было полезно?

Решение

You should change your regex to this maybe :

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

or

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

Другие советы

What about ?

// the regex pattern (case insensitive & multiline)
$search = "~<code.*?>(.*?)</code>~is";
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top