finding multiples instances of an expression

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tristan

    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<cou nt($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
  • Janwillem Borleffs

    #2
    Re: finding multiples instances of an expression

    Tristan wrote:[color=blue]
    > This is succesfully finding the first word but id does not find the
    > second word.
    >
    > Can anyone point to where im going wrong?
    >[/color]

    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($matche s[1] as $match) {
    print "Word found: $match\n";
    }
    } else {
    print "No match";
    }


    ?>


    JW



    Comment

    Working...