3v4l.org

run code in 300+ PHP versions simultaneously
<?php $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);
based on 5SepA
Output for 7.2.0 - 7.2.34, 7.3.0 - 7.3.26, 7.4.0 - 7.4.13, 8.0.0 - 8.0.2
string(437) "Test text http://hello-world.com?utm_source=source&utm_medium=email&utm_campaign=mycamp Test text http://google.com/file.jpg?google=1&utm_source=source&utm_medium=email&utm_campaign=mycamp Test text https://hell.o.wor.ld/test?qwe=qwe&utm_source=source&utm_medium=email&utm_campaign=mycamp Test text text text http://test.test/test?utm_source=source&utm_medium=email&utm_campaign=mycamp hello all how are you I hope you all are fine "