Hi, I'll be sending out a newsletter and i want to track clicks through GA. my idea is that i will grab all the links from the NL body and send them to a redirect script (with the GA campaign,source variables appended) the reason that i will use this redirect script is becuase some of the links in the NL will be external sites. is then a way with regular expressions to grab all the links and link text from a text block like : <a href="link" title="linktitle"> linktext </a> <a href="link" title="linktitle" class="xxxx"> linktext </a> <a href="link" title="linktitle" target="_blank"> linktext </a> <a href="link" title="linktitle"><img src="pathtoimage"/></a> so as i can go something like foreach($matchedlinks as $singlelink){ echo $singlelink->link; echo $singlelink->linktitle; } Many thanks
preg_match_all('/<a title=".*?" href=(.*?)>/', $content, $matches); I believe $matches[0] = the content of title="" and $matches[1] = the url from href="" had this lying around in one of my old scripts
cheers mate can you tell me why the below is not working ? $regexp = "/<a\stitle=(\"??)([^\" >]*?)\\1[^>]*\shref=(\"??)(http[^\" >]*?)\\1[^>]*>(.*)<\/a>/siU";
I'm sorry but I am not very good with developing regular expression maybe this will help you figure it out though: http://www.phpf1.com/tutorial/php-regular-expression.html http://www.regextester.com/
<?php $html = ' <a href="link 1" title="linktitle"> linktext </a> <a href="link 2" title="linktitle" class="xxxx"> linktext </a> <a href="link 3" title="linktitle" target="_blank"> linktext </a> <a href="link 4" title="linktitle"><img src="pathtoimage"/></a> '; if (preg_match_all('~<a\s+(?:.*?)href="([^"]+)"(?:[^>]*)>(.*?)</a>~si', $html, $links)) { $links = array_combine($links[1], $links[2]); foreach ($links AS $url => $title) { echo $title, ': ', $url, "<br />\n"; } } else echo 'No links found'; ?> PHP: ... duplicate links will only show once.