NeoMarine Posted December 27, 2007 Share Posted December 27, 2007 How do I specify in preg_match that I want a match only for: 4/ or /4/ The first "/" is optional. This is what I've tried: preg_match("/\/?4\/?/", $string) ...only the number "4" is found even if it's "54" or "45". I need to say NO characters other than / before or after the number 4. Please do help my mind can't handle this silly simple problem hehe Quote Link to comment Share on other sites More sharing options...
dsaba Posted December 27, 2007 Share Posted December 27, 2007 How do I specify in preg_match that I want a match only for: 4/ or /4/ The first "/" is optional. ~(|/)[0-9]~ tested: http://nancywalshee03.freehostia.com/regextester/regex_tester.php?seeSaved=6kkwtc3i *edit woops change that to: ~(|/)[0-9]/~ //I forgot to add the mandatory slash at the end Quote Link to comment Share on other sites More sharing options...
NeoMarine Posted December 27, 2007 Author Share Posted December 27, 2007 Ok maybe I confused you a little with my question. What I mean is: $string = "4/5/2/3/4/6/1416/647/4/0/90/" preg_match("/\/?7\/?/", $string) I want to find ONLY 7 exists in this case. Now, 7 exists in $string but in 647 which I want it to IGNORE. I only want to find it if the string had it in there like this: "8/5/7/" where 7 is between / and / OR if 7 was at the start of the string, it is like this: "7/4/2/" where there's no / in front this time. Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 27, 2007 Share Posted December 27, 2007 your regexp should be <?php $string = "4/5/2/3/4/6/1416/647/4/0/90/"; preg_match("~(/7/|7/|/7)?~", $string); ?> Quote Link to comment Share on other sites More sharing options...
NeoMarine Posted December 27, 2007 Author Share Posted December 27, 2007 That doesn't work correctly. It will find the 7/ in the 647/ substring. I need it to only find either "7/" or "/7/". If there's anything before "7/", such as "647/" then it should return false (no match). Also... when testing it appears to always find true... well... I maybe this is a little more tricky than it seems? Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 27, 2007 Share Posted December 27, 2007 sorry wrong expression should of been $string = "4/5/2/3/4/6/1416/647/4/0/90/"; preg_match("~\b7\b~", $string); Quote Link to comment Share on other sites More sharing options...
NeoMarine Posted December 27, 2007 Author Share Posted December 27, 2007 Perfect - T H A N K Y O U !! 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.