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 Link to comment https://forums.phpfreaks.com/topic/83322-solved-simple-preg_match-question/ 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 Link to comment https://forums.phpfreaks.com/topic/83322-solved-simple-preg_match-question/#findComment-423895 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. Link to comment https://forums.phpfreaks.com/topic/83322-solved-simple-preg_match-question/#findComment-423940 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); ?> Link to comment https://forums.phpfreaks.com/topic/83322-solved-simple-preg_match-question/#findComment-423957 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? Link to comment https://forums.phpfreaks.com/topic/83322-solved-simple-preg_match-question/#findComment-423997 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); Link to comment https://forums.phpfreaks.com/topic/83322-solved-simple-preg_match-question/#findComment-424036 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 !! Link to comment https://forums.phpfreaks.com/topic/83322-solved-simple-preg_match-question/#findComment-424044 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.