Results 1 to 11 of 11

Thread: preg_match_all, and whitespace

  1. #1

    Thread Starter
    Member MasterEvilAce's Avatar
    Join Date
    Jul 2002
    Posts
    60

    preg_match_all, and whitespace

    How do I make this code work no matter how much space is inbetween the html tags?
    Code:
    $template = '<table cellspacing="0" cellpadding="0" border="0" width="100%"><tr><td><h1>(.*)</h1></td><td align="right">&nbsp;</td></tr></table>';
    preg_match_all("|".$template."|U", $copy, $system);
    Doesn't work if the text appears like
    Code:
    <table cellspacing="0" cellpadding="0" border="0" width="100%">
    <tr>
    <td><h1>BLAH HAHA!</h1></td>
    <td align="right">&nbsp;</td>
    </tr>
    </table>
    I'd like to keep the PHP on a single line, just so it takes up less space

  2. #2
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Did you try using the 'x' modifier? I believe that ignores all whitespace.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  3. #3

    Thread Starter
    Member MasterEvilAce's Avatar
    Join Date
    Jul 2002
    Posts
    60
    :/

    tried it, but it doesn't change anything

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Then you'll have to change the template. I think \w matches a whitespace, so put \w* everywhere where there might be whitespace. And that's a lot of places

    It's not pretty.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Member MasterEvilAce's Avatar
    Join Date
    Jul 2002
    Posts
    60
    ok, got the problem figured out and all...


    but.. using preg_match_All, it's weird..

    $img[1][1]
    $img[1][2]
    $img[1][3]

    Grabs correctly, but it SKIPS the first instance of the found string..
    (Say i'm looking for the word "Hi", I only need that word 3 times.. but there are only 3 instances of the word, in the text.

    The FIRST two variables will be "Hi".. the third will be EMPTY. It's skipping the first instance of the string to be found..


    if I do:
    $img[1][0]
    $img[1][1]
    $img[1][2]
    The LAST two are "Hi".. but the first one is a WHOLE LOT more text than I specified to search for.. but the second variable *IS* the second instance of "Hi" and the third IS the THIRD instance of "Hi"

    So.. something is really weird.. and I have no idea what's wrong with it.... Any suggestions?

    BTW: I'm using non-greedy statements (matches as little text as possible)

  6. #6

    Thread Starter
    Member MasterEvilAce's Avatar
    Join Date
    Jul 2002
    Posts
    60
    when i do
    $img[1][0]

    it seems to be grabbing TOO MUCH.
    but I have the greediness OFF... but it's still grabbing too much

    i don't understand.. in my expression, are there any non-literal characters that i forgot to escape?

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Arrays start with 0. But for regexp matches, [0] is the whole input string. What about $img[0][n] ?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8

    Thread Starter
    Member MasterEvilAce's Avatar
    Join Date
    Jul 2002
    Posts
    60
    $img[1][0] -- Too much.. or whatever..
    $img[1][1] -- Is actually the SECOND image on the page
    $img[1][2] -- Is the THIRD image on the page

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You sure your expression captures the first image?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10
    New Member
    Join Date
    Jan 2004
    Posts
    13
    To keep a string on a single line use this...

    PHP Code:
    Function sameLine($string)
        {
            
    $string str_replace("\n","",str_replace("\r","",$string));
            return 
    $string;
        } 

  11. #11

    Thread Starter
    Member MasterEvilAce's Avatar
    Join Date
    Jul 2002
    Posts
    60
    Originally posted by CornedBee
    You sure your expression captures the first image?
    yes, most definitely, it does...

    All the images are setup the same, on the pages i'm grabbing the html from...


    To keep a string on a single line use this...
    I use something similar, and so yes, everything is on a single line, but the code still seems to capture too much, the first time..

Posting Permissions

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



Click Here to Expand Forum to Full Width