1

I have a function to remove href links from HTML i'm loading into an iframe. The html is a full imap HTML body of an email.

I have 2 questions if someone could help.

  1. I'm trying to remove all links. So i have A and AREA href links. Are there any other type of links that Im not thinking of? I'm under the impression that you can't have JS in emails so im not checking for JS. I want to remove ALL links. I don't want the email contents to be clickable.

  2. Within my function how do search for a specific string and remove it if found. Specifically i want to remove email addresses from the html. I was thinking i can include MAILTO in my function, but that won't cover all email addresses.

    function stripLinks($lala){
       $dom = new DOMDocument();
       $htmltags = $lala;
       @$dom->loadHTML($htmltags);
    
       $xpath = new DOMXPath($dom);
       $hrefs = $xpath->evaluate("/html/body//a");
       for ($i = 0; $i < $hrefs->length; $i++) {
            $href = $hrefs->item($i);
            $href->removeAttribute('target');     
            $href->removeAttribute('href');
       }
       $hrefs_area = $xpath->evaluate("/html/body//area");
       for ($i = 0; $i < $hrefs_area->length; $i++) {
            $href = $hrefs_area->item($i);      
            $href->removeAttribute('target');     
            $href->removeAttribute('href');
       }
       $lala=$dom->saveHTML();
    
    return $lala;
    }
    

Would this be the correct way to do it? There are several variations of the emails so is there a wild card i can use?

 str_replace("email@address.com","",$lala);
return $lala;

THANKS IN ADVANCE

SOLUTION FOR #2

$pattern = "/[^@\s]*@[^@\s]*\.[^@\s]*/";
$replacement = "[removed]";
preg_replace($pattern, $replacement, $string);
vitalyp
  • 671
  • 4
  • 12
  • 23
  • thanks. $pattern = "/[^@\s]*@[^@\s]*\.[^@\s]*/"; $replacement = "[removed]"; preg_replace($pattern, $replacement, $string); worked for removing email addresses – vitalyp Dec 15 '15 at 03:15
  • One more question. Is there any other way to make a link in an email other than and ? – vitalyp Dec 15 '15 at 03:17

0 Answers0