PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, November 20, 2022

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

 November 20, 2022     laravel, php, preg-replace, preg-replace-callback     No comments   

Issue

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



Answered By - Umair Khan
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing