[英]Regex pattern to find all <code></code> blocks even if they have a CSS class
我的WordPress functions.php文件中包含以下代码。 其目的是检查保存时的所有帖子内容,以查看是否存在<code></code>
块,然后对标记内的内容执行htmlspecialchars操作。
// 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;
}
对于<code></code>
块没有类的实例,该代码绝对可以正常工作。 我的问题是,无论是否有CSS类,我都使用<code></code>
标记,并且无论是否具有类,我都需要htmlspecialchars操作应用于所有代码标记。
我需要说些类似“查找<code(在此有或没有任何东西)>”之类的内容,以便搜索字符串将同时找到普通代码标签和具有类的代码标签,例如<code class="language-html"></code>
。
希望这是有道理的。
另外,我知道regex在这里并不是很多人推荐的解决方案,因此,如果您有更好的方法来实现结果,请随时提出建议。
非常感谢,詹姆斯
关于什么 ?
// the regex pattern (case insensitive & multiline)
$search = "~<code.*?>(.*?)</code>~is";
您应该将正则表达式更改为:
$search = "~<code\s[^>]*.(.*?)<\/code>~is";
要么
$search = "~<code\s.*?>(.*?)</code>~is";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.