0

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 ?

2
  • May be it can help stackoverflow.com/a/31647970/4575350 Commented Oct 13, 2020 at 13:05
  • @sta i want to append i don't want to remove Commented Oct 13, 2020 at 13:10

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

I know this way but i don't want to do a loop and all is it possible with preg_replace_callback or directly throw regex?
$pattern = '/(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*)|[-A-Z0-9+&@#\/%=~_|$?!:,.])*(?:([-A-Z0-9+&@#\/%=~_|$?!:,.]*)|[A-Z0-9+&@#\/%=~_|$])/im'; i have used this pattern to find URL inside anchor tag as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.