我想只对<code>中要剥离的内容</ code>中的内容运行htmlentities()
我写了一个函数,它接受一个字符串并在<code> </ code>之间找到内容
function parse_code($string) {
// test the string and find contents with <code></code>
preg_match('@<code>(.*?)</code>@s', $string, $matches);
// return the match if it succeeded
if($matches) {
return $matches[1];
}else {
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
function parse_code($string) {
<div class="myclass" rel="stuff"> things in here </div>
<code> only run htmlentites() here so strip out things like < > " ' & </code>
<div> things in here </div>
但是我需要一些实际运行htmlentities()的函数的帮助; 在<code> </ code>中的内容然后implode()将它们全部重新组合在一起.例如,假设我们在下面有字符串.
// test the string and find contents with <code></code>
preg_match('@<code>(.*?)</code>@s', $string, $matches);
// return the match if it succeeded
if($matches) {
return $matches[1];
}else {
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
再一次,该函数需要保持字符串的所有内容相同,但只修改并运行<code> </ code>内容的htmlentities()
您可以使用自定义回调函数简化此操作:
$html = preg_replace_callback(
"@ (?<= <code>) .*? (?= </code>) @six",
"htmlentities_cb", $html
);
function htmlentities_cb($matches) {
return htmlentities($matches[0], ENT_QUOTES, "UTF-8");
}
Run Code Online (Sandbox Code Playgroud)
匹配封闭代码标记的语法称为lookbehind和lookahead assertion.它简化了回调并且稍后避免了implode(),因为断言匹配本身不会成为$ matches [0]的一部分.虽然@six用于case-insensitve标记匹配,但允许正则表达式中的空格使其更具可读性.