glenelkins Posted October 19, 2009 Share Posted October 19, 2009 hi i maybe have a string like "hellohellobyehello" i want to match for "hello" so use: preg_match ( '%hello%', $string ); but how would i return true if the string contains only 1 occurance of "hello". so in this example it would be false because there are 3 "hello" in there Quote Link to comment https://forums.phpfreaks.com/topic/178193-match-once/ Share on other sites More sharing options...
Garethp Posted October 19, 2009 Share Posted October 19, 2009 preg_match_all("~hello~", $String, $Temp); if(sizeof($Temp[0]) != 1) { //fail } Quote Link to comment https://forums.phpfreaks.com/topic/178193-match-once/#findComment-939511 Share on other sites More sharing options...
glenelkins Posted October 19, 2009 Author Share Posted October 19, 2009 ok all well and good but the string and pattern will have more than just in my example to macth. it may have a string like: '1234hellobyedonttalktome' pattern may be '%1234hello%'; only want to make sure the string has 1 occurance of the "hello" but must start with 1234 Quote Link to comment https://forums.phpfreaks.com/topic/178193-match-once/#findComment-939517 Share on other sites More sharing options...
cags Posted October 19, 2009 Share Posted October 19, 2009 If you need to match multiple things you might have wanted to mention that initially. With regular expressions, theres no point in saying I want to match this, getting a pattern then saying oh it must also match this... the chances are the pattern will be changed completely. It's important to know from the outset exactly what needs to be matched. Having said that, in your particular example you're probably better off using multiple patterns. Also using the information provided so far your probably (IMHO) better off using str functions rather than regex. if(substr($input, 0, 4) == '1234' && substr_count($input, 'hello') == 1) { Quote Link to comment https://forums.phpfreaks.com/topic/178193-match-once/#findComment-939571 Share on other sites More sharing options...
Garethp Posted October 19, 2009 Share Posted October 19, 2009 preg_match_all("~hello~", $String, $Temp); if(preg_match("^1234", $String) && sizeof($Temp[0]) != 1) Quote Link to comment https://forums.phpfreaks.com/topic/178193-match-once/#findComment-939587 Share on other sites More sharing options...
cags Posted October 19, 2009 Share Posted October 19, 2009 preg_match_all("~hello~", $String, $Temp); if(preg_match("^1234", $String) && sizeof($Temp[0]) != 1) Whilst that would also work (assuming you remembered the delimeters on the preg_match call) it would be slower. Quote Link to comment https://forums.phpfreaks.com/topic/178193-match-once/#findComment-939595 Share on other sites More sharing options...
thebadbad Posted October 19, 2009 Share Posted October 19, 2009 FYI, preg_match_all() returns the number of full pattern matches. Quote Link to comment https://forums.phpfreaks.com/topic/178193-match-once/#findComment-939615 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.