PHP preg_replace expression to remove URL parameter - php

I wanted to remove all occurrences of specific pattern of a parameter from a URL using preg_expression. Also removing the last "&" if exist
The pattern looks like: make=xy ("make" is fixed; "xy" can be any two letters)
Example:
http://example.com/index.php?c=y&make=yu&do=ms&r=k&p=7&
After processing preg_replace, the outcome should be:
http://example.com/index.php?c=y&do=ms&r=k&p=7
I tried using:
$url = "index.php?ok=no&make=ae&make=as&something=no&make=gr";
$url = preg_replace('/(&?lang=..&?)/i', '', $url);
However, this did not work well because I have duplicates of make=xx in the URL (which is a case that could happen in my app).

You don't need RegEx for this:
$url = "http://example.com/index.php?ok=no&make=ae&make=as&something=no&make=gr&";
list($file, $parameters) = explode('?', $url);
parse_str($parameters, $output);
unset($output['make']); // remove the make parameter
$result = $file . '?' . http_build_query($output); // Rebuild the url
echo $result; // http://example.com/index.php?ok=no&something=no

You could try using:
$str = parse_url($url, PHP_URL_QUERY);
$query = array();
parse_str($str, $query);
var_dump($query);
This will return to you the query as an array. You could then use http_build_query() function to restore the array in a query string.
But if you want to use regexp:
$url = "index.php?make=ae&ok=no&make=ae&make=as&something=no&make=gr";
echo $url."\n";
$url = preg_replace('/\b([&|&]{0,1}make=[^&]*)\b/i','',$url);
$url = str_replace('?&','?',$url);
echo $url;
This will remove all make in the URL

with rtrim you can remove last &
$url = rtrim("http://example.com/index.php?c=y&make=yu&do=ms&r=k&p=7&","&");
$url = preg_replace('~&make=([a-z\-]*)~si', '', $url);

$url = "index.php?ok=no&make=ae&make=as&something=no&make=gr";
$url = preg_replace('/(&?make=[a-z]{2})/i', '', $url);
echo $url;

Just by using preg_replace
$x = "http://example.com/index.php?c1=y&make=yu&do1=ms&r1=k&p1=7&";
$x = preg_replace(['/(\?make=[a-z]*[&])/i', '/(\&make=[a-z]*[^(&*)])/i', '/&(?!\w)/i'], ['?','',''], $x);
echo $x;
And the result is: http://example.com/index.php?c1=y&do1=ms&r1=k&p1=7
Hope this will be helpful to you guys.

Related

PHP and cURL extract variable from header

$response = curl_exec($ch);
preg_match_all('/^Location:(.*)$/mi', $response, $matches);
curl_close($ch);
Using this script, I get the output:
http://website.com/632383970568166e82391f.png
And I need to extract the ID, which is abcdef and use it in another call. How can I do that?
Use regular expressions or do this:
$url = "http://subdomain.website.com/something/folder1/folder2/folder3/folder4/folder5/20385425_632383970568166e82391f.png";
$urlParts = explode('/', $url)
$imgFileParts = explode('_', $urlParts[(count($urlParts) - 1)]);
$imgId = $imgFileParts[0];
Simple regex
$re = "/\\S+\\/([\\S]+)_/";
$str = "http://subdomain.website.com/something/folder1/folder2/folder3/folder4/folder5/20385425_632383970568166e82391f.png";
preg_match($re, $str, $matches);

how to check content of request_uri

hello i would like to know how to avoid double postings in a request_uri.
as an example:
http://www.example.com/foo/foo/...
should only be:
http://www.example.com/foo/...
for example i would like to create a check that i need for a header-function that needs to be checked before it headers. it should not header if the url-string contains double postings. so the check would be something like:
$host = $_SERVER['HTTP_HOST'];
$url = $_SERVER['REQUEST_URI'];
$urlArray = explode('/', $url);
$urlArrayUnique = array_unique($urlArray);
$urlUnique = implode('/', $urlArrayUnique);
if (isset($_SESSION['a'])){
$var = $_SESSION['a'];
if($url !== $urlUnique){
header ('Location:'.$host.'/'.$var.'/'.$basename);
exit;
}
}
if there is someone who could help me out i really would appreciate.
thanks alot.
Try the below code, use explode and get unique and implode it into URL....
$url = $_SERVER['REQUEST_URI'];
$urlArray = explode('/', $url);
$urlArrayUnique = array_unique($urlArray);
$urlUnique = implode('/', $urlArrayUnique);
You can get dulicate values in $urlArray too...
You can modify this below code to suit as per your need:
$host = $_SERVER['HTTP_HOST'];
$url = $_SERVER['REQUEST_URI'];
$urlArray = explode('/', $url);
$lai = max(array_keys($urlArray));//here we get the max array index
if($urlArray[$lai-1] != $urlArray[$lai])header('Location:'.$host.'/'.$var.'/'.$basename);

