0

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="#">

WHAT IT SHOULD LOOK LIKE:

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

May need to incorporate get element using DOM as the Full php has a created document. If that is the case I would need to know how to find by class and add the href url that way.

FULL 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(' & ', ' &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>';
    ?>
      </div>
    </div>

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>
    <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>
3
  • DOM is a better route, but your preg_replace fails because you aren't using delimiters. Commented Nov 25, 2018 at 20:41
  • what is add_filter()? Commented Nov 25, 2018 at 21:12
  • @billynoah I believe it’s a Wordpress thing Commented Nov 26, 2018 at 19:00

2 Answers 2

1

You're not very clear about how your question relates to the code shown, but I don't see any attempt to replace the attribute within the DOM code. You'd want to look at XPath to find the desired elements:

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

Output:

<foo><bar><a class="epclean1" href="https://google.com/">test1</a></bar><bar><a class="epclean1" href="https://example.com">test2</a></bar></foo>
Sign up to request clarification or add additional context in comments.

3 Comments

quick question: if I were to want to use an ACR field value($post_url) instead of "google.com" - How would I go about it?
I suppose just pass it to the function as an argument
Thanks, much appreciated
0

Hmm. I think your pattern and replacement might be your problem.

What you have

                $pattern = 'class="epclean1 href="(.*?)"';
                $replacement = 'class="epclean1 href="google.com"';

Fix

                $pattern = '/class="epclean1" href=".*"/';
                $replacement = 'class="epclean1" href="google.com"';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.