Roozbeh J
Roozbeh J

Reputation: 77

Cleaning URL in php

I have set of URLs that I need to import them into database but there are some variables passed in these URLs (Google Tracking codes) and I'm trying to clean automatically.

URL Example: http://canadaam.ctvnews.ca/health/online-test-for-alzheimer-s-measuring-your-cognitive-health-1.1914180&ct=ga&cd=CAIyAA&usg=AFQjCNFZKDiQeiP0vhyBKcqssn9Zz8Lhqg

So as you can see I need to clear anything here

"&ct=ga&cd=CAIyAA&usg=AFQjCNFZKDiQeiP0vhyBKcqssn9Zz8Lhqg"

I do understand that I can clear this using str_replace but this only works if all codes within url are similar and as we all know each URL could have its own code therefore I am looking for a solution or any guidance in regards to how to resolved this.

Any help would be appreciated.

Upvotes: 1

Views: 248

Answers (2)

l0ckm4
l0ckm4

Reputation: 747

try the following

$url = 'http://canadaam.ctvnews.ca/health/online-test-for-alzheimer-s-measuring-your-cognitive-health-1.1914180?ct=ga&cd=CAIyAA&usg=AFQjCNFZKDiQeiP0vhyBKcqssn9Zz8Lhqg';

$parts = parse_url($url);
unset($parts['query']);
echo unparse_url($parts);


function unparse_url($parts_arr) {
   if (strcmp($parts_arr['scheme'], '') != 0) {
     $ret_url = $parts_arr['scheme'] . '://';
   }
   $ret_url .= $parts_arr['user'];
   if (strcmp($parts_arr['pass'], '') != 0) {
     $ret_url .= ':' . $parts_arr['pass'];
   }
   if ((strcmp($parts_arr['user'], '') != 0) || (strcmp($parts_arr['pass'], '') != 0)) {
     $ret_url .= '@';
   }
   $ret_url .= $parts_arr['host'];
   if (strcmp($parts_arr['port'], '') != 0) {
     $ret_url .= ':' . $parts_arr['port'];
   }
   $ret_url .= $parts_arr['path'];
   if (strcmp($parts_arr['query'], '') != 0) {
         $ret_url .= '?' . $parts_arr['query'];
       }
       if (strcmp($parts_arr['fragment'], '') != 0) {
         $ret_url .= '#' . $parts_arr['fragment'];
     }

   return $ret_url;
 }

function found at http://saatske.demon.nl/vanWWW/php/function.parse-url.php

Upvotes: 0

Dean
Dean

Reputation: 368

As long as you don't care about any of the URL parameters you can use explode.

e.g. Something like this should work:

$url = "http://canadaam.ctvnews.ca/health/online-test-for-alzheimer-s-measuring-your-cognitive-health-?1.1914180&ct=ga&cd=CAIyAA&usg=AFQjCNFZKDiQeiP0vhyBKcqssn9Zz8Lhqg";

$urlArray = explode("?", $url);

echo $urlArray[0];

Upvotes: 1

Related Questions