preg_match_all

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

    preg_match_all

    Hallo,

    this text:




    Über <a href="[linkto:13]">allen</a> strahlt die <a
    href="[linkto:14]">Sonne</a> – <a href="[linkto:13]">über</a> allen in
    der Welt.

    preg_match_all( '/\[linkto:(.*)\]/',$text,$linkto );
    $num = count($linkto['1']);
    echo $num;


    Why i get only 1 and not 3 results?

    Mark
  • Csaba  Gabor

    #2
    Re: preg_match_all

    Mark Knochen wrote:[color=blue]
    > Über <a href="[linkto:13]">allen</a> strahlt die <a
    > href="[linkto:14]">Sonne</a> - <a href="[linkto:13]">über</a> allenin
    > der Welt.
    >
    > preg_match_all( '/\[linkto:(.*)\]/',$text,$linkto );
    > $num = count($linkto['1']);
    > echo $num;
    >
    > Why i get only 1 and not 3 results?[/color]

    You should have examined the match that was found, and then you would
    have seen: that '.*' is greedy. This means that the first 'linkto:' is
    found and the the regular expression machine tries to use as many
    characters as it can before it encounters the next one, which is the
    **last** ']', hence only one match.

    To get your three matches, you could replace '.*' with '.*?' which
    makes the * non greedy.

    Csaba Gabor from Vienna

    Comment

    Working...