php - PHP 字符串搜索和替换 - 可能使用 DOM 需要
问题描述
我似乎无法弄清楚如何实现我的目标。
我想根据生成的 RSS 提要查找和替换特定的类链接(无论有什么链接,都需要稍后替换的选项)
示例 HTML:
<a class="epclean1" href="#">
它应该是什么样子:
<a class="epclean1" href="google.com">
可能需要使用 DOM 合并 get 元素,因为完整的 php 具有创建的文档。如果是这种情况,我需要知道如何按类查找并以这种方式添加 href url。
完整的 PHP:
<?php
$rss = new DOMDocument();
$feed = array();
$urlArray = array(array('url' => 'https://feeds.megaphone.fm')
);
foreach ($urlArray as $url) {
$rss->load($url['url']);
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue
);
array_push($feed, $item);
}
}
usort( $feed, function ( $a, $b ) {
return strcmp($a['title'], $b['title']);
});
$limit = sizeof($feed);
$previous = null;
$count_firstletters = 0;
for ($x = 0; $x < $limit; $x++) {
$firstLetter = substr($feed[$x]['title'], 0, 1); // Getting the first letter from the Title you're going to print
if($previous !== $firstLetter) { // If the first letter is different from the previous one then output the letter and start the UL
if($count_firstletters != 0) {
echo '</ul>'; // Closing the previously open UL only if it's not the first time
echo '</div>';
}
echo '<button class="glanvillecleancollapsible">'.$firstLetter.'</button>';
echo '<div class="glanvillecleancontent">';
echo '<ul style="list-style-type: none">';
$previous = $firstLetter;
$count_firstletters ++;
}
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
echo '<li>';
echo '<a class="epclean'.$i++.'" href="#" target="_blank">'.$title.'</a>';
echo '</li>';
}
echo '</ul>'; // Close the last UL
echo '</div>';
?>
</div>
</div>
上面的 fullphp 在网站上显示是这样的(因为有 200+,所以缩短了):
<div class="modal-glanvillecleancontent">
<span class="glanvillecleanclose">×</span>
<p id="glanvillecleaninstruct">Select the first letter of the episode that you wish to get clean version for:</p>
<br>
<button class="glanvillecleancollapsible">8</button>
<div class="glanvillecleancontent">
<ul style="list-style-type: none">
<li><a class="epclean1" href="#" target="_blank">80's Video Vixen Tawny Kitaen 044</a></li>
</ul>
</div>
<button class="glanvillecleancollapsible">A</button>
<div class="glanvillecleancontent">
<ul style="list-style-type: none">
<li><a class="epclean2" href="#" target="_blank">Abby Stern</a></li>
<li><a class="epclean3" href="#" target="_blank">Actor Nick Hounslow 104</a></li>
<li><a class="epclean4" href="#" target="_blank">Adam Carolla</a></li>
<li><a class="epclean5" href="#" target="_blank">Adrienne Janic</a></li>
</ul>
</div>
解决方案
您不太清楚您的问题与显示的代码有何关系,但我没有看到任何尝试替换 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);
输出:
<foo><bar><a class="epclean1" href="https://google.com/">test1</a></bar><bar><a class="epclean1" href="https://example.com">test2</a></bar></foo>
推荐阅读
- java - 如何通过 Youtube API v3 YouTube.PlaylistItems.List 请求检索视频的时长?
- sql - 用于检索在特定列中具有所有相同值的所有用户的 SQL 语句
- python - python中的多线程进程使用队列写入文件检查工作是否已完成
- javascript - 如何删除模态中的属性选择选项
- ios - Core Data 回滚到旧模型版本
- amazon-dynamodb - 使用 DynamoDB 模板中的查询操作过滤分页
- javascript - useState 设置方法不改变值 - 反应
- javascript - 在 requestIdleCallback 中使用 requestAnimationFrame
- python - 关于Python中count()方法的问题
- javascript - 为什么比较两个值不起作用?