antdk Posted April 4, 2014 Share Posted April 4, 2014 Hi, I have this string: <?php $url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];?> If i print this, i get something like http://xxxxx.dk/foo/foo/43 http://xxxxx.dk/foo/foo/54 etc Now i only want a specific event to orrur when the last number in the url is a specifc number like 1, 2, 3 or 4). How can this be achieved? Its important to say that the "foo"'s can be variable. So i one url it could be abc/qqq/123 and another hi/mom/9823 . So there might need somekind of "all" code as a replacement for the foos? Quote Link to comment https://forums.phpfreaks.com/topic/287515-check-url/ Share on other sites More sharing options...
ginerjm Posted April 4, 2014 Share Posted April 4, 2014 You confuse yourself methinks. You said that you only care about the last digit, then you worry about some "foo" values which do not appear to be the last digit. This is simple string manipulation which I'm sure you have read about in the php manual. And if not, I suggest that be your next stop. (It should have been your first stop....) Quote Link to comment https://forums.phpfreaks.com/topic/287515-check-url/#findComment-1474942 Share on other sites More sharing options...
QuickOldCar Posted April 5, 2014 Share Posted April 5, 2014 Going by what you asked, this can do what you need. $url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; $last_path = end(explode("/", rtrim($url, "/"))); if (!is_int($last_path) && $last_path < 1) { $last_path = 1; } switch ($last_path) { case 1: echo "Number one"; break; case 2: echo "Number two"; break; case 3: echo "Number three"; break; case 4: echo "Number four"; break; } Did you consider using GET requests in your urls? http://xxxxx.dk/foo/foo/?page=4 if(isset($_GET['page']) && trim($_GET['page']) != '' && ctype_digit($_GET['page']) && $_GET['page'] >= 1){ $page = trim($_GET['page']); } else { $page = 1; } echo $page; Quote Link to comment https://forums.phpfreaks.com/topic/287515-check-url/#findComment-1475014 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.