Help with regular expression please

Discussion in 'PHP' started by gerex_2, May 7, 2009.

  1. #1
    Please help I am working for more than 3 hours now this problem but I can't think or find way to correct it...

    my regular expression is like this
    %<h[^>]+>(.*)</h.>%i

    It works perfectly if the string look like this
    <H1><A NAME="SECTION000300000000000000000">2 Simple Patterns</A></H1>
    Code (markup):
    Result : <A NAME="SECTION000300000000000000000">2 Simple Patterns</A></H1>
    But if string will look like this
    
    <H1><A NAME="SECTION000300000000000000000">2 Simple Patterns
    </A>
    </H1>
    
    Code (markup):
    Result : None

    I guess the problem is the line break or not sure..

    Thanks in advance
     
    gerex_2, May 7, 2009 IP
  2. kusal

    kusal Peon

    Messages:
    91
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    try giving m modifiers

    %<h[^>]+>(.*)</h.>%im

    m - Treat string as multiple lines
    i - Case-insensitive match
     
    kusal, May 7, 2009 IP
  3. gerex_2

    gerex_2 Peon

    Messages:
    37
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi Kusal,

    Thanks mate, but still don't work...
     
    gerex_2, May 7, 2009 IP
  4. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #4
    Hi,

    It seems preg_match treat such string as an array and array elements don't match the regexp. Proof:
    
    $s='<H1><A NAME="SECTION000300000000000000000">2 Simple Patterns
    </A>
    </H1>';
    $arr=array();
    preg_match_all('#.*#',$s,&$arr);
    print_r($arr);
    
    PHP:
    So you can replace "\r\n" for the input with "".
    Regards
     
    koko5, May 8, 2009 IP