468,104 Members | 1,308 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,104 developers. It's quick & easy.

finding multiples instances of an expression

Im trying to expand a search util by uing regular expression to allow
common search criteria such as +-* and phrases "".

My understanding of ereg(string pattern, string string, [array
registers]) is that the array register should collect all instances
that match the pattern.

Heres is an example of the code:

$word4 = "+budgie +ferret dog."; //set phrase

$regexp = "\+[0-9A-Za-z]+"; //assign pattern

if(ereg($regexp, $word4, $reg)){ //test for true result
for($i=0;$i<count($reg);$i++){ //loop through array result
echo "<b>finding multiple instances.:</b> Found string '$reg
[$i]'<br>"; // echo result
}
}else{
echo "<b>finding multiple instances.:</b> $regexp not found"; //
echo if none found
}

This is succesfully finding the first word but id does not find the
second word.

Can anyone point to where im going wrong?

Regards
Tristan
Jul 17 '05 #1
1 1830
Tristan wrote:
This is succesfully finding the first word but id does not find the
second word.

Can anyone point to where im going wrong?


This will work better:

<?php

$word4 = "+budgie +ferret dog."; //set phrase
$regexp = "/(\+[a-z\d]+)+/i"; //assign pattern

if (preg_match_all($regexp, $word4, $matches)) {
foreach($matches[1] as $match) {
print "Word found: $match\n";
}
} else {
print "No match";
}
?>
JW

Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

21 posts views Thread by André | last post: by
3 posts views Thread by Tim | last post: by
4 posts views Thread by sathyashrayan | last post: by
6 posts views Thread by Derek Peschel | last post: by
22 posts views Thread by Simon Forman | last post: by
275 posts views Thread by Astley Le Jasper | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.