<?php
$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);
string(429) "Test text http://hello-world.com?utm_source=source&utm_medium=email&utm_campaign=mycampTest text http://google.com/file.jpg?google=1&utm_source=source&utm_medium=email&utm_campaign=mycampTest text https://hell.o.wor.ld/test?qwe=qwe&utm_source=source&utm_medium=email&utm_campaign=mycampTest text text text http://test.test/test?utm_source=source&utm_medium=email&utm_campaign=mycamphello all how are you I hope you all are fine "
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
string(429) "Test text http://hello-world.com?utm_source=source&utm_medium=email&utm_campaign=mycampTest text http://google.com/file.jpg?google=1&utm_source=source&utm_medium=email&utm_campaign=mycampTest text https://hell.o.wor.ld/test?qwe=qwe&utm_source=source&utm_medium=email&utm_campaign=mycampTest text text text http://test.test/test?utm_source=source&utm_medium=email&utm_campaign=mycamphello all how are you I hope you all are fine "