Regex function to change url to another

Im totally new to regex. I need your help.
What is the regex function for changing the below url to url listed downside?
How to use that regex function in PHP.
https://fbcdn-photos-a.akamaihd.net/hphotos-ak-ash4/393656_257350694313804_126044397444435_712409_344887174_s.jpg
TO
https://fbcdn-sphotos-a.akamaihd.net/hphotos-ak-ash4/s320x320/393656_257350694313804_126044397444435_712409_344887174_n.jpg
OR
https://fbcdn-sphotos-a.akamaihd.net/hphotos-ak-ash4/s480x480/393656_257350694313804_126044397444435_712409_344887174_n.jpg
Thanks in advance.
$size = "s320x320";
$url = preg_replace("#https://(.*)/(.*)/(.*)\_s.jpg#i", "https://$1/$2/$size/$3_n.jpg", $url);
$url = str_replace("-photos-", "-sphotos-", $url);
This code is untested, but should work. The third line, str_replace, is used to make the regex simpler. :)
preg_replace( '#^(.*?)photos(.*)(/[^/]*?)_s.jpg$#', '$1sphotos$2/s320x320$3_n.jpg', $url );
Ok, let me try to answer with some code
$url = 'https://fbcdn-photos-a.akamaihd.net/hphotos-ak-ash4/393656_257350694313804_126044397444435_712409_344887174_s.jpg';
$str = array('fbcdn-photos-a.akamaihd.net/hphotos-ak-ash4', '_s');
$rep1 = array('fbcdn-sphotos-a.akamaihd.net/hphotos-ak-ash4/s320x320', '_n');
$rep2 = array('fbcdn-sphotos-a.akamaihd.net/hphotos-ak-ash4/s480x480', '_n');
//TO
$url1 = str_replace($str, $rep1, $url);
//OR
$url2 = str_replace($str, $rep2, $url);

Grabbing digits at end of URL using PHP

On a page of mine, I have a GET as a URL of a website.
mypage.com/page.php?=URLHERE
On this URL, I need the ID at the very end of the URL
mypage.com/page.php?url=http://www.otherwebsite.com/something.php?id=%%%%%%%
These numbers are sometimes different amount of digits, so how would I do that?
My code:
$url = $_GET['url'];
Assuming the url parameter is a properly encoded URL, then useparse_url() to get the URL components and parse_str() to retrieve the id parameter from its query string.
$url = $_GET['url'];
// First parse_url() breaks the original URL up
$parts = parse_url($url);
// parse_str() parses the query string from the embedded URL
$query = parse_str($parts['query']);
// Finally, you have your full id parameter
$id = $query['id'];
assuming the the url has id at the begining of query_string
<?php
$url = $_GET['url'];
$url = basename($url);
$url =explode("?",$url);
$url = explode("=",$url[1]);
echo $url[1];
?>
try the parse_url
$url = parse_url($_GET['url'], PHP_URL_QUERY);
$query = explode('=', $url);
$id = $query[1];
I'd use the PHP function explode() http://php.net/manual/en/function.explode.php
For this example
$numbers = explode("=",$url);
$id_value = $numbers[2];
print $id_value;

PHP: Remove 'WWW' from URL inside a String

Currently I am using parse_url, however the host item of the array also includes the 'WWW' part which I do not want. How would I go about removing this?
$parse = parse_url($url);
print_r($parse);
$url = $parse['host'] . $parse['path'];
echo $url;
$url = preg_replace('#^www\.(.+\.)#i', '$1', $parse['host']) . $parse['path'];
This won't remove the www in www.com, but www.www.com results in www.com.
preg_replace('#^(http(s)?://)?w{3}\.#', '$1', $url);
if you don't need a protocol prefix, leave the second parameter empty
$url = preg_replace('/^www\./i', '', $parse['host']) . $parse['path'];

Categories