2

Now I have it like so:

<?
  $url=$_SERVER['REQUEST_URI'];
  $requred_string= substr(strrchr($url, "/"), 1);
?>

and it returns the last segment, but it also returns all the ?p=y&g=x gibberish that I don't need. How can I slice off the $_GET variables?

2
  • 1
    Use parse_url() Commented Apr 6, 2015 at 15:50
  • try to print_r(parse_url($url)); Commented Apr 6, 2015 at 15:51

2 Answers 2

1

You can use strtok to exclude the query strings:

$url = strtok($_SERVER['REQUEST_URI'], '?');
$requred_string = substr(strrchr($url, '/'), 1);
echo $requred_string;

Or as mentioned in the comments, parse_url will work also:

$requred_string = substr(strrchr(parse_url($url)['path'], '/'), 1); // PHP 5.4 or greater with dereference
Sign up to request clarification or add additional context in comments.

Comments

0

here's a more elegant way basename(parse_url($url, PHP_URL_PATH))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.