1

I want to automatically replace the link to add UTM tracking codes when saving the content to the database. I'm using Coldfusion. For example:

 <p>So this is my sample text. 
 <a href="http://test.com/welkom">Click here</a> for the website. 
  <a href="http://test.com/news.cfm?news_id=10" class="test">Click here for the news</a>.

Replace this with:

    <p>So this is my sample text. 
    <a href="http://test.com/welkom?utm_campaign=test&utm_source=Email">Click here</a> for the website. 
    <a href="http://test.com/news.cfm?news_id=10&utm_campaign=test&utm_source=Email" class="test">Click here for the news</a>.

Can someone help me on this one?

mpromonet
  • 11,326
  • 43
  • 62
  • 91
Samall
  • 117
  • 9
  • What options have you tried to get your desired result and where are you having trouble? Is the value in the `href` dynamic? – Tony Junkes Jun 21 '15 at 19:37
  • Well I'm searching the web for a regular expression, but I don't understand them. I also found a jquery example which is working realtime, but I need it for an email campagne tool. So when I save the jquery result to the DB the UTM tags are gone. They added dynamically: http://wptheming.com/2013/10/add-utm-parameters-with-jquery/ – Samall Jun 21 '15 at 19:54
  • also found this in PHP: http://stackoverflow.com/questions/19618894/php-alter-url-in-html – Samall Jun 21 '15 at 20:01
  • Just to clarify, you want to modifying pre-existing HTML and then add it to the DB? This isn't HTML that you're building dynamically first and then adding to the DB? The link to the PHP answer is more involved as it covers different url formats. Are you going to need to cover for all of those situations? Based on that example, you could try and piece something together by using this UDF - http://www.cflib.org/udf/replaceWithCallback as it's similar to `preg_replace_callback()` from what I can tell. When I get a chance, I can take a further look. – Tony Junkes Jun 21 '15 at 21:20

0 Answers0