PHP字符串搜索和替换-可能需要使用DOM
我似乎不知道如何实现我的目标 我想根据生成的RSS提要查找并替换一个特定的类链接(无论有什么链接,都需要稍后替换的选项) HTML示例:PHP字符串搜索和替换-可能需要使用DOM,php,html,Php,Html,我似乎不知道如何实现我的目标 我想根据生成的RSS提要查找并替换一个特定的类链接(无论有什么链接,都需要稍后替换的选项) HTML示例: <a class="epclean1" href="#"> ”; 回音“”; } 回音“”;//关闭最后一个UL 回声'; ?> 上面的fullphp在站点上显示如下(这是因为有200+而缩短): × 选择希望获得清晰版本的剧集的第一个字母: 8. A. 嗯。我想你的模式和替换可能是你的问题 你有什么
<a class="epclean1" href="#">
”;
回音“”;
}
回音“”;//关闭最后一个UL
回声';
?>
上面的fullphp在站点上显示如下(这是因为有200+而缩短):
×
选择希望获得清晰版本的剧集的第一个字母:
8.
A.
嗯。我想你的模式和替换可能是你的问题
你有什么
$pattern = 'class="epclean1 href="(.*?)"';
$replacement = 'class="epclean1 href="google.com"';
修理
您不太清楚您的问题与所示代码的关系,但我没有看到任何试图在DOM代码中替换该属性的尝试。您需要查看XPath以找到所需的元素:
function change_clean($content) {
$dom = new DomDocument;
$dom->loadXML($content);
$xpath = new DomXpath($dom);
$nodes = $xpath->query("//a[@class='epclean1']");
foreach ($nodes as $node) {
if ($node->getAttribute("href") === "#") {
$node->setAttribute("href", "https://google.com/");
}
}
return $dom->saveXML();
}
$xml = '<?xml version="1.0"?><foo><bar><a class="epclean1" href="#">test1</a></bar><bar><a class="epclean1" href="https://example.com">test2</a></bar></foo>';
echo change_clean($xml);
function change\u clean($content){
$dom=新的DomDocument;
$dom->loadXML($content);
$xpath=newdomxpath($dom);
$nodes=$xpath->query(“//a[@class='epclean1']”);
foreach($node作为$node){
如果($node->getAttribute(“href”)==“#”){
$node->setAttribute(“href”https://google.com/");
}
}
返回$dom->saveXML();
}
$xml='';
echo change_clean($xml);
输出:
<foo><bar><a class="epclean1" href="https://google.com/">test1</a></bar><bar><a class="epclean1" href="https://example.com">test2</a></bar></foo>
DOM是一个更好的方法,但是您的preg\u replace
失败,因为您没有使用分隔符。什么是add\u filter()
?@billynoah我相信这是一个Wordpress thingquick问题:如果我想使用ACR字段值($post\u url)而不是“”-我该怎么做?我想把它作为一个论据传递给函数谢谢,非常感谢
$pattern = 'class="epclean1 href="(.*?)"';
$replacement = 'class="epclean1 href="google.com"';
$pattern = '/class="epclean1" href=".*"/';
$replacement = 'class="epclean1" href="google.com"';
function change_clean($content) {
$dom = new DomDocument;
$dom->loadXML($content);
$xpath = new DomXpath($dom);
$nodes = $xpath->query("//a[@class='epclean1']");
foreach ($nodes as $node) {
if ($node->getAttribute("href") === "#") {
$node->setAttribute("href", "https://google.com/");
}
}
return $dom->saveXML();
}
$xml = '<?xml version="1.0"?><foo><bar><a class="epclean1" href="#">test1</a></bar><bar><a class="epclean1" href="https://example.com">test2</a></bar></foo>';
echo change_clean($xml);
<foo><bar><a class="epclean1" href="https://google.com/">test1</a></bar><bar><a class="epclean1" href="https://example.com">test2</a></bar></foo>