preg_match VS preg_match_all

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

    preg_match VS preg_match_all

    I'm wondering if someone can explain why the following works with
    preg_match_all, but not preg_match:

    $html = "product=345678 9&"

    preg_match_all ("|product=(\d{ 5,10})&|i", $html, $out);

    $out[1][0] = 3456789

    preg_match ("|product=(\d{ 5,10})&|i", $html, $out);

    $out[1][0] = 3

    For some reason, preg_match only returns the first character of the match.
    Is this by design or does the regexp pattern need to be modified?

    I'm curious to know what the difference is.

    Thanks in advance.


  • Pedro

    #2
    Re: preg_match VS preg_match_all

    Han wrote:[color=blue]
    > I'm wondering if someone can explain why the following works with
    > preg_match_all, but not preg_match:
    >
    > $html = "product=345678 9&"
    >
    > preg_match_all ("|product=(\d{ 5,10})&|i", $html, $out);
    >
    > $out[1][0] = 3456789
    >
    > preg_match ("|product=(\d{ 5,10})&|i", $html, $out);
    >
    > $out[1][0] = 3
    >
    > For some reason, preg_match only returns the first character of the match.
    > Is this by design or does the regexp pattern need to be modified?
    >
    > I'm curious to know what the difference is.[/color]

    It is by design!

    preg_match() returns the first and *ONLY* the first match.
    preg_match_all( ) returns an array with *ALL* the matches.

    after your preg_match()
    $out[1] = 3456789
    $out[1][0] = 3
    $out[1][1] = 4
    $out[1][2] = 5
    ...
    the second index of $out[1] represents the character index inside the
    string, $out[1] is the first match of the first set of ( )

    --
    I have a spam filter working.
    To mail me include "urkxvq" (with or without the quotes)
    in the subject line, or your mail will be ruthlessly discarded.

    Comment

    • Han

      #3
      Re: preg_match VS preg_match_all

      Ah, dang it. I was so close, but got hung up on preg_match_all syntax.

      Thanks for the clarification.

      "Pedro" <hexkid@hotpop. com> wrote in message
      news:blnape$e05 os$1@ID-203069.news.uni-berlin.de...[color=blue]
      > Han wrote:[color=green]
      > > I'm wondering if someone can explain why the following works with
      > > preg_match_all, but not preg_match:
      > >
      > > $html = "product=345678 9&amp;"
      > >
      > > preg_match_all ("|product=(\d{ 5,10})&amp;|i", $html, $out);
      > >
      > > $out[1][0] = 3456789
      > >
      > > preg_match ("|product=(\d{ 5,10})&amp;|i", $html, $out);
      > >
      > > $out[1][0] = 3
      > >
      > > For some reason, preg_match only returns the first character of the[/color][/color]
      match.[color=blue][color=green]
      > > Is this by design or does the regexp pattern need to be modified?
      > >
      > > I'm curious to know what the difference is.[/color]
      >
      > It is by design!
      >
      > preg_match() returns the first and *ONLY* the first match.
      > preg_match_all( ) returns an array with *ALL* the matches.
      >
      > after your preg_match()
      > $out[1] = 3456789
      > $out[1][0] = 3
      > $out[1][1] = 4
      > $out[1][2] = 5
      > ...
      > the second index of $out[1] represents the character index inside the
      > string, $out[1] is the first match of the first set of ( )
      >
      > --
      > I have a spam filter working.
      > To mail me include "urkxvq" (with or without the quotes)
      > in the subject line, or your mail will be ruthlessly discarded.[/color]


      Comment

      Working...