【问题标题】:Alter all a href links in php更改 php 中的所有 a href 链接
【发布时间】:2017-10-23 20:50:02
【问题描述】:

目前正在处理我需要将 UTM 标记添加到所有链接的事情,但有 1/2 的小问题我无法弄清楚

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

另一个问题是一个小问题,我不确定是否会改变,因为我不得不添加一个 url,如果它在不知道域名的情况下默认将 utm 标签添加到所有 a href's 可能会很整洁.

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

$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);

【问题讨论】:

  • 你可能需要使用 DOM 而不是 RegExp ,stackoverflow.com/a/11235611/2359679
  • 你的代码做什么,不做什么?
  • 代码将 ?utm=some&medium=stuff 添加到 urls = add-link.com需要它来做 add-link.com?test=test&utm=some&medium=stuff

标签: php preg-replace-callback


【解决方案1】:

一旦检测到 url,你可以使用parse_url()parse_str() 来详细说明 url,添加 utm 和 medium 并重建它,而无需过多关心 get 参数的内容或 hash:

$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"。如果链接不是来自 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);

【讨论】:

猜你喜欢
  • 2013-01-12
  • 1970-01-01
  • 2021-09-15
  • 2015-06-20
  • 1970-01-01
  • 2015-07-12
  • 2019-09-05
  • 1970-01-01
  • 2014-10-20
相关资源
最近更新 更多