как я могу добавить utm_source и utm_medium во все url-адреса из контента блога
Предположим, что у меня есть приведенный ниже текст в базе данных.
$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 и medium для всех URL-адресов.
$utmUrl = "utm_source=source&utm_medium=email&utm_campaign=mycamp";
Я уже знаю, что могу найти все URL-адреса и сделать str_replace (), но я хочу сделать это с помощью
подскажите()
дайте мне знать, если кто-нибудь знает какое-либо другое решение
здесь проблема в том, что какой-то URL уже есть ? пометка в URL так там мне нужно добавить URL с & а где его нет ? там мне нужно что-то добавить ?
У вопроса есть решение - Посмотреть?
Ответы - как я могу добавить utm_source и utm_medium во все url-адреса из контента блога / how can i append utm_source and utm_medium in all url from blog content
Является ответом!

13.10.2020 04:22:23
Правка, предложенная радость
Что-то вроде этого.
$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);
Edit: с 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);
старый ответ.
Что-то вроде этого.
$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);
Edit: с 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);
Помочь в развитии проекта: