Fluoresce Posted October 22, 2013 Share Posted October 22, 2013 $requestURI = $_SERVER['REQUEST_URI']; if($requestURI == '/articles/1') { // do something } I want the above code to be executed if the page URI is /articles/1, /articles/2, /articles/3, etc. Does PHP have a character placeholder to put in place of "1" so that the code will execute no matter what number appears after /articles/? I've checked online but couldn't find one. Quote Link to comment Share on other sites More sharing options...
requinix Posted October 22, 2013 Share Posted October 22, 2013 Use URL rewriting so you don't have to do this work yourself. For example, with Apache, mod_rewrite, and a .htaccess you do RewriteEngine on RewriteRule ^articles/(\d+)$ articles.php?id=$1 [L,QSA]Then make articles.php use $_GET["id"] to decide what article to show. Quote Link to comment Share on other sites More sharing options...
Fluoresce Posted October 22, 2013 Author Share Posted October 22, 2013 Use URL rewriting so you don't have to do this work yourself. For example, with Apache, mod_rewrite, and a .htaccess you do RewriteEngine on RewriteRule ^articles/(\d+)$ articles.php?id=$1 [L,QSA]Then make articles.php use $_GET["id"] to decide what article to show. Thanks. I've already done that. This is something different. How can I get the code to execute no matter what appears after /articles/? Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 22, 2013 Share Posted October 22, 2013 (edited) One way would be preg_match if(peg_match('#/articles/\d+/?#', $requestURI) { // do something } The above should match /articles/1 or /articles/102356 But if your using mod_rewrite then just check if the page id exists (refer to requinix example) if(isset($_GET['id'])) { // do something } Edited October 22, 2013 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
Solution kicken Posted October 22, 2013 Solution Share Posted October 22, 2013 (edited) There is no wildcard character. You have to change your condition to either substring out a prefix and match that to '/articles/' or use preg_match and a regex to test the value. eg: if (substr($requestURI, 0, 10) == '/articles/')or if (preg_match('#^/articles/\d+#', $requestURI)) Edited October 22, 2013 by kicken Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted October 22, 2013 Share Posted October 22, 2013 (edited) Lots of ways depending on what you want to achieve overall. Here are two: $requestURI = $_SERVER['REQUEST_URI']; $something = basename($requestURI); if($something == '/articles') { // do something } if(preg_match('#/articles/\d+#', $requestURI)) { // do something } EDIT: I'm late today. Edited October 22, 2013 by AbraCadaver Quote Link to comment Share on other sites More sharing options...
Fluoresce Posted October 22, 2013 Author Share Posted October 22, 2013 I thank you all very much! Since different variations of preg_match() were presented and I don't understand that function yet, I decided to use this: if(substr($requestURI, 0, 10) == '/articles/') { // Do something } Quote Link to comment 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.