欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

preg_match_all ok的例子 特殊字符 换行等各种例子 ismU 有大用

shiping1 的头像

//修饰符 特殊字符
 i   (忽略大小写)   
  g   (全文查找出现的所有   pattern)   
  gi   (全文查找、忽略大小写)
  U   表示非贪婪匹配 ,匹配一次就中止了   (U好像是非贪婪匹配) (可以100%的肯定 U的非贪婪匹配)
  m   将一个字符串视为多行   ^ 定位换行符之后的内容, $定位换行符之前的内容
php <wbr>正则中的"i,m,s,x,e"分别表示什么当设定了此修正符,“行起始”和“行结束”除了匹配整个字符串开头和结束外,还分别匹配其中的换行符的之后和之前。
  s 表示 使用 小圆点匹配任意字符 ,包括  换行符 (\n)
\num num应该被赋予一个正数。与已经储存的部分比较。例:"(.)\1"和任意的两个连续的相同
文字一致。


见  /node/1486


i          
如果设定此修正符,模式中的字符将同时匹配大小写字母。

m       
当设定了此修正符,“行起始”和“行结束”除了匹配整个字符串开头和结束外,还分别匹配其中的换行符的之后和之前。

s        
如果设定了此修正符,模式中的圆点元字符(.)匹配所有的字符,包括换行符。没有此设定的话,则不包括换行符。

x         
如果设定了此修正符,模式中的空白字符除了被转义的或在字符类中的以外完全被忽略,在未转义的字符类之外的 #以及下一个换行符之间的所有字符,包括两头,也都被忽略。

e         
如果设定了此修正符,preg_replace() 在替换字符串中对逆向引用作正常的替换,

?在 . + 和 * 之后 表示非贪婪匹配: *、+和?限定符都是贪婪的,因为它们会尽可能多的匹配文字,只有在它们的后面加上一个?就可以实现非贪婪或最小匹配。

例如:

1
2
3
4
5
6
7
8
9
<?php                                    
$string = "上飞机离开我<img border='0' alt='' src='/uploadfile/2009/0921/20090921091612567.jpg' border='0' />sdfsdf";                                    
 
$su = preg_match("/ \<[ ]*img.*src[ ]*\=[ ]*[\"|\'](.+?)[\"|\'] /", $string,$match); // 匹配src=的内容                                    
print_r($match[1]); // 输出 /uploadfile/2009/0921/20090921091612567.jpg                                    
 
$su = preg_match("/ \<[ ]*img.*src[ ]*\=[ ]*[\"|\'](.+)[\"|\'] /", $string,$match);                                    
print_r($match[1]); // 输出 /uploadfile/2009/0921/20090921091612567.jpg' border='                                    
?>                                    

例子:(?i):    

(?i)在PHP中的意思是内部修正符,i指不区分大小写    

其它的修正符还有x,m,s,U等。和我们使用的模式修正符是一样的。

区别在于它是在模式内部使用的。仅作用于(?i)所在的子模式内

   

ccc(a(?i))bcd 匹配 cccabcd和cccAbcd

而a(?i)bc则和abc加上\i修正符是一样的因为(?i)作用于整个模式

后向引用    

对一个正则表达式模式或部分模式两边添加圆括号将导致相关匹配存储到一个临时缓冲区中,所捕获的每个子匹配都按照在正则表达式模式中从左至右所遇到的内容存储。存储子匹配的缓冲区编号从 1 开始,连续编号直至最大 99 个子表达式。每个缓冲区都可以使用 '\n' 访问,其中 n 为一个标识特定缓冲区的一位或两位十进制数。

可以使用非捕获元字符 '?:', '?=', or '?!' 来忽略对相关匹配的保存。


   


   


 




查找匹配的网址
preg_match_all('/<a(.*)href=["\']([^>]+)["\'](.*)>/misU',$content,$out,PREG_PATTERN_ORDER);



1)getImgSrcArr_ok.php
<?php
/*
 * Created on 2012-12-29
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 $content = "<div style=\"text-align: center\">
    <span style=\"font-size: 12px\"><img border=\"0\" src=\"http://www.fzzbxx.com/uploadpic/2010102393514.jpg\" /></span></div>
<div>
    <span style=\"font-size: 12px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;葛建国老师,著名服装结构设计师、高级服装打版师。曾在香港高升服装集团等多家大型公司任要职,是我校服装制版的骨干老师。此图为葛建国老师在教制版课程。</span></div>
<div style=\"text-align: center\">
    <span style=\"font-size: 12px\"><img border=\"0\" src=\"http://www.fzzbxx.com/uploadpic/201111291503231.jpg\" /></span></div>
<div>
    <span style=\"font-size: 12px\">&nbsp;&nbsp;&nbsp;&nbsp;罗楠老师,毕业于苏州大学服装艺术设计系,中国十佳时装设计师,任芭芭拉女装首席设计师以及设计总监。设计追求完美,对每一项工作精益求精力求通过服装将所理解的美展示给大家。</span></div>
<div style=\"text-align: center\">
    <span style=\"font-size: 12px\"><img border=\"0\" src=\"http://www.fzzbxx.com/uploadpic/201154152119.jpg\" />&nbsp;</span></div>
<div>
    <span style=\"font-size: 12px\"><font face=\"Verdana\">王大勇老师,高级服装设计师,南师大服装设计系毕业后在南京、上海、苏州等地多家大型服装企业任设计师或设计总监。图为王大勇老师在讲授服装设计课程。</font></span></div>
<div style=\"text-align: center\">
    <p>
        <span style=\"font-size: 12px\"><font face=\"Verdana\"><img border=\"0\" src=\"http://www.fzzbxx.com/uploadpic/2010102393632.jpg\" /></font></span></p>
    <p style=\"text-align: left\">
        <span style=\"font-size: 12px\"><font face=\"Verdana\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 戴正智,著名服装结构设计师、高级服装打版师。原台湾聚阳集团(上市公司)驻印尼公司技术科长兼首席样版师,对服装工业制版有很深的研究。此图为戴正智老师在教打版。</font></span></p>
    <p>
        <span style=\"font-size: 12px\"><font face=\"Verdana\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font></span><span style=\"font-size: 12px\"><font face=\"Verdana\"><img border=\"0\" src=\"http://www.fzzbxx.com/uploadpic/20101230155538.jpg\" /></font></span></p>
    <p>
        <span style=\"font-size: 12px\"><font face=\"Verdana\"><font face=\"Verdana\">李国锐老师</font>,<font face=\"Verdana\">高级画师、学校骨干教师,在服装素描、服装色彩上有很高的造诣。此图为李国锐老师在讲色彩课程。</font></font></span></p>
    <p>
        <span style=\"font-size: 12px\"><img border=\"0\" src=\"http://www.fzzbxx.com/uploadpic/2010102211215772.jpg\" /></span></p>
    <p style=\"text-align: left\">
        <span style=\"font-size: 12px\">&nbsp;&nbsp;&nbsp;&nbsp;何海军老师,著名服装设计师,原台湾百和集团(上市公司)副总兼设计总监、《新文化教育理念》创始人、服装教育专家。现任我校校长兼北京婷美集团首席设计师何海军先生在上商品规划设计课程。</span></p>
    <p>
        <span style=\"font-size: 12px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <img border=\"0\" src=\"http://www.fzzbxx.com/uploadpic/2010102393431.jpg\" /></span></p>
    <p style=\"text-align: left\">
        <span style=\"font-size: 12px\">&nbsp;&nbsp;&nbsp;&nbsp;刘松老师,著名服装设计师、高级服装打版师、服装教育专家,曾进修过多个国家立裁等课程。曾任苏州雅可集团,江苏千仞岗集团首席制版师。现任我校校长的刘老师在教高级立裁课程。</span></p>
    <p>
        <span style=\"font-size: 12px\"><img border=\"0\" src=\"http://www.fzzbxx.com/uploadpic/2010102393555.jpg\" /></span></p>
    <p style=\"text-align: left\">
        <span style=\"font-size: 12px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 王红兵老师,高级服装结构师、高级服装打版师。曾在上海、苏州等多家大型服装企业任技术科主管,有丰富的制版和教师经验,此图为王老师在讲制版课程。</span></p>
    <p>
        <span style=\"font-size: 12px\"><img border=\"0\" src=\"http://www.fzzbxx.com/uploadpic/201154152216.jpg\" /></span></p>
    <p>
        <span style=\"font-size: 12px\"><font face=\"Verdana\">周伟峰老师,高级服装制版师,曾在苏州福瑞服装企业,上海博慧服饰、上海嘉保服饰公司等多家企业。<br />
        <img border=\"0\" src=\"http://www.fzzbxx.com/uploadpic/2010102394156.jpg\" /></font></span></p>
    <p>
        <span style=\"font-size: 12px\"><font face=\"Verdana\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 夏正玉老师,2001年毕业于服装专业学校服装工程系,曾就职于苏州市雅可集团技术部制版,CAD制版工作。07年受聘就职于我校服装制版系任CAD制版专业教师成功编写《度卡CAD技术操作手册》和《德卡CAD技术操作手册》,提高了教学质量和教学水平,在为培养企业制版师现代化设备操作技能上做出来杰出贡献。</font></span></p>
    <p>
        <span style=\"font-size: 12px\"><font face=\"Verdana\"><img border=\"0\" src=\"http://www.fzzbxx.com/uploadpic/201154152521.jpg\" /></font></span></p>
    <p>
        <span style=\"font-size: 12px\"><font face=\"Verdana\">杨德兵老师,高级服装制版师,曾在苏州,上海,杭州多家服装企业任技术科制版师或主管工作,对制版有很多自己独特的见解。<br />
        <img border=\"0\" src=\"http://www.fzzbxx.com/uploadpic/2010102393708.jpg\" /></font></span></p>
    <p>
        <span style=\"font-size: 12px\"><font face=\"Verdana\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font face=\"Verdana\">王丽娟老师,高级服装结构师、高级服装打版师。曾在江苏阳光集团、海澜之家等服装公司任打版师,是我校结构设计骨干教师。此图为王丽娟老师在教结构设计课程。</font></font></span></p>
</div>
";
 preg_match_all('|<img(.*)src=["\']([^>]+)["\'](.*)\/?>|isU', str_replace("\\", "", $content), $imgs,PREG_PATTERN_ORDER);

 
 var_dump($imgs[2]);
?>

2)preg_match_all_flag.php
<?php
/*
 * Created on 2013-4-17
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
//可选,指定匹配结果放入 matches 中的顺序,可供选择的标记有:
//PREG_PATTERN_ORDER:默认,对结果排序使 $matches[0] 为全部模式匹配的数组,
//$matches[1] 为第一个括号中的子模式所匹配的字符串组成的数组,以此类推
//PREG_SET_ORDER:对结果排序使 $matches[0] 为第一组匹配项的数组,
//$matches[1] 为第二组匹配项的数组,以此类推
//PREG_OFFSET_CAPTURE:如果设定本标记,对每个出现的匹配结果也同时返回其附属的字符串偏移量
?>
PREG_PATTERN_ORDER
结果排序为$matches[0]保存完整模式的所有匹配,
$matches[1] 保存第一个子组的所有匹配,以此类推。
<?php
preg_match_all("|<[^>]+>(.*)</[^>]+>|U",
    "<b>example: </b><div align=left>this is a test</div>",
    $out, PREG_PATTERN_ORDER);
echo $out[0][0] . ", " . $out[0][1] . "\n";
echo $out[1][0] . ", " . $out[1][1] . "\n";
?>
以上例程会输出:
<b>example: </b>, <div align=left>this is a test</div>
example: , this is a test
因此, $out[0]是包含匹配完整模式的字符串的数组, $out[1]是包含闭合标签内的字符串的数组。


PREG_SET_ORDER
结果排序为$matches[0]包含第一次匹配得到的所有匹配(包含子组),
 $matches[1]是包含第二次匹配到的所有匹配(包含子组)的数组,以此类推。
 <?php
preg_match_all("|<[^>]+>(.*)</[^>]+>|U",
    "<b>example: </b><div align=\"left\">this is a test</div>",
    $out, PREG_SET_ORDER);
echo $out[0][0] . ", " . $out[0][1] . "\n";
echo $out[1][0] . ", " . $out[1][1] . "\n";
?>
以上例程会输出:
<b>example: </b>, example:
<div align="left">this is a test</div>, this is a test

3)preg_match_all.php
<?php
//preg_match() 返回 pattern 所匹配的次数。要么是 0 次(没有匹配)或 1 次,
//因为 preg_match() 在第一次匹配之后将停止搜索。
//preg_match_all() 则相反,会一直搜索到 subject 的结尾处。
//如果出错 preg_match() 返回 FALSE。



//内容里面找正则表达式,放在 $imgs数组里
preg_match_all('|<img (.*)src=[\'|"]([^>]+)[\'|"](.*) />|isU', str_replace("\\", "", $content), $imgs); //提取么一张照片作为缩略图
preg_match_all('|<img (.*)src=[\'|"]([^>]+)[\'|"](.*) [/]?>|isU', str_replace("\\", "", $content), $imgs);
preg_match_all('|<a href(.*)</a>|isU', str_replace("\\", "", $content), $imgs); //提取么一张照片作为缩略图
//这个绝对有用
preg_match_all("|<v:note\s+id='(\d+)'\s+count='(\d+)'\s*>(.*)<\/v:note>|ismU",$voteNoteStr,$voteNoteArr,PREG_SET_ORDER);


//$tr 是全部内容  $out是匹配后的输出结果 
// 好像要把?去掉,,,又好像不能去掉 改成  preg_match_all('/<tr>(.*)<\/tr>/im',$tr,$out,PREG_PATTERN_ORDER);

preg_match_all('/<tr>(.*?)<\/tr>/im',$tr,$out,PREG_PATTERN_ORDER);


4)preg_match_all2.php
<?php
  $html="<b>粗体字符</b><h1>这是h1</h1>";
  preg_match("/(<([\w]+)[^>]*>)(.*)(<\/\\2>)/",$html,$matches);
  var_dump($matches);
  //echo count($matches[0]);
  //for($i=0;$i<)
?>

5)preg_match_all5.php
<?php
  $html="<b>粗体字符</b><h1>这是h1</h1>";
  preg_match("/(<([\w]+)[^>]*>)(.*)(<\/\\2>)/",$html,$matches);
  var_dump($matches);
  //echo count($matches[0]);
  //for($i=0;$i<)
?>

6)preg_match_all6_ok.php
<?php

//可选,指定匹配结果放入 matches 中的顺序,可供选择的标记有:
//PREG_PATTERN_ORDER:默认,对结果排序使 $matches[0] 为全部模式匹配的数组,
//$matches[1] 为第一个括号中的子模式所匹配的字符串组成的数组,以此类推
//PREG_SET_ORDER:对结果排序使 $matches[0] 为第一组匹配项的数组,
//$matches[1] 为第二组匹配项的数组,以此类推
//PREG_OFFSET_CAPTURE:如果设定本标记,对每个出现的匹配结果也同时返回其附属的字符串偏移量

//下面是好的
$tr =  "<tr><td width='50%'>aa</td><td width='20%'>ddddd</td><td width='30%'>2011-08-06 11:39:11</td></tr>    
        <tr><td width='50%'>aa</td><td width='20%'>ffff</td><td width='30%'>2011-08-06 11:39:17</td></tr>
        <tr><td width='50%'>aa</td><td width='20%'>aaaaaaaaaaa</td><td width='30%'>2011-08-06 11:39:31</td></tr>
        <tr><td width='50%'>ddd</td><td width='20%'>aa</td><td width='30%'>2011-08-06 11:40:25</td></tr>
        <tr><td width='50%'>我们正在玩</td><td width='20%'>aa</td><td width='30%'>2011-08-06 12:00:47</td></tr>
        <tr><td width='50%'>我们在操作它</td><td width='20%'>aa</td><td width='30%'>2011-08-07 23:38:44</td></tr>
        <tr><td width='50%'>tttt</td><td width='20%'>aa</td><td width='30%'>2011-08-07 23:42:37</td></tr>
        <tr><td width='50%'>测试一下啊啊 啊啊</td><td width='20%'>wanwq</td><td width='30%'>2011-08-09 11:33:17</td></tr>
        <tr><td width='300px' style=\"word-wrap:break-word;\">订单状态更改为已删除</td><td width='20%'>wangjing</td><td width='30%'>2011-09-01 14:11:19</td></tr>
        <tr><td width='300px' style=\"word-wrap:break-word;\">订单状态更改为审核通过</td><td width='20%'>aa</td><td width='30%'>2011-09-01 14:51:15</td></tr>
    <tr><td width='300px' style=\"word-wrap:break-word;\">订单状态更改为审核通过</td><td width='20%'>aa</td><td width='30%'>2011-09-01 14:58:22</td></tr>
    <tr>
        <textarea name=\"oprateLog\" cols=\"87\" rows=\"8\"></textarea><br>
        <div align=\"center\"><input type=\"button\" name=\"saveLog\" value=\"保存操作日志\"
            onclick=\"log_Submit('jj_yuyue_company_new_fast')\" /></div>
        </td>
    </tr>";
preg_match_all('/<tr>(.*?)<\/tr>/im',$tr,$out,PREG_PATTERN_ORDER);
//为什么这里不要ism 而只用 im 就可以了  好像这里没有 s 是不分行的,是同一行的 (应该是加了s才可以换行,没有s不行换行匹配)
var_dump($out[0]);

//PREG_PATTERN_ORDER 这个参数看得出来
//array(11) {
//  [0]=>
//  string(98) "<tr><td width='50%'>aa</td><td width='20%'>ddddd</td><td width='30%'>2011-08-06 11:39:11</td></tr>"
//  [1]=>
//  string(97) "<tr><td width='50%'>aa</td><td width='20%'>ffff</td><td width='30%'>2011-08-06 11:39:17</td></tr>"
//  [2]=>
//  string(104) "<tr><td width='50%'>aa</td><td width='20%'>aaaaaaaaaaa</td><td width='30%'>2011-08-06 11:39:31</td></tr>"
//  [3]=>
//  string(96) "<tr><td width='50%'>ddd</td><td width='20%'>aa</td><td width='30%'>2011-08-06 11:40:25</td></tr>"
//  [4]=>
//  string(103) "<tr><td width='50%'>我们正在玩</td><td width='20%'>aa</td><td width='30%'>2011-08-06 12:00:47</td></tr>"
//  [5]=>
//  string(105) "<tr><td width='50%'>我们在操作它</td><td width='20%'>aa</td><td width='30%'>2011-08-07 23:38:44</td></tr>"
//  [6]=>
//  string(97) "<tr><td width='50%'>tttt</td><td width='20%'>aa</td><td width='30%'>2011-08-07 23:42:37</td></tr>"
//  [7]=>
//  string(113) "<tr><td width='50%'>测试一下啊啊 啊啊</td><td width='20%'>wanwq</td><td width='30%'>2011-08-09 11:33:17</td></tr>"
//  [8]=>
//  string(151) "<tr><td width='300px' style="word-wrap:break-word;">订单状态更改为已删除</td><td width='20%'>wangjing</td><td width='30%'>2011-09-01 14:11:19</td></tr>"
//  [9]=>
//  string(147) "<tr><td width='300px' style="word-wrap:break-word;">订单状态更改为审核通过</td><td width='20%'>aa</td><td width='30%'>2011-09-01 14:51:15</td></tr>"
//  [10]=>
//  string(147) "<tr><td width='300px' style="word-wrap:break-word;">订单状态更改为审核通过</td><td width='20%'>aa</td><td width='30%'>2011-09-01 14:58:22</td></tr>"
//}
?>

7)preg_match_all7_ok.php
<?php
//下面是好的
$tr =  "<tr><td width='50%'>aa</td><td width='20%'>ddddd</td><td width='30%'>2011-08-06 11:39:11</td></tr>    
        <tr><td width='50%'>aa</td><td width='20%'>ffff</td><td width='30%'>2011-08-06 11:39:17</td></tr>
        <tr><td width='50%'>aa</td><td width='20%'>aaaaaaaaaaa</td><td width='30%'>2011-08-06 11:39:31</td></tr>
        <tr><td width='50%'>ddd</td><td width='20%'>aa</td><td width='30%'>2011-08-06 11:40:25</td></tr>
        <tr><td width='50%'>我们正在玩</td><td width='20%'>aa</td><td width='30%'>2011-08-06 12:00:47</td></tr>
        <tr><td width='50%'>我们在操作它</td><td width='20%'>aa</td><td width='30%'>2011-08-07 23:38:44</td></tr>
        <tr><td width='50%'>tttt</td><td width='20%'>aa</td><td width='30%'>2011-08-07 23:42:37</td></tr>
        <tr><td width='50%'>测试一下啊啊 啊啊</td><td width='20%'>wanwq</td><td width='30%'>2011-08-09 11:33:17</td></tr>
        <tr><td width='300px' style=\"word-wrap:break-word;\">订单状态更改为已删除</td><td width='20%'>wangjing</td><td width='30%'>2011-09-01 14:11:19</td></tr>
        <tr><td width='300px' style=\"word-wrap:break-word;\">订单状态更改为审核通过</td><td width='20%'>aa</td><td width='30%'>2011-09-01 14:51:15</td></tr>
    <tr><td width='300px' style=\"word-wrap:break-word;\">订单状态更改为审核通过</td><td width='20%'>aa</td><td width='30%'>2011-09-01 14:58:22</td></tr>
    <tr>
        <textarea name=\"oprateLog\" cols=\"87\" rows=\"8\"></textarea><br>
        <div align=\"center\"><input type=\"button\" name=\"saveLog\" value=\"保存操作日志\"
            onclick=\"log_Submit('jj_yuyue_company_new_fast')\" /></div>
        </td>
    </tr>";
preg_match_all('/<tr>(.*?)<\/tr>/im',$tr,$out,PREG_SET_ORDER);
//为什么这里不要ism 而只用 im 就可以了  好像这里没有 s 是不分行的,是同一行的 (应该是加了s才可以换行,没有s不行换行匹配)
var_dump($out[0]);

//PREG_SET_ORDER 这个参数看得出来
//array(2) {
//  [0]=>
//  string(98) "<tr><td width='50%'>aa</td><td width='20%'>ddddd</td><td width='30%'>2011-08-06 11:39:11</td></tr>"
//  [1]=>
//  string(89) "<td width='50%'>aa</td><td width='20%'>ddddd</td><td width='30%'>2011-08-06 11:39:11</td>"
//}

?>

8)preg_match_all8_ok.php
<?php
/*
 * Created on 2012-6-11
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 $main = file_get_contents("http://wz.2k001.com/db/d/t/z/");
// var_dump($main);
 $main ="<table width='100%' border='0' cellspacing='0' cellpadding='0'>
<tr>
    <td width='34%'>
<a href='/db/k/2011/1114/177.html' target='_blank'><span class='arrow'></span>视角转换</a>
<div id='line2'></div>
    </td>
    <td width='34%'>
<a href='/db/k/2011/1114/178.html' target='_blank'><span class='arrow'></span>创建角色</a>
<div id='line2'></div>
    </td>
    <td width='34%'>
<a href='/db/k/2011/1114/234.html' target='_blank'><span class='arrow'></span>选择阵营</a>
<div id='line2'></div>
    </td>
    </tr>
    </table>
    <table width='100%' border='0' cellspacing='0' cellpadding='0'>
<tr>
    <td width='34%'>
<a href='/db/k/2011/1114/177.html' target='_blank'><span class='arrow'></span>视角转换</a>
<div id='line2'></div>
    </td>
    <td width='34%'>
<a href='/db/k/2011/1114/178.html' target='_blank'><span class='arrow'></span>创建角色</a>
<div id='line2'></div>
    </td>
    <td width='34%'>
<a href='/db/k/2011/1114/234.html' target='_blank'><span class='arrow'></span>选择阵营</a>
<div id='line2'></div>
    </td>
    </tr>
    </table>        ";
 preg_match_all("/<table.*?>.*?<\/table>/ism",$main,$out,PREG_SET_ORDER);
 
 //看来/ism 这三个参数要好好研究一下 特别是s
 //为什么这里不要ism 而只用 im 就可以了  好像这里没有 s 是不分行的,是同一行的 (应该是加了s才可以换行,没有s不行换行匹配)
 var_dump($out[0]);
 //输出
// //array(1) {
//  [0]=>
//  string(517) "<table width='100%' border='0' cellspacing='0' cellpadding='0'>
//<tr>
//    <td width='34%'>
//<a href='/db/k/2011/1114/177.html' target='_blank'><span class='arrow'></span>视角转换</a>
//<div id='line2'></div>
//    </td>
//    <td width='34%'>
//<a href='/db/k/2011/1114/178.html' target='_blank'><span class='arrow'></span>创建角色</a>
//<div id='line2'></div>
//    </td>
//    <td width='34%'>
//<a href='/db/k/2011/1114/234.html' target='_blank'><span class='arrow'></span>选择阵营</a>
//<div id='line2'></div>
//    </td>
//    </tr>
//    </table>"
//}
 
?>

9)preg_match_all9_ok.php

<?php
/*
 * Created on 2012-6-11
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 $main = file_get_contents("aa.html");

 preg_match_all("/<table[^>]*bordercolor=\"#006fd8\"(.*?)>.*?<\/table>/ism",$main,$out,PREG_SET_ORDER);
 
 var_dump($out[0]);
 var_dump($out[1]);
 
 //[^>]* 这里表示不是以>结尾的
 //就取得了两个表格
?>




普通分类: