$str = '<ul class="msgtitlelist linelist articlelist">
<li><cite>10-07-08 </cite><a href="http://www.autowo.com/index.php/viewnews-98466.html" target="_blank">坐着"中巴"去旅游 哈弗M2全新跨界设计</a></li></ul>';
 eregi("<ul class=\"msgtitlelist linelist articlelist\">(.*)<\/ul>",$str,$b);
 preg_match_all("/<ul class=\"msgtitlelist linelist articlelist\">(.*)<\/ul>/im",$str,$a);
 print_r($a);
 print_r($b);同一字符串,用eregi可以匹配但是用preg_match不能。后来改动过程中发现是因为字符串中有换行的缘故。请教下要怎么写正则才能匹配这个有换行的字符串呢?

解决方案 »

  1.   

    preg_match_all("/<ul class=\"msgtitlelist linelist articlelist\">(.*)<\/ul>/is",$str,$a);
      

  2.   

    摘自PHP手册:m (PCRE_MULTILINE) 
    By default, PCRE treats the subject string as consisting of a single "line" of characters (even if it actually contains several newlines). The "start of line" metacharacter (^) matches only at the start of the string, while the "end of line" metacharacter ($) matches only at the end of the string, or before a terminating newline (unless D modifier is set). This is the same as Perl. When this modifier is set, the "start of line" and "end of line" constructs match immediately following or immediately before any newline in the subject string, respectively, as well as at the very start and end. This is equivalent to Perl's /m modifier. If there are no "\n" characters in a subject string, or no occurrences of ^ or $ in a pattern, setting this modifier has no effect. 所以要改为:preg_match_all("/^.*<ul class=\"msgtitlelist linelist articlelist\">(.*)<\/ul>/im",$str,$a);
      

  3.   

    preg_match_all("/<ul class=\"msgtitlelist linelist articlelist\">(.*)<\/ul>/is",$str,$a);
    用这个可以,太感谢了