HTML解析器适用于输入过滤,但是对于修改输出,您需要具有简单的正则表达式解决方案的性能.在这种情况下,回调正则表达式将执行:
$html = preg_replace_callback("#<a\s[^>]*href="(http://[^"]+)"[^>]*>#",
"cb_ext_url", $html);
function cb_ext_url($match) {
list ($orig, $url) = $match;
if (strstr($url, "http://localhost/")) {
return $orig;
}
elseif (strstr($orig, "rel=")) {
return $orig;
}
else {
return rtrim($orig, ">") . ' rel="external">';
}
}
Run Code Online (Sandbox Code Playgroud)
您可能需要更细粒度的检查.但这是一般方法.
| 归档时间: |
|
| 查看次数: |
872 次 |
| 最近记录: |