Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2003
    Posts
    878

    Question Quick Question:: PHP preg Regular Pattern

    Hi gurus,
    need a fast tip this round!!

    My Question is: (example)
    PHP Code:
    $raw_data='blah_blah<a>xyz</a>blah_blah<a>uvw</a>blah_blah'
    Using this pattern:
    "/<a>.*<\/a>/"
    by preg_match_all will capture the whole bunch into $matches like that:
    <a>xyz</a>blah_blah<a>uvw</a>

    Can enlighten me the correct pattern to capture:
    <a>xyz</a> and then <a>uvw</a> separately into $matches??

    Many ths!!

  2. #2
    Join Date
    Mar 2003
    Posts
    878
    No 1??.....
    i tried with pattern modifier U to make it "Ungreedy" and seems to work!! Am i on the right track??

  3. #3
    Join Date
    Apr 2003
    Posts
    55
    my friend said try this...

    #<a>([^<].*)</a>#i

  4. #4
    Join Date
    Mar 2003
    Posts
    878
    Originally posted by cis4life
    my friend said try this...

    #<a>([^<].*)</a>#i
    you mean like this:
    PHP Code:
    preg_match_all("#<a>([^<].*)</a>#i",$raw_data,$matches); 
    I tried it...
    Nope! it captured the whole chunk again into 1st $matches:
    <a>xyz</a>blah_blah<a>uvw</a>
    and less <a></a>(front and back) into 2nd $matches

    btw, whats the diff. between # and / in pattern??

  5. #5
    Join Date
    Jul 2003
    Location
    Kuwait
    Posts
    5,099
    Try

    PHP Code:
    preg_match_all("|<a>(.*?)</a>|i",$raw_data,$matches); 
    The #, / and | in my example are delimeters. Each patterns needs one at the start and the end, and they must be the same character. The i makes the pattern case insensitive.
    In order to understand recursion, one must first understand recursion.
    If you feel like it, you can read my blog
    Signal > Noise

  6. #6
    Join Date
    Jan 2003
    Location
    Perth, WA, Australia
    Posts
    1,276
    Using /U is the same as putting .*? rather than .*, except it does it automatically for every instance. I tend to use the ? though, as I may need it to be greedy in other parts.
    nu-metal.org :: coming soon

  7. #7
    Join Date
    Mar 2003
    Posts
    878
    Originally posted by fyrestrtr
    The #, / and | in my example are delimeters. Each patterns needs one at the start and the end, and they must be the same character. The i makes the pattern case insensitive.
    thanks!!
    now i know the delimiter is NOT limited to just /, but all other non-alpha-numberic letter as well!!

  8. #8
    Join Date
    Mar 2003
    Posts
    878
    Originally posted by fyrestrtr
    Try
    PHP Code:
    preg_match_all("|<a>(.*?)</a>|i",$raw_data,$matches); 
    Originally posted by digitok
    Using /U is the same as putting .*? rather than .*, except it does it automatically for every instance. I tend to use the ? though, as I may need it to be greedy in other parts.
    Thanks guys!! that works like a charm!!

    btw, can elaborate on this::

    .*?

    . stands for any character, less "\n"
    * stands for 0 or many.
    ? stands for 1 or none.

    But when you insert ? behind .* , care to explain, in plain english, why it works in the above application, to "Ungreedy" the capturing??

    sorry if i sounds abit nerd... merely want have some firm grips on this pattern matching understanding better

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •