Jump to content

preg_match array?


jackpf

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.

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

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

Link to comment
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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