Removing utm source via php / regex - php

<?php
$before='http://www.urchin.com/download.html? utm_source=google&utm_medium=email&utm_campaign=product';
$after = preg_replace('[?]utm_source=.*/','', $before);
echo $after;
?>
Hi all,
How can I remove UTM tracking from URL via PHP/Regex in the above code example?
New to PHP so please explain your answer.
Thanks in advance!
Edit: Got a bit closer but still getting errors.

$url = strtok($url, '?');
You can read more about strtok here.
Update: If you need to remove only utm_ params, you can use regular expression, e.g.:
$url = preg_replace( '/&?utm_.+?(&|$)$/', '', $url );
Note: This regex will remove any utm_ parameter from your URL.

Use strtok to get the url as you like
$url = strtok($url, '?');

Figured out:
<?php
$before='http://www.urchin.com/download.html?utm_source=google&utm_medium=email&utm_campaign=product';
$after = preg_replace('/[?]utm_source=.*/','', $before);
echo $after;
?>

Here is my solution how to remove all UTM params from URL including hash UTM parameters:
<?php
$urls = [
'https://www.someurl.com/?utm_medium=flow&red=true&article_id=5456#omg&utm_medium=email',
'https://www.someurl.com/#utm_medium=flow&utm_medium=email',
'https://www.someurl.com/?articleid=1235&utm_medium=flow&utm_medium=email',
'https://www.someurl.com/?utm_medium=flow&articleid=1235&utm_medium=email',
'https://www.someurl.com/?utm_medium=encoding%20space%20works&encoding=works%20also'
];
foreach ($urls as $url) {
echo rtrim(preg_replace('/utm([_a-z0-9=%]+)\&?/', '', $url), '&?#') . PHP_EOL;
}

Related

Extract a Flickr photo ID from URL

I need to extract a piece of a URL in PHP. From the URL http://www.flickr.com/photos/28370443#N08/7885410582 I need only the 7885410582 part.
My code:
$string2 = 'http://www.flickr.com/photos/28370443#N08/7885410582';
preg_match_all('~s/ (.*?) /~i',$string2,$matches, PREG_PATTERN_ORDER);
echo $matches[1][0] . '<br />';
Can anyone look at it and correct it for me?
Thanks in advance.
you can use
$data = explode("/",$url)
$value = $data[sizeof($data)-1]
try this regex:
\d+$
will find all digits before string end
or:
(?<=/)\d+$
will find all digits after / sign before string end
If the identifier you want is always the last in the URL, don't bother with regular expressions and instead use simple string functions.
<?php
$url = 'http://www.flickr.com/photos/28370443#N08/7885410582';
$flickr_id = substr($url, strrpos($url, "/") + 1);
echo $flickr_id; // 7885410582
Try this:
preg_match_all('~^.+/(\d+)$~',$string2,$matches);
Description
Demo
http://regex101.com/r/aL2hL3
Sample code
$string2 = 'http://www.flickr.com/photos/28370443#N08/7885410582';
preg_match('~^.+/(\d+)$~',$string2,$matches);
echo $matches[1]; // 7885410582

preg_replace remove http: from link

Trying to retrieve data from the database to put into a YouTube frame
When a person submits their link to their video, they send it as http://ww... I then need to convert that when it displays in the iframe as //ww... so how do I remove the http: from their links using preg_replace?
You can use ltrim
$newUrl = ltrim($url, 'http:');
You want regex like this:
$new = preg_replace( '/^https?:\/\//', '', $url );
That will ensure http:// and https:// are removed.
^ = start of the string
? = previous character optional
If your site is ONLY allowing http:// then #Aurelio is correct
Here you go.. preg_replace( "#^[^:.]*[:]+#i", "", $URL );
you can try this code below:
<?php
$url = "http://youtube.com";
$url = preg_replace( "#^[^:.]*[:]+#i", "", $url);
echo $url;
?>
You can achieve the same goal with str_replace() that is faster:
$newUrl = str_replace('http:, '', $url);

Function to remove GET variable with php

i have this URI.
http://localhost/index.php?properties&status=av&page=1
i am fetching basename of the URI using following code.
$basename = basename($_SERVER['REQUEST_URI']);
the above code gives me following string.
index.php?properties&status=av&page=1
i would want to remove the last variable from the string i.e &page=1. please note the value for page will not always be 1. keeping this in mind i would want to trim the variable this way.
Trim from the last position of the string till the first delimiter i.e &
Update :
I would like to remove &page=1 from the string, no matter in which position it is on.
how do i do this?
Instead of hacking around with regular expression you should parse the string as an url (what it is)
$string = 'index.php?properties&status=av&page=1';
$parts = parse_url($string);
$queryParams = array();
parse_str($parts['query'], $queryParams);
Now just remove the parameter
unset($queryParams['page']);
and rebuild the url
$queryString = http_build_query($queryParams);
$url = $parts['path'] . '?' . $queryString;
There are many roads that lead to Rome. I'd do it with a RegEx:
$myString = 'index.php?properties&status=av&page=1';
$myNewString = preg_replace("/\&[a-z0-9]+=[0-9]+$/i","",$myString);
if you only want the &page=1-type parameters, the last line would be
$myNewString = preg_replace("/\&page=[0-9]+/i","",$myString);
if you also want to get rid of the possibility that page is the only or first parameter:
$myNewString = preg_replace("/[\&]*page=[0-9]+/i","",$myString);
Thank you guys but i think i have found the better solution, #KingCrunch had suggested a solution i extended and converted it into function. the below function can possibly remove or unset any URI variable without any regex hacks being used. i am posting it as it might help someone.
function unset_uri_var($variable, $uri) {
$parseUri = parse_url($uri);
$arrayUri = array();
parse_str($parseUri['query'], $arrayUri);
unset($arrayUri[$variable]);
$newUri = http_build_query($arrayUri);
$newUri = $parseUri['path'].'?'.$newUri;
return $newUri;
}
now consider the following uri
index.php?properties&status=av&page=1
//To remove properties variable
$url = unset_uri_var('properties', basename($_SERVER['REQUEST_URI']));
//Outputs index.php?page=1&status=av
//To remove page variable
$url = unset_uri_var('page', basename($_SERVER['REQUEST_URI']));
//Outputs index.php?properties=&status=av
hope this helps someone. and thank you #KingKrunch for your solution :)
$pos = strrpos($_SERVER['REQUEST_URI'], '&');
$url = substr($_SERVER['REQUEST_URI'], 0, $pos - 1);
Documentation for strrpos.
Regex that works on every possible situation: /(&|(?<=\?))page=.*?(?=&|$)/. Here's example code:
$regex = '/(&|(?<=\?))page=.*?(?=&|$)/';
$urls = array(
'index.php?properties&status=av&page=1',
'index.php?properties&page=1&status=av',
'index.php?page=1',
);
foreach($urls as $url) {
echo preg_replace($regex, '', $url), "\n";
}
Output:
index.php?properties&status=av
index.php?properties&status=av
index.php?
Regex explanation:
(&|(?<=\?)) -- either match a & or a ?, but if it's a ?, don't put it in the match and just ignore it (you don't want urls like index.php&status=av)
page=.*? -- matches page=[...]
(?=&|$) -- look for a & or the end of the string ($), but don't include them for the replacement (this group helps the previous one find out exactly where to stop matching)
You could use a RegEx (as Chris suggests) but it's not the most efficient solution (lots of overhead using that engine... it's easy to do with some string parsing:
<?php
//$url="http://localhost/index.php?properties&status=av&page=1";
$base=basename($_SERVER['REQUEST_URI']);
echo "Basename yields: $base<br />";
//Find the last ampersand
$lastAmp=strrpos($base,"&");
//Filter, catch no ampersands found
$removeLast=($lastAmp===false?$base:substr($base,0,$lastAmp));
echo "Without Last Parameter: $removeLast<br />";
?>
The trick is, can you guarantee that $page will be stuck on the end? If it is - great, if it isn't... what you asked for may not always solve the problem.

Replace string using php preg_replace

Hi all i know preg_replace can be used for formatting string but i need help in that concerned area my url will be like this
http://www.example.com/index.php/
also remove the http,https,ftp....sites also
what i want is to get
result as
example.com/index.php
echo preg_replace("~(([a-z]*[:](//))|(www.))~", '', "ftp://www.example.com");
$url = 'http://www.example.com/index.php/';
$strpos = strpos($url,'.');
$output = substr($url,$strpos+1);
$parts=parse_url($url);
unset($parts['scheme']);
//echo http_build_url($parts);
echo implode("",$parts);
EDIT
To use http_build_url you needs pecl_http you can use implode as alternate
Something like this
$url = "http://www.example.com/index.php";
$parts = parse_url($url);
unset($parts['scheme']);
echo preg_replace('/^((ww)[a-z\d][\x2E])/i', '', join('', $parts));
Output
example.com/index.php
Example #2
$url = "http://ww3.nysif.com/Workers_Compensation.aspx";
Output
nysif.com/Workers_Compensation.aspx

Remove URL regardless of format

Having a brain freeze...
Have a URL which may be in any of the formats :
http://url.com/stuff
url.com/somestuff
www.url.com/otherstuff
https://www.url.com/morestuff
You get the picture.
How do I remove the .com part to leave just the various 'stuff' parts ? For example, the above would end up :
stuff
somestuff
otherstuff
morestuff
You could achieve that using the following code:
$com_pos = strpos($url, '.com/');
$stuff_part = substr($url, $com_pos + 5);
Click here to see the working code.
This should do the trick for you!
<?php
$url = "http://url.com/stuff";
$querystring = preg_replace('#^(https|http)?(://)?(www.)?([a-zA-Z0-9-]+)\.[a-zA-Z]{2,6}/#', "", $url);
echo $querystring;
I submitted this answer because I'm not very fond of solutions using explode() to handle this. Maybe your query string contains more slashes so, you'd have to write exceptions for those cases.
You can use explode to make an array, then get the last element from the array.
$str = 'http://url.com/stuff';
$arr = explode('/', $str);
echo end($arr); // 'stuff'
$path = parse_url('http://url.com/stuff', PHP_URL_PATH);
If you leave the second parameter unspecified you can return an array including the domain etc.
Use explode function to divide the string.
<?php
$url = "http://url.com/stuff";
$stuff = explode("/", $url);
echo $stuff[sizeof($stuff) - 1];
?>
I used sizeof to access to last element.
preg_replace("/^(https?:\/\/)?[^\/]+/" ,"", $url);

Categories