finding multiples instances of an expression

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

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
  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?


Re: finding multiples instances of an expression

Tristan wrote:
Quoted text here. Click to load it

This will work better:


$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";



Site Timeline