更改 php 中的所有 href 链接

Alter all a href links in php

目前正在做一些我需要将 UTM 标签添加到所有 link 的事情,有 1/2 个我无法弄清楚的小问题

这是我正在使用的代码,问题是如果 link 有一个类似 ?test=test 的参数,那么它会拒绝添加 utm 标签。

另一个问题是一个小问题,我不确定是否可以更改,因为我不得不添加一个 url,如果它默认将 utm 标签添加到所有 href,它可能会很整洁不知道域名。

希望有人能帮助我,把我推向正确的方向。

$url_modifier_domain = preg_quote('add-link.com');

$html_text = preg_replace_callback(
    '#((?:https?:)?//'.$url_modifier_domain.'(/[^\'"\#]*)?)(?=[\'"\#])#i',
    function($matches){
        $url_modifier = 'utm=some&medium=stuff';
        if (!isset($matches[2])) return $matches[1]."/?$url_modifier";
        $q = strpos($matches[2],'?');
        if ($q===false) return $matches[1]."?$url_modifier";
        if ($q==strlen($matches[2])-1) return $matches[1].$url_modifier;
        return $matches[1]."&$url_modifier";
    },
    $html);

一旦检测到 urls,您可以使用 parse_url()parse_str() 来详细说明 url,添加 utm 和 medium 并重建它而不必太在意获取参数或散列的内容:

$url_modifier_domain = preg_quote('add-link.com');

$html_text = preg_replace_callback(
    '#((?:https?:)?//'.$url_modifier_domain.'(/[^\'"\#]*)?)(?=[\'"\#])#i',
    function ($matches) {
        $link = $matches[0];
        if (strpos($link, '#') !== false) {
            list($link, $hash) = explode('#', $link);
        }
        $res = parse_url($link);

        $result = '';
        if (isset($res['scheme'])) {
            $result .= $res['scheme'].'://';
        }
        if (isset($res['host'])) {
            $result .= $res['host'];
        }
        if (isset($res['path'])) {
            $result .= $res['path'];
        }
        if (isset($res['query'])) {
            parse_str($res['query'], $res['query']);
        } else {
            $res['query'] = [];
        }

        $res['query']['utm'] = 'some';
        $res['query']['medium'] = 'stuff';

        if (count($res['query']) > 0) {
            $result .= '?'.http_build_query($res['query']);
        }
        if (isset($hash)) {
            $result .= '#'.$hash;
        }

        return $result;
    },
    $html
);

如您所见,代码更长但更简单

编辑 我做了一些更改,搜索文本中的每个 href="xxx"。如果 link 不是来自 add-link.com 脚本将跳过它,否则他将尝试以可能的最佳方式打印它

$html = 'blabla <a href="http://add-link.com/">a</a>
<a href="http://add-link.com/">a</a>
<a href="http://add-link.com/#hashed">a</a>
<a href="http://abcd.com/#hashed">a</a>
<a href="http://add-link.com/?test=1">a</a>
<a href="http://add-link.com/try.php">a</a>
<a href="http://add-link.com/try.php?test=1">a</a>
<a href="http://add-link.com/try.php#hashed">a</a>
<a href="http://add-link.com/try.php?test=1#hashed">a</a>
<a href="http://add-link.com/try.php?test=1#hashed">a</a>
<a href="//add-link.com?test=test" style="color: rgb(198, 156, 109);">a</a>
';

$url_modifier_domain = preg_quote('add-link.com');

$html_text = preg_replace_callback(
    '/href="([^"]+)"/i',
    function ($matches) {
        $link = $matches[1];

    // ignoring outer links
    if(strpos($link,'add-link.com') === false) return 'href="'.$link.'"';

        if (strpos($link, '#') !== false) {
            list($link, $hash) = explode('#', $link);
        }
        $res = parse_url($link);

        $result = '';
        if (isset($res['scheme'])) {
            $result .= $res['scheme'].'://';
        } else if(isset($res['host'])) {
       $result .= '//';
    }

        if (isset($res['host'])) {
            $result .= $res['host'];
        }
        if (isset($res['path'])) {
            $result .= $res['path'];
        } else {
        $result .= '/';
    }

        if (isset($res['query'])) {
            parse_str($res['query'], $res['query']);
        } else {
            $res['query'] = [];
        }

        $res['query']['utm'] = 'some';
        $res['query']['medium'] = 'stuff';

        if (count($res['query']) > 0) {
            $result .= '?'.http_build_query($res['query']);
        }
        if (isset($hash)) {
            $result .= '#'.$hash;
        }

        return 'href="'.$result.'"';
    },
    $html
);

var_dump($html_text);