I have a newsletter mailer that sends emails automaticly, however ive found a bug in it. I looked at the newsletter and noticed that colors were messed up and after debugging, i found that it was the link replace function that is causing this, somehow the str_replace is replacing correctly, but it also changes the hashtag of the color like this with a link, and is in places where does not exist a href link:
style="font-size: 8pt; color: https://etcetc.com"
It's an odd situation, as it is replacing all links correctly, but however seems like the str_replace replaces all color:; values with the links aswell, and it was not supposed.
Ive tried putting old link and new link into an array to see if there were any of thoose colors in there, however in the array the links appear correctly with old one and new one to replace.
Ive tried instead of replacing in the foreach, passing the old link and new link to a new array and parsing from there but same outcome.
// Trocar links para contabilitizar | Change links to parser
$html = $corpo; // This contains the HTML of the newsletter
//Create a new DOM document
$dom = new DOMDocument;
//Parse the HTML. The @ is used to suppress any parsing errors
//that will be thrown if the $html string isn't valid XHTML.
@$dom->loadHTML($html);
//Get all links. You could also use any other tag name here,
//like 'img' or 'table', to extract other tags.
$links = $dom->getElementsByTagName('a');
//Iterate over the extracted links and display their URLs
foreach($links as $link) {
$old_link = $link->getAttribute('href');
$new_link = $this->url."/scripts/link.php?link=".$link->getAttribute('href')."&campaign_id=".$cid;
$html = str_replace($old_link, $new_link, $html);
}
$corpo = $html;
print_r($corpo);
Example of the newsletter HTML, original : https://codepen.io/gx19/pen/XwdaMN I removed image links for security reasons
Here is what this function is doing to the html: https://codepen.io/gx19/pen/byproX
FIX: (A huge thanks to @LucaRainone ) I had to check if the old url was actually an URL otherwise it would replace color hashtag's.
//Iterate over the extracted links and display their URLs
foreach ($links as $link){
$old_link = $link->getAttribute('href');
$new_link = $this->url."/scripts/link.php?link=".$link->getAttribute('href')."&campaign_id=".$cid;
if (strpos($old_link, 'https://') !== false OR strpos($old_link, 'http://') !== false) {
$html = str_replace($old_link,$new_link,$html);
}
}