Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

282
Views
PHP Get unsubscribe URL from email body

I have an email's HTML body. I need to parse just the unsubscribe link from that. So if at any point in the dom there is some kind of link, containing the word Unsubscribe, I would need to return the URL of that specific link. I tried different regex but I can't seem to find just the unsubscribe URL or sometimes at all.

$regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*(?:unsubscribe).*)<\/a>";
preg_match_all("/$regexp/iU", $body, $matches);
var_dump($matches);

This does not work :/

Thanks

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can use DOMXpath and check if the anchor contains a case-insensitive match to unsubscribe and get the URL using getAttribute to get the value of href .

 $data = <<<DATA This is a link <a href="https://stackoverflow.com/">SO</a> and this is <a href="http://test.test">unsubscribe</a> and another and this is <a href="http://test.test">UnSubScribe</a>. DATA; $dom = new DomDocument(); $dom->loadHTML($data); $xpath = new DOMXPath($dom); $query = "//a[contains(translate(., 'UNSUBSCRIBE', 'unsubscribe'),'unsubscribe')]"; $anchors = $xpath->query($query); foreach ($anchors as $a) { echo sprintf("%s: %s" . PHP_EOL, $a->nodeValue, $a->getAttribute("href") ); }

Production

 unsubscribe: http://test.test UnSubScribe: http://test.test

See a PHP demo .

over 4 years ago · Santiago Trujillo Report

0

I couldn't quickly find a solution to your problem with regular expressions alone, so I hope you'll be okay with using a little more PHP than regular expressions.

This is what I came up with:

 $regexp = '<a\s+(?:[^>]*?\s+)?href=[\'|"]([^"\']*)[\'|"]>(.*?)<\/a>'; preg_match_all("/$regexp/i", $body, $matches); $urls = $matches[1]; $tagContents = $matches[2]; $unsubscribeUrls = []; for ($i = 0; $i < count($tagContents); $i++) { if(!isset($urls[$i]) || !isset($tagContents[$i])){ continue; } if(stripos($tagContents[$i], "unsubscribe") !== false){ $unsubscribeUrls[] = $urls[$i]; } } var_dump($unsubscribeUrls);

This will first match all the tags a into URL and tag content. Then, using PHP, it will check if the content of the tag contains "unsubscribe". Doing so will add it to the $unsubscribeUrls variable. This variable should contain all the URLs you want.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!