0

I have been racking my brains over this for hours, I need to pass some html to a function and have it replace the links and the return the html with the replaced links.

<?php
    final static public function replace_links($campaign_id, $text) {
        $regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>";
        if(preg_match_all("/$regexp/siU", $text, $matches, PREG_SET_ORDER)) {
            foreach($matches as $match) {
                if(substr($match[2], 0, 2) !== '##') {  //  ignore the placeholders
                    if(substr($match[2], 0, 6) !== 'mailto') {  //  ignore email addresses
                        // $match[2] = link address
                        // $match[3] = link text
                        $url = "http://xxx.com/click?campaign_id=$campaign_id&email=##email_address##&next=" . $match[2];
                        #$text .= str_replace($match[2], $url, $text);
                        #echo $links . "\n";
                        preg_replace($match[2], "<a href='$url'>{$match[3]}</a>", $match[2]);
                    }
                }
                return $text;
            }
        }
    }
?>

When I echo the links it shows all the matched links. Question is how do i return the complete HTML with the replaced links example below.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<a href="mailto:sssss">xxxx</a>
<a href="http://www.abc.com/">xxxx</a>
<a href="http://www.google.com/yeah-baby-yeah">xxxx</a>
</body>
</html>

Should become:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<a href="mailto:sssss">xxxx</a>
<a href="https://xxx.com/click?next=http://www.abc.com/">xxxx</a>
<a href="https://xxx.com/click?next=http://www.google.com/yeah-baby-yeah">xxxx</a>
</body>
</html>

Hope this makes sense.

Thanks in advance,

Kyle

Kyle Hudson
  • 898
  • 1
  • 14
  • 26
  • use domdocument and xpath to grab the link, it should be very straight forward. – ajreal Apr 03 '12 at 19:26
  • Thanks ajreal, I will have a look at it, i have seen http://stackoverflow.com/questions/5317503/php-preg-replace-find-links-and-add-a-hash-to-it – Kyle Hudson Apr 03 '12 at 19:28

2 Answers2

0

Don't reinvent the wheel just use something like this:

http://code.google.com/p/jquery-linkify/

It's really easy i use it as well

Jerome Ansia
  • 6,854
  • 11
  • 53
  • 99
0

I don't know much about preg_replace, but i needed exatcly the same function as you. Changing the line:

preg_replace($match[2], "<a href='$url'>{$match[3]}</a>", $match[2]);

for this:

str_replace($match[0],$url,$text);

seems to do the trick.

I just needed to get the return from this functions, so:

//$text = preg_replace($match[2], "<a href='$url'>{$match[3]}</a>", $match[2]);
$text = str_replace($match[0],$url,$text);