jackpf Posted February 22, 2009 Share Posted February 22, 2009 Hi all, Quick question- is there any way to preg_match with an array of matches. Eg: $str = 'abc'; $match = array('a', 'b', 'c'); if(preg_match($match, $str)) { .... I have googled it, to no avail. Any help would be appreciated. Thanks, Jack. Quote Link to comment https://forums.phpfreaks.com/topic/146313-preg_match-array/ Share on other sites More sharing options...
gevans Posted February 22, 2009 Share Posted February 22, 2009 why not look at the preg_match documentation? Quote Link to comment https://forums.phpfreaks.com/topic/146313-preg_match-array/#findComment-768142 Share on other sites More sharing options...
gevans Posted February 22, 2009 Share Posted February 22, 2009 to answer your question, yes you can use an array Quote Link to comment https://forums.phpfreaks.com/topic/146313-preg_match-array/#findComment-768143 Share on other sites More sharing options...
jackpf Posted February 22, 2009 Author Share Posted February 22, 2009 I did!! I couldn't find anything about arrays. I have attempted to research this before posting; I'm not one of those people who post questions simply because I can't be bothered too google it. If there is a way of preg_match-ing arrays, would you be kind enough to give me an example of how to do so? Thanks, Jack. Quote Link to comment https://forums.phpfreaks.com/topic/146313-preg_match-array/#findComment-768157 Share on other sites More sharing options...
.josh Posted February 22, 2009 Share Posted February 22, 2009 No, you cannot use an array for patterns with preg_match. You can only do that with preg_replace. If you have multiple patterns you want to match, you need to loop through your array. Quote Link to comment https://forums.phpfreaks.com/topic/146313-preg_match-array/#findComment-768176 Share on other sites More sharing options...
gevans Posted February 22, 2009 Share Posted February 22, 2009 int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags [, int $offset ]]] ) :'( I've shamed myself, can't even read my own advice Quote Link to comment https://forums.phpfreaks.com/topic/146313-preg_match-array/#findComment-768352 Share on other sites More sharing options...
printf Posted February 22, 2009 Share Posted February 22, 2009 Hi all, Quick question- is there any way to preg_match with an array of matches. Eg: $str = 'abc'; $match = array('a', 'b', 'c'); if(preg_match($match, $str)) { .... I have googled it, to no avail. Any help would be appreciated. Thanks, Jack. Use implode(); with preg_quote() in your preg_match()... $total_found = preg_match ( '/(' . implode ( '|', preg_quote ( $array, '/' ) ) . ')/is', $haystack ); Quote Link to comment https://forums.phpfreaks.com/topic/146313-preg_match-array/#findComment-768391 Share on other sites More sharing options...
redarrow Posted February 22, 2009 Share Posted February 22, 2009 look at this it longer but very understandable. print_f is pseudo code but your get there. <?php $email=array("me@me_.com","_me@m_e.net","[email protected]"); $e=implode(' ',$email); if(preg_match("/[a-z0-9\-\_]{0,50}+@[a-z0-9\_\-]{0,50}\.[a-z]{0,3}$/i",$e)){ echo " correct emails\n $e "; }else { echo "incorrect emails\n $e"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/146313-preg_match-array/#findComment-768393 Share on other sites More sharing options...
jackpf Posted February 22, 2009 Author Share Posted February 22, 2009 Ahh...I think I understand, and thanks for your help guys. Well, basically, what I'm trying to do is allow multiple occurences of bbcode in my forum. so if someone did [box]hello[box]hello[/box][/box] it'd display both boxes. Atm it doesn't because it only checks for my bbcode array once. However, I've gotten around this by using the following: for($i = 0; $i <= 5; $i++) { $return = preg_replace($exist, $replace, $str); } return $return; But this only works for five occurences ( or whatever I set ). What I was trying to do is find out how many matches there are, and then repeat preg_replace() that many times. You guys seem very knowledgeable, do you happen to know how I could do this? Thanks, Jack. Quote Link to comment https://forums.phpfreaks.com/topic/146313-preg_match-array/#findComment-768417 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.