Having Issues with search and replacing link


I cant seem to figure out how to achieve my goal.

I want to find and replace a specific class link based off of a generated RSS feed (need the option to replace later no matter what link is there)

Example HTML:
<a class="epclean1" href="#">

<a class="epclean1" href="google.com">


<?php $i = 0;
$rss = new DOMDocument(); 
$feed = array();
$urlArray = array(array('url' => 'https://feeds.megaphone.fm/SHM5907757338')
foreach ($urlArray as $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(' & ', ' &amp; ', $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>';

var coll = document.querySelectorAll(".glanvillecleancollapsible,.glanvillecleancollapsible1");
var i;

for (i = 0; i < coll.length; i++) {
  coll[i].addEventListener("click", function() {
    var glanvillecleancontent = this.nextElementSibling;
    if (glanvillecleancontent.style.display === "block") {
      glanvillecleancontent.style.display = "";
    } else {
      glanvillecleancontent.style.display = "block";
// Get the modal
var glanvillecleanmod = document.getElementById('myglanvillecleanmod');

// Get the button that opens the modal
var glanvillecleanBtn = document.getElementById("myglanvillecleanBtn");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("glanvillecleanclose")[0];

// When the user clicks the button, open the modal 
glanvillecleanBtn.onclick = function() {
    glanvillecleanmod.style.display = "block";

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
    glanvillecleanmod.style.display = "";

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
    if (event.target == glanvillecleanmod) {
        glanvillecleanmod.style.display = "";

The above fullphp shows on site like so (this is shortened as there is 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>

     <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>
<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>


What is this? You combine both the class and the href into one string? I think that is your issue.
$pattern = ‘class=“epclean1” href="(.*?)"’;
$replacement = ‘class=“epclean1” href=“google.com”’;
might work better for you…


I accidentally posted it here like that but originally it was already set up the way you mentioned, I double checked still having issues


Well, I doubt you are showing us all of your code, but, going over the code you did show, there is a lot if issues.
First, the first couple of lines set a variable $i to zero then the next line adds one to it… ???

Next, you create a variable array named $urlArray, but, you make it an array of arrays? Not sure if you need it set up that way as your indexes would be numbered and then the inner array would contain the url’s.

But, even though you are adding in an odd extra array, using $url[‘url’] should work. Just a lot of extra code for one simple array. I would have just used one array of url’s and just do a foreach without the as part.

Then, you go thru all the url’s in a loop and get all the tags labelled ‘item’. Looking at the source code for that one test url, it shows many “items”. So, that appears okay. Then, you get all the titles and load them into the variable $feed. But, you do not save any of the links in the items.

Lastly, you create a PHP function to replace the links. But, you never call it in your code. At least I did not see where you did. The last line “add_filter” is some sort of function call, but, I do not see this defined anywhere. If this is part of a library or platform, like WordPress, then we would need to know where that is defined and where you call the change_clean function. As-is, your code does not make much sense!

How have you debugged it so far? You could alter the change_clean function to check for success or not and if not, just display the input $content so you know what is coming into the function to see why it is failing. Another issue is that if there is one extra space in the incoming data, it would fail. If you look for your ‘class=“someclass” href’ and it has one extra space in it, then your code will fail. Normally, if you are using your dom system, you would just use that to replace the href’s data as it would always be exact instead of using your version!
Not sure if any of this helps, but, hope so…


I didn’t have all details, updated for more clarity


Okay, let’s start over. You posted this URL: https://feeds.megaphone.fm/SHM5907757338
as your test page. I looked at this page and it has no links on it at all.

I can create sample code for you for changing the HREF’s in all the anchors (links) as it is simple to do.
But, this RSS feed does not contain anchors. It does have “enclosure” tags that have URL’s in them, but,
no anchors.

Let’s start with the URL you want to pull info from. If it is the one in this post, what do you need to change
in that page? The URL’s in the enclosure tags? If so, that is very easy to do in just a few lines of code.


Issue Resolved using:

function change_clean($content) {
    $dom = new DomDocument;
    $xpath = new DomXpath($dom); 
    $nodes = $xpath->query('//a[@class="epclean1"]');
    foreach ($nodes as $node) {
        if ($node->getAttribute("href") === "#") {
		$node->setAttribute("href", "google.com");
    return $dom->saveXML();


Well, glad you solved it.

( That was the code I had set up for you but, there were no 's on the URL posted… )