string replace

Hey there, I'm having a problem with a string replace not working as it should.

Basically, I've got a button link on my home page - https://cretecarrier.com/ - (Hunt Flatbed APPLY)
The link is https://intelliapp2.driverapponline.com/c/hunt?r=CreteCarrier-Homepage-HUNT-APPLY

Now, I also have a tracking function which stores UTM codes as cookies. I also use it to append it to url's.  

So an example, i click a google add which takes me to our websites.  https://cretecarrier.com/?utm_source=the-big-test

In theory, what should happen is the link - https://intelliapp2.driverapponline.com/c/hunt?r=CreteCarrier-Homepage-HUNT-APPLY . should change to https://intelliapp2.driverapponline.com/c/hunt?utm_source=the-big-test . BUT it is changing to https://intelliapp2.driverapponline.com/c/hunt?utm_source=the-big-test?r=CreteCarrier-Homepage-HUNT-APPLY

How can i, if i come into the site through an add with a UTM code, change the url from
- https://intelliapp2.driverapponline.com/c/hunt?r=CreteCarrier-Homepage-HUNT-APPLY 
TO
- https://intelliapp2.driverapponline.com/c/hunt?utm_source=the-big-test

Here is my current code.
function set_ad_cookies() {
	
  $vars = array('utm_source', 'utm_campaign', 'utm_media');
  foreach ($vars as $k) {
      if (isset($_GET[$k]) && !isset($_COOKIE['orig_'.$k])) {
          setcookie('orig_'.$k, $_GET[$k], 0, '/', $domains['cookie_domain']);
      }
  }
  
    if (!isset($_COOKIE['orig_utmsource'])) {
	  setcookie('orig_utmsource', $_GET['utm_source'], 0, '/', $domains['cookie_domain']);
	}
	if (!isset($_COOKIE['orig_utmmedium'])) {
	  setcookie('orig_utmmedium', $_GET['utm_medium'], 0, '/', $domains['cookie_domain']);
	}
	if (!isset($_COOKIE['orig_utmcampaign'])) {
	  setcookie('orig_utmcampaign', $_GET['utm_campaign'], 0, '/', $domains['cookie_domain']);
	}
}
add_action('init', 'set_ad_cookies');



function replace_linkshunt_home($text) {
	$url = explode('?', 'https://'.$_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
	$addCookiesToLink = "";
		if (strlen($url[1]) > 1) {
		$text = str_replace('https://intelliapp2.driverapponline.com/c/hunt?r=CreteCarrier-Homepage-HUNT-APPLY', 'https://intelliapp2.driverapponline.com/c/hunt?' . $url[1], $text);
	}
	else { //no parameters in URL. Cookies?
		if(isset($_COOKIE['orig_utmsource'])) {
			$addCookiesToLink .= "utm_source=" . $_COOKIE['orig_utmsource'];
		}
		elseif(isset($_COOKIE['orig_utm_source'])) {
			if(strlen($addCookiesToLink) > 0) {
				$addCookiesToLink .= "&";
			}
			$addCookiesToLink .= "utm_source=" . $_COOKIE['orig_utm_source'];
		}
		if(isset($_COOKIE['orig_utmmedium'])) {
			if(strlen($addCookiesToLink) > 0) {
				$addCookiesToLink .= "&";
			}	
			$addCookiesToLink .= "utm_medium=" . $_COOKIE['orig_utmmedium'];
		}
		if(isset($_COOKIE['orig_utm_media'])) {
			if(strlen($addCookiesToLink) > 0) {
				$addCookiesToLink .= "&";
			}	
			$addCookiesToLink .= "utm_media=" . $_COOKIE['orig_utm_media'];
		}
		if(isset($_COOKIE['orig_utmcampaign'])) {
			if(strlen($addCookiesToLink) > 0) {
				$addCookiesToLink .= "&";
			}	
			$addCookiesToLink .= "utm_campaign=" . $_COOKIE['orig_utmcampaign'];
		}
		elseif(isset($_COOKIE['orig_utm_campaign'])) {
			if(strlen($addCookiesToLink) > 0) {
				$addCookiesToLink .= "&";
			}	
			$addCookiesToLink .= "utm_campaign=" . $_COOKIE['orig_utm_campaign'];
		}
			if(strlen($addCookiesToLink) > 0) {
				//echo $addCookiesToLink;
					$text = str_replace('https://intelliapp2.driverapponline.com/c/hunt?r=CreteCarrier-Homepage-HUNT-APPLY', 'https://intelliapp2.driverapponline.com/c/hunt?' . $addCookiesToLink, $text);
		}
		
		//echo "test1" . $_COOKIE['orig_utm_campaign'] . " 2:" . $_COOKIE['orig_utm_source'] . " 3:" . $_COOKIE['orig_utmcampaign'] . " 4:" . $_COOKIE['orig_utmmedium'] . " 5:" . $_COOKIE['orig_utmsource'];
	}
	return $text;
}
add_filter('widget_text', 'replace_linkshunt_home');
add_filter('the_content', 'replace_linkshunt_home');

Open in new window

Jon ImmsWeb DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Just to clarify your question can be simplified to this?
How can i, if i come into the site through an add with a UTM code, change the url from
- https://intelliapp2.driverapponline.com/c/hunt?r=CreteCarrier-Homepage-HUNT-APPLY 
TO
- https://intelliapp2.driverapponline.com/c/hunt?utm_source=the-big-test
There are several options you can use.
My question is - what were you expecting.
Let's look at it like this
IN THIS               : https://intelliapp2.driverapponline.com/c/hunt?r=CreteCarrier-Homepage-HUNT-APPLY
SEARCH FOR THIS       : https://intelliapp2.driverapponline.com/c/hunt?r=CreteCarrier-Homepage-HUNT-APPLY
AND REPLACE WITH THIS : https://intelliapp2.driverapponline.com/c/hunt?

Open in new window

Or to put it another way
Replace AB in ABC with DE => DEC

I am not clear on what is coming in though - where is the utm_source=the-big-test coming from?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Julian HansenCommented:
You did get a response to your question.

I submitted a post with questions clarifying your requirement. You never responded to that.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.