我如何在博客内容中的所有 url 中附加 utm_source 和 utm_medium

how can i append utm_source and utm_medium in all url from blog content

假设我在数据库中有以下文本。

$str = 'Test text http://hello-world.com Test text  http://google.com/file.jpg?google=1 Test text https://hell.o.wor.ld/test?qwe=qwe Test text text text http://test.test/test hello all how are you I hope you all are fine ';

现在对于这篇文章,我想在下面添加 utm_source 和所有 URL 的媒介。

$utmUrl = "utm_source=source&utm_medium=email&utm_campaign=mycamp";

我已经知道我可以找到所有 URL 并执行 str_replace() 但我想用

preg_replace()

如果有人知道任何其他解决方案,请告诉我

这里的问题是有些URL已经有了?在 URL 中标记,所以我需要在 URL 中添加 & 而没有?我需要附加 ?

Edit Suggested by joy

类似这样。

$re ='/(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[A-Z0-9+&@#\/%=~_|$])/im';
$str = 'Test text http://hello-world.com Test text  http://google.com/file.jpg?google=1 Test text https://hell.o.wor.ld/test?qwe=qwe Test text text text http://test.test/test hello all how are you I hope you all are fine ';
$utmUrl = "utm_source=source&utm_medium=email&utm_campaign=mycamp";
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
foreach ($matches as $match)
    $str = preg_replace('%' . $match[0] . '%', $match[0] . (strpos($match[0], '?') !== false ? '&' : '?') . $utmUrl, $str);
var_dump($str);

Working Example

编辑:preg_replace_callback

$re = '/(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[A-Z0-9+&@#\/%=~_|$])/im';
$str = 'Test text http://hello-world.com Test text  http://google.com/file.jpg?google=1 Test text https://hell.o.wor.ld/test?qwe=qwe Test text text text http://test.test/test hello all how are you I hope you all are fine ';
$utmUrl = "utm_source=source&utm_medium=email&utm_campaign=mycamp";
$str = preg_replace_callback($re, function ($match) use ($utmUrl) {
    return $match[0] . (strpos($match[0], '?') !== false ? '&' : '?') . $utmUrl . ' ';
}, $str);

var_dump($str);

Working Example

Old Answer.

类似这样。

$re = '%(https?://.*)\s%mU';
$str = 'Test text http://hello-world.com Test text  http://google.com/file.jpg?google=1 Test text https://hell.o.wor.ld/test?qwe=qwe Test text text text http://test.test/test hello all how are you I hope you all are fine ';
$utmUrl = "utm_source=source&utm_medium=email&utm_campaign=mycamp";
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
foreach ($matches as $match)
    $str = preg_replace('%' . $match[1] . '%', $match[1] . (strpos($match[1], '?') !== false ? '&' : '?') . $utmUrl, $str);
var_dump($str);

Working Example

编辑:preg_replace_callback

$re = '%(https?://.*)\s%mU';
$str = 'Test text http://hello-world.com Test text  http://google.com/file.jpg?google=1 Test text https://hell.o.wor.ld/test?qwe=qwe Test text text text http://test.test/test hello all how are you I hope you all are fine ';
$utmUrl = "utm_source=source&utm_medium=email&utm_campaign=mycamp";
$str = preg_replace_callback($re, function ($match) use ($utmUrl) {
    return $match[1] . (strpos($match[1], '?') !== false ? '&' : '?') . $utmUrl . ' ';
}, $str);

var_dump($str);

Working Example