james2008 Posted February 7, 2011 Share Posted February 7, 2011 Hi, I'm trying to do a preg_match on a url against an entry in the database. Its easiest to explain using an example: A user enters the following path : dells/plan/treats There is an entry in the database : */plan/treats I have a variable set by replacing the "*" with "([\w]+)", from the database entry: $url_reg = "([\w]+)/plan/treats" I'm trying to do a pregmatch against the $path and the $url_reg, which should be returning true: $url_reg = "([\w]+)/plan/treats"; $path = "dells/plan/treats"; $found = preg_match($url_reg, $path,$match); $found is returning false. Can anyone see what I'm doing wrong? Thanks James Quote Link to comment https://forums.phpfreaks.com/topic/226989-preg_match-not-sure-what-im-doing-wrong/ Share on other sites More sharing options...
sasa Posted February 7, 2011 Share Posted February 7, 2011 change [\w] to \w in pattern remove [ and ] Quote Link to comment https://forums.phpfreaks.com/topic/226989-preg_match-not-sure-what-im-doing-wrong/#findComment-1171112 Share on other sites More sharing options...
james2008 Posted February 7, 2011 Author Share Posted February 7, 2011 Thanks for your reply. I'm still doing something wrong though. I've tried the following and still getting a false responce: 1. url_reg = (\w+)/plan/treats 2. url_reg = (\w)/plan/treats 3. url_reg = \w+/plan/treats 4.url_reg = \w/plan/treats Quote Link to comment https://forums.phpfreaks.com/topic/226989-preg_match-not-sure-what-im-doing-wrong/#findComment-1171119 Share on other sites More sharing options...
sasa Posted February 7, 2011 Share Posted February 7, 2011 try <?php $url_reg = "~(\w+)/plan/treats~"; $path = "dells/plan/treats"; echo $found = preg_match($url_reg, $path,$match); ?> Quote Link to comment https://forums.phpfreaks.com/topic/226989-preg_match-not-sure-what-im-doing-wrong/#findComment-1171123 Share on other sites More sharing options...
james2008 Posted February 7, 2011 Author Share Posted February 7, 2011 Thanks a million! That works, I'm going to work through it now so I have a good understanding of what's going on. Quote Link to comment https://forums.phpfreaks.com/topic/226989-preg_match-not-sure-what-im-doing-wrong/#findComment-1171124 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.