Regex matching multiline string failed.

Discussion in 'PHP' started by ads2help, Nov 3, 2008.

  1. #1
    $string ="<mytag>hhsfasfgia
    fdhftgh</mytag>";
    
    // the solution below was taken from a forum
    // with or without this line doesn't make any difference
    // some kind of windows and linux's difference in multiline
    // $string = str_replace("\r\n","\n",$string);
    
    $string = preg_match_all("/(<([\w]+)[^>]*>)(.*)(<\/\\2>)/m",$string,$match);
    print '<pre>';
    print_r($match);
    print '</pre>';
    
    PHP:
    As u can see multiline mode is already ON but still it can't match the string. If i change the string to :

    $string ="<mytag>hhsfasfgiafdhftgh</mytag>";
    PHP:
    It can match !

    Any idea? Thank you.
     
    ads2help, Nov 3, 2008 IP
  2. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $string = preg_match_all("/(<([\w]+)[^>]*>)(.*)(<\/\\2>)/s",$string,$match);
    PHP:
     
    JAY6390, Nov 3, 2008 IP
    ads2help likes this.
  3. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #3
    Thanks, Jay! It works.

    But i have a question, isn't m represents multi-line mode? and s represents single-line mode?

    How come it works in single line mode?

    Thank you anyway, +rep
     
    ads2help, Nov 3, 2008 IP
  4. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #4
    JAY6390, Nov 4, 2008 IP