Search code examples
phplaravelpreg-replacepreg-replace-callback

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


Assume I have the below text in the database.

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

Now for this text, I want to add below utm_source and medium for all URL.

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

I already know that I can find all URL and do str_replace() but I want to do it with

preg_replace()

let me know if anyone knows any other solution

here the problem is some URL already have ? mark in URL so there I need to apped URL with & and where there is no ? there I need to append with ?


Solution

  • Edit Suggested by joy

    Something like this.

    $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

    Edit: With 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.

    Something like this.

    $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

    Edit: With 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