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.
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);
}
                        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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With