Monkuar Posted February 25, 2012 Share Posted February 25, 2012 $string = "[img]"; $paragraph = $text; if (preg_match_all($string, $paragraph, $matches)) { echo count($matches[0]) . " matches found"; exit; }else { echo "match NOT found"; exit; } okay, if I use it does not find a match, how do I make it so it's not case sensitive? Quote Link to comment https://forums.phpfreaks.com/topic/257729-help-preg_retch/ Share on other sites More sharing options...
requinix Posted February 25, 2012 Share Posted February 25, 2012 preg_match does much more than just look for a simple string. It requires you to actually learn how to use the function rather than just throw stuff at it. if (preg_match_all("/" . preg_quote($string) . "/i", $paragraph, $matches)) { Quote Link to comment https://forums.phpfreaks.com/topic/257729-help-preg_retch/#findComment-1320989 Share on other sites More sharing options...
Monkuar Posted February 25, 2012 Author Share Posted February 25, 2012 preg_match does much more than just look for a simple string. It requires you to actually learn how to use the function rather than just throw stuff at it. if (preg_match_all("/" . preg_quote($string) . "/i", $paragraph, $matches)) { doesn't /i mean case sensitive? why does preg_quote need to be used? (just curious) im trying to learn Quote Link to comment https://forums.phpfreaks.com/topic/257729-help-preg_retch/#findComment-1320992 Share on other sites More sharing options...
requinix Posted February 25, 2012 Share Posted February 25, 2012 Good, because I didn't want to try explaining things if you didn't care about it (no offense, I'm just lazy). You're looking for the exact string "" (case-insensitive). Regular expressions are overkill for that, but you need a count of matches and there is no built-in PHP function that can do that*. So now you have to create a regular expression meaning "literally ''". Two things: expressions need delimiters and [ and ] are special characters. What you had before "worked" because preg_match() accepts a [] pair as delimiters, but that meant you were actually only searching for "img". If you had the string To use img tags use "[img]". there would have been two matches. So the slashes in the expression are the new delimiters, preg_quote() turns an arbitrary string into something safe for regular expressions (turning the [ and ] into \[ and \]), and the /i flag means case-insensitive. Now that you're in the Regex forum, take a look at the stickies we have here. * substr_count() is case-sensitive. Quote Link to comment https://forums.phpfreaks.com/topic/257729-help-preg_retch/#findComment-1320996 Share on other sites More sharing options...
Monkuar Posted February 25, 2012 Author Share Posted February 25, 2012 Good, because I didn't want to try explaining things if you didn't care about it (no offense, I'm just lazy). You're looking for the exact string "" (case-insensitive). Regular expressions are overkill for that, but you need a count of matches and there is no built-in PHP function that can do that*. So now you have to create a regular expression meaning "literally ''". Two things: expressions need delimiters and [ and ] are special characters. What you had before "worked" because preg_match() accepts a [] pair as delimiters, but that meant you were actually only searching for "img". If you had the string To use img tags use "[img]". there would have been two matches. So the slashes in the expression are the new delimiters, preg_quote() turns an arbitrary string into something safe for regular expressions (turning the [ and ] into \[ and \]), and the /i flag means case-insensitive. Now that you're in the Regex forum, take a look at the stickies we have here. * substr_count() is case-sensitive. Hmm, thanks for explaining! Helps alot, then just giving code, will help me in the future also. Coulda sworn the /i meant case sensitive, ( wouldn't of worked then) I guess for regex it's a whole different ballgame and /i makes it work for that particular string, thanks Req. Quote Link to comment https://forums.phpfreaks.com/topic/257729-help-preg_retch/#findComment-1320997 Share on other sites More sharing options...
requinix Posted February 25, 2012 Share Posted February 25, 2012 Coulda sworn the /i meant case sensitive Case-insensitive Quote Link to comment https://forums.phpfreaks.com/topic/257729-help-preg_retch/#findComment-1321012 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.