Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何仅在链接为Nofollow时替换标记之间的代码_Php_Regex_Preg Replace_Preg Match - Fatal编程技术网

Php 如何仅在链接为Nofollow时替换标记之间的代码

Php 如何仅在链接为Nofollow时替换标记之间的代码,php,regex,preg-replace,preg-match,Php,Regex,Preg Replace,Preg Match,尝试用一些HTML代码替换nofollow文本和图像链接(将nofollow链接转换为CSS/javascript链接),使用preg_match和preg_replace已经有一段时间了,但今天早些时候意识到它在某些情况下失败了 我的PHP基本代码(比这更复杂,我一直在使用粘贴在底部的代码),但这是核心代码,添加过滤器部分是特定于WordPress的: add_filter('the_content', 'st_nofollow_content', 99); function st_nofo

尝试用一些HTML代码替换nofollow文本和图像链接(将nofollow链接转换为CSS/javascript链接),使用preg_match和preg_replace已经有一段时间了,但今天早些时候意识到它在某些情况下失败了

我的PHP基本代码(比这更复杂,我一直在使用粘贴在底部的代码),但这是核心代码,添加过滤器部分是特定于WordPress的:

add_filter('the_content', 'st_nofollow_content', 99);

function st_nofollow_content($content) {
$content = preg_replace('/<a href="(.*?)" rel="nofollow">(.*?)<\/a>/i', '<span class="affst" title="tests" id="$1">$2</span>', $content);
return $content;
}
add_filter('the_content','st_nofollow_content',99);
函数st_nofollow_content($content){
$content=preg_replace('/blah,blah。。。
输出

<span class="affst" title="tests" id="url-one">anchor one</a> blah, blah... <a href="url-two">anchor-two</span>
锚定一个诸如此类,诸如此类……诸如此类,诸如此类……锚定二个
我理解这个问题,但不确定是否有preg_替换解决方案

这段代码就是我一直在使用的代码,并且在大多数情况下都有效,除了有或没有nofollow链接的情况:

if (preg_match('/<a href="(.*?)<\/a>(.*?)<a href="(.*?)" rel="nofollow">(.*?)<\/a>/i', $content)) {
$content = preg_replace('/<a href="(.*?)<\/a>(.*?)<a href="(.*?)" rel="nofollow">(.*?)<\/a>/i', '<a href="$1</a>$2<span class="affst" title="tests" id="$3">$4</span>', $content);
} else {
$content = preg_replace('/<a href="(.*?)" rel="nofollow">(.*?)<\/a>/i', '<span class="affst" title="tests" id="$1">$2</span>', $content);
}

if(预匹配(“/是的,你真的应该用DOMDocument来做这个……这是一个毛茸茸的烂摊子,我个人会投票赞成烧掉它然后重新开始。但是,当我不得不面对这样的事情时,我会选择bash工具,比如
find
sed
——但是,看起来你需要的是比一个巨大的跨文件鳍更精确的外科手术工具d-and-replace…“DomeElement类的对象无法转换为字符串”-DomeElement不是字符串,尝试在其上使用preg_replace是没有意义的。确定要替换的元素后,应创建新的“replacement”元素,并使用DOM方法替换原始元素(
createElement
createTextNode
appendChild
replaceChild
)。
<a href="url-one">anchor one</a> blah, blah... <span class="affst" title="tests" id="url-two">anchor two</span>
if (preg_match('/<a href="(.*?)<\/a>(.*?)<a href="(.*?)" rel="nofollow">(.*?)<\/a>/i', $content)) {
$content = preg_replace('/<a href="(.*?)<\/a>(.*?)<a href="(.*?)" rel="nofollow">(.*?)<\/a>/i', '<a href="$1</a>$2<span class="affst" title="tests" id="$3">$4</span>', $content);
} else {
$content = preg_replace('/<a href="(.*?)" rel="nofollow">(.*?)<\/a>/i', '<span class="affst" title="tests" id="$1">$2</span>', $content);
}
function st_nofollow_content($content){
$dom = new DOMDocument();
$dom->loadHTML( $content );
$dom->preserveWhiteSpace = false;

$alinks = $dom->getElementsByTagName('a');

foreach ($alinks as $alink) {
$rel = $alink->getAttribute('rel');
if( $rel = 'nofollow') {
$alink = preg_replace('/<a href="(.*?)" rel="nofollow">(.*?)<\/a>/i', '<span class="affst" title="tests" id="$1">$2</span>', $alink);
}
}
$content = $dom->saveHTML();

return $content;
}