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. Link to comment https://forums.phpfreaks.com/topic/283196-is-there-a-php-character-placeholder/ 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. Link to comment https://forums.phpfreaks.com/topic/283196-is-there-a-php-character-placeholder/#findComment-1454985 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/? Link to comment https://forums.phpfreaks.com/topic/283196-is-there-a-php-character-placeholder/#findComment-1454991 Share on other sites More sharing options...
Ch0cu3r Posted October 22, 2013 Share Posted October 22, 2013 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 } Link to comment https://forums.phpfreaks.com/topic/283196-is-there-a-php-character-placeholder/#findComment-1454995 Share on other sites More sharing options...
kicken Posted October 22, 2013 Share Posted October 22, 2013 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)) Link to comment https://forums.phpfreaks.com/topic/283196-is-there-a-php-character-placeholder/#findComment-1454996 Share on other sites More sharing options...
AbraCadaver Posted October 22, 2013 Share Posted October 22, 2013 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. Link to comment https://forums.phpfreaks.com/topic/283196-is-there-a-php-character-placeholder/#findComment-1454997 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 } Link to comment https://forums.phpfreaks.com/topic/283196-is-there-a-php-character-placeholder/#findComment-1455004 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.