Function to remove get variable with php

I have this URI.

http: //localhost/index.php? properties & status = av & page = 1

I am retrieving the base name of the URI using the following code.

$basename = basename($_SERVER['REQUEST_URI']); 

the above code gives me the following line.

?

index.php properties & state = ay & page = 1

I would like to remove the last variable from the line ie &page=1 . note that the page value will not always be 1 . Given this, I would like to trim the variable this way.

Trim from last position of line to first separator ie &

Update:

I would like to remove &page=1 from the string, no matter what position it is in.

how to do it?

+6
source share
6 answers

Instead of hacking into a regular expression, you should parse the string as a url (what is it)

 $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 url

 $queryString = http_build_query($queryParams); $url = $parts['path'] . '?' . $queryString; 
+18
source

There are many roads leading to Rome. I would do it with RegEx:

 $myString = 'index.php?properties&status=av&page=1'; $myNewString = preg_replace("/\&[a-z0-9]+=[0-9]+$/i","",$myString); 

if you only need the parameters &page=1 -type, the last line will be

 $myNewString = preg_replace("/\&page=[0-9]+/i","",$myString); 

if you also want to get rid of the possibility that the page is the only or first parameter:

 $myNewString = preg_replace("/[\&]*page=[0-9]+/i","",$myString); 
+3
source

Thanks guys, but I think I found a better solution, @KingCrunch suggested a solution that I expanded and converted it to a function. the function below can delete or cancel any URI variable without using the hackers used. I am sending it as it can 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 thanks @KingKrunch for your decision :)

+3
source
 $pos = strrpos($_SERVER['REQUEST_URI'], '&'); $url = substr($_SERVER['REQUEST_URI'], 0, $pos - 1); 

Documentation for strrpos.

+2
source

Regex, which works in all possible situations: /(&|(?<=\?))page=.*?(?=&|$)/ . Here is a sample 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"; } 

Conclusion:

 index.php?properties&status=av index.php?properties&status=av index.php? 

Regex explanation:

  • (&|(?<=\?)) - either match & , or ? but if he ? , don't put it in a match and just ignore it (you don't want URLs like index.php&status=av )
  • page=.*? - corresponds to page=[...]
  • (?=&|$) - find & or the end of the line ( $ ), but don’t add them to replace (this group helps the previous one to find out exactly where to stop matching)
+2
source

You can use RegEx (as Chris suggests), but this is not the most efficient solution (a lot of overhead using this engine ... this is easy to do with some parsing of strings:

 <?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 that you can guarantee that $page gets stuck at the end? If it’s great, if it’s not ... what you asked for may not always solve the problem.

+1
source

Source: https://habr.com/ru/post/895905/


All Articles