Jump to content

preg_match array?


jackpf

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/146313-preg_match-array/#findComment-768157
Share on other sites

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 );

 

 

Link to comment
https://forums.phpfreaks.com/topic/146313-preg_match-array/#findComment-768391
Share on other sites

 

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";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/146313-preg_match-array/#findComment-768393
Share on other sites

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? :D

 

Thanks,

Jack.

Link to comment
https://forums.phpfreaks.com/topic/146313-preg_match-array/#findComment-768417
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.