Welcome to WebmasterWorld Guest from 54.147.63.124

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

preg match more than 1 subject

     
7:42 pm on Dec 17, 2008 (gmt 0)

Junior Member

5+ Year Member

joined:Feb 6, 2008
posts:94
votes: 0


is it possible to have more than one subject like:

if(preg_match($pattern, $subject ¦¦ $subject2 ¦¦ $subject3)) {
//result
}
8:11 pm on Dec 17, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 12, 2005
posts:5966
votes: 0


No it is not. You'd have to have multiple preg-matches:

if(preg_match(..) ¦¦ preg_match(..) ¦¦ preg_match(..))
12:47 pm on Dec 18, 2008 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12533
votes: 0


If it is the same regular expression, loop over your subjects ...
$subjects = array( 
'My favorite pet is a cat.',
'My favorite pet is a snake.',
'My favorite pet is a bird.'
);
$pattern = '/\s([a-z]+)\.$/';
print '<pre>';
foreach ($subjects as $subject) {
if (preg_match($pattern, $subject, $matches)) {
print "Do you have a {$matches[1]}?\n";
}
}
print '</pre>';