What you're looking for is a combination of pathinfo and parseurl:
pathinfo(parseurl($url)['path'])['filename'];
pathinfo will break the path into well-defined parts, of which filename is that last part you're looking for (2). If you're looking instaed for the absolute location in the path, you may want to split the path on / and simply get the value at index 3.
I use nathaniel fords example but if you run into a problem where files are named '2.html' some servers will load those even though you have '2'.
You can also do this.
home.php?page=2 as the web address
home.php
<?php
// check to see if $page is set
$page = $POST[page];
$page = preg_replace('/\D/', '', $page);
if(!isset($page)){
query page two stuff or what you need.
}
?>
What you're looking for is a combination of
pathinfo
andparseurl
:pathinfo
will break the path into well-defined parts, of whichfilename
is that last part you're looking for (2
). If you're looking instaed for the absolute location in the path, you may want to split the path on/
and simply get the value at index 3.We can test this like so:
And then on the command line:
I use nathaniel fords example but if you run into a problem where files are named '2.html' some servers will load those even though you have '2'.
You can also do this.
home.php?page=2 as the web address