Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append to all urls in a string?

Tags:

php

How should I go about appending to the end of all urls in string of html that is about to be sent out as as email? I want to add the google analytics campaign tracking to it like this:
?utm_source=email&utm_medium=email&utm_campaign=product_notify

99% of the pages will not end in '.html' and some urls might already have things like ?sr=1 at the end of them.

like image 897
Echo says Reinstate Monica Avatar asked Jun 07 '10 14:06

Echo says Reinstate Monica


2 Answers

Update to @ircmaxell's answer, regex now matches even when there are attributes on the before href & code simplification.

/**
 * @param string $body
 * @param string $campaign
 * @param string $medium
 * @return mixed
 */
protected function add_analytics_tracking_to_urls($body, $campaign, $medium = 'email') {
    return preg_replace_callback('#(<a.*?href=")([^"]*)("[^>]*?>)#i', function($match) use ($campaign, $medium) {
        $url = $match[2];
        if (strpos($url, '?') === false) {
            $url .= '?';
        } else {
            $url .= '&';
        }
        $url .= 'utm_source=' . $medium . '&utm_medium=' . $medium . '&utm_campaign=' . urlencode($campaign);
        return $match[1] . $url . $match[3];
    }, $body);
}
like image 97
Aran Avatar answered Oct 31 '22 22:10

Aran


Well... You could do something like this:

function AppendCampaignToString($string) {
    $regex = '#(<a href=")([^"]*)("[^>]*?>)#i';
    return preg_replace_callback($regex, '_appendCampaignToString', $string);
}
function _AppendCampaignToString($match) {
    $url = $match[2];
    if (strpos($url, '?') === false) {
        $url .= '?';
    }
    $url .= '&utm_source=email&utm_medium=email&utm_campaign=product_notify';
    return $match[1].$url.$match[3];
}

That should automatically find all the links on the page (even external ones, so be careful). The ? check just makes sure that we append a query string on to it...

Edit: Fixed issue in the regex that didn't work as intended.

like image 26
ircmaxell Avatar answered Oct 31 '22 22:10

ircmaxell