Jump to content

Archived

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

jackpf

preg_match array?

Recommended Posts

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
int preg_match  ( string $pattern  , string $subject  [, array &$matches  [, int $flags  [, int $offset  ]]] )

 

:'(

 

I've shamed myself, can't even read my own advice

Share this post


Link to post
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 );

 

 

Share this post


Link to post
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","me@me.net");

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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

×
×
  • 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.