preg_match_all help needed

Discussion in 'PHP' started by deriklogov, Nov 22, 2008.

  1. #1
    Hello,

    Could you please explain why this part doesnt work:

    $bg = preg_match_all("/<TD width=\"5\"><IMG src=\"images\/blue_box_R.gif\" width=5 height=24><\/TD>(.*)<td><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">/U", $str, $res3);


    I am trying to extract information between <TD width=\"5\"><IMG src=\"images\/blue_box_R.gif\" width=5 height=24><\/TD> and <td><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">

    but because that part which have to be extracted contains is a description including html tags I am having problem to extract it, what can I do about that ?

    thank you
     
    deriklogov, Nov 22, 2008 IP
  2. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #2
    Add an "s" pattern modifier if that ".*" will ever contain a newline. You might also want to add an "i" flag if you don't want upper/lower case letters to make a difference.
    http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php

    You should get in the habbit of using a pattern delimiter that suits your pattern. For instance you're using a forward slash in your pattern but not a pound symbol (#) so a pound would make a better delimiter so you don't have to escape characters you don't need to escape.
    Same goes for the quotes. Since you're not using apostrophes (single quotes) or variables in your pattern, it makes more sense to wrap your pattern with single quotes instead of double quotes so you don't have to escape all of the quotes in your pattern.
     
    joebert, Nov 22, 2008 IP
  3. deriklogov

    deriklogov Well-Known Member

    Messages:
    1,080
    Likes Received:
    22
    Best Answers:
    0
    Trophy Points:
    130
    #3
    to joebert:

    Thank you , after I added i and s to the end that pattern start working in tool to check patterns (working fine)
    but it doesnt work in php script:

    /Product Details<\/TD>
    <TD width=\"5\"><IMG src=\"images\/blue_box_R.gif\" width=5 height=24><\/TD>
    <\/tr>
    <tr>
    <td colspan=3 valign=top class=\"medium\">(.*)<\/tr>
    <\/table>
    <\/td>
    <\/tr>
    <tr>/Uis



    I think its not working in php because of those so many new lines and tabs , what I can do about that ?
    pattern have been copied from html where those new lines and tabs present
     
    deriklogov, Nov 22, 2008 IP
  4. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #4
    Replace the newlines and tabs in the pattern with \s*
     
    joebert, Nov 22, 2008 IP
  5. deriklogov

    deriklogov Well-Known Member

    Messages:
    1,080
    Likes Received:
    22
    Best Answers:
    0
    Trophy Points:
    130
    #5
    Do I have to put \s* for every new line and every tab or just between spots (tags) ?
     
    deriklogov, Nov 22, 2008 IP
  6. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #6
    If you have a newline followed by two tabs, replace all 3 characters with a single \s*
    If you have two nelines in a row, replace them with a single \s*

    Basicly what \s* means is "whitespace such as tabs, spaces, and newlines zero or more times".

    '#Product Details</TD>\s*<TD width="5"><IMG src="images/blue_box_R.gif" width=5 height=24></TD>\s*</tr>\s*<tr>\s*<td colspan=3 valign=top class="medium">(.*)</tr>\s*</table>\s*</td>\s*</tr>\s*<tr>#Uis'
    Code (markup):
     
    joebert, Nov 22, 2008 IP
  7. deriklogov

    deriklogov Well-Known Member

    Messages:
    1,080
    Likes Received:
    22
    Best Answers:
    0
    Trophy Points:
    130
    #7
    Thank You Very Much
    All perfectly working

     
    deriklogov, Nov 22, 2008 IP