homepage Welcome to WebmasterWorld Guest from 174.129.76.87
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
preg match more than 1 subject
tr8er8

5+ Year Member



 
Msg#: 3809741 posted 7:42 pm on Dec 17, 2008 (gmt 0)

is it possible to have more than one subject like:

if(preg_match($pattern, $subject ¦¦ $subject2 ¦¦ $subject3)) {
//result
}

 

eelixduppy

WebmasterWorld Senior Member eelixduppy us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 3809741 posted 8:11 pm on Dec 17, 2008 (gmt 0)

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

if(preg_match(..) ¦¦ preg_match(..) ¦¦ preg_match(..))

coopster

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



 
Msg#: 3809741 posted 12:47 pm on Dec 18, 2008 (gmt 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>';

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved