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