重写字符串中的多个链接URL

I have a long string that contains multiple HTML links that looks like this:

<a href="example.com">My Link></a>

or

<a href="http://example2.com">A different Link</a>

etc.

I need to rewrite these links in PHP so that they send the traffic thru my redirector, so I can tell users they are now leaving to an external domain, etc. My redirector is located at mydomain.com/leaving.php. What I want to do is rewrite these links to something like this:

<a href="http://www.mydomain.com/leaving.php/[URL ENCODED LINK FROM ABOVE]">My Link>

Since not all urls have http:// to begin with I think I need to first strip those from all href links.

How can I grep the correct HTML links (ignoring image src) and url_encode them, and place them back in the original string.

EDIT: Just to clearify, I am not looking for help with the redirection part, just how to replace several URLs within a large string when they sometimes have http

This is just a pseudo that you can modify it as you need.

You need first a .htaccess file that contents following lines;

RewriteEngine On
RewriteRule ^leaving/(.*)$ leaving.php?url=$1 [L]

And in leaving.php;

$url = trim(urldecode($_GET['url']));
// check url is exists
if ($url == '') {
    header('Location: http://www.mydomain.com/');
    exit;
}
// add http if needs
if (substr($url, 0, 7) != 'http://') {
    $url = 'http://'. $url;
}

// send it to target
header('Location: '. $url);
exit;

UPDATE:

If you trying this on server-side, it's useless, cos if PHP sent output once, then u cannot use preg's anymore. So, if want to do this on client-side, following code or somthing like will help you.

var links = document.getElementsByTagName("a"),
    link, href, i = 0;
while (link = links[i++]) {
    // get real url applying getAttribute with "2" params
    if ((href = link.getAttribute("href", 2)) !== null
            // skip non-href links
            && href.charAt(0) !== "#") {
        // add http if not exists
        if (href.substring(0, 7) !== 'http://') {
            href = "http://"+ href
        }
        link.href = "http://www.mydomain.com/leaving/"+ href;
    }
}

Editing XML using regular expressions is error-prone and awkward, but there are handy tools around.

Easiest end most reliable way to edit HTML is using DOM and XPath. Find all links and rewrite them.

Possibly you want to add some filter to exclude internal URLs. You can either do this in the XPath query (possibly more elegant and faster as less results have to be processed) or the foreach loop.

<?php
    $html = <<< HTML
<p>
<a href="example.com">My Link>
<a href="http://example2.com">A different Link</a>
</p>
HTML;

    $dom = new DOMDocument;
    $dom->loadHTML($html);

    // Find all anchor elements containing a href attribute
    $xpath = new DOMXPath($dom);
    $anchors = $xpath->query('//a[@href]');

    // Replace all href attributes with redirection url
    foreach ($anchors as $anchor)
        // Possibly filter internal URLs?
        $anchor->setAttribute('href', 'http://www.mydomain.com/leaving.php/'.urlencode($anchor->getAttribute('href')));

    // Save html with replaced links
    $newHtml = $dom->saveXml($dom->documentElement);
?>