regular expression extract links / php / analytics question

Discussion in 'PHP' started by Jake Nothering, Dec 13, 2010.

  1. #1
    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
     
    Jake Nothering, Dec 13, 2010 IP
  2. shofstetter

    shofstetter Well-Known Member

    Messages:
    178
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    120
    #2
    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:)
     
    shofstetter, Dec 13, 2010 IP
  3. Jake Nothering

    Jake Nothering Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    cheers mate

    can you tell me why the below is not working ?

    $regexp = "/<a\stitle=(\"??)([^\" >]*?)\\1[^>]*\shref=(\"??)(http[^\" >]*?)\\1[^>]*>(.*)<\/a>/siU";
     
    Jake Nothering, Dec 14, 2010 IP
  4. shofstetter

    shofstetter Well-Known Member

    Messages:
    178
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    120
    #4
    shofstetter, Dec 15, 2010 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    
    <?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.
     
    nico_swd, Dec 17, 2010 IP