-3

if I have some URL like these:

http://localhost/myfile.php?start=2018&end=2019&page=1&status=

Did anynone know how to replace any words from that link that contain page= ?

The result that I want is :

http://localhost/myfile.php?start=2018&end=2019&status=

Even if the link is like this:

http://localhost/myfile.php?start=2018&end=2019&status=&page=3

I still want the result will be without page variable and value, so it might be: http://localhost/myfile.php?start=2018&end=2019&status=

any idea how to do this? Especially without regex (regex is my last option)

Cross Vander
  • 2,077
  • 2
  • 19
  • 33

1 Answers1

5

You can use parse_url(), parse_str() and http_build_query():

// Your URL
$str = 'http://localhost/myfile.php?start=2018&end=2019&page=1&status=';

// Get parts
$parts = parse_url($str);

// Get array of arguments
parse_str($parts['query'], $args);

// Remove unwanted index
unset($args['page']);

// Rebuild your URL
echo $parts['scheme'] . '://' . $parts['host'] . $parts['path'] . '?' . http_build_query($args);

// Output: http://localhost/myfile.php?start=2018&end=2019&status=

I encouraged to read documentation or to print_r() vars of this sample code for a better understanding.

AymDev
  • 6,626
  • 4
  • 29
  • 52