By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,985 Members | 2,877 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,985 IT Pros & Developers. It's quick & easy.

finding multiples instances of an expression

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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.