一个html文件中有多个table,我想提取其中一个包含某个关键字的table,但是老搞不定,能否帮我看看待处理的html字符窜如下:
------------------------------
$sTmp = '
<body bgcolor="#FFFFFF" link="#0000FF" vlink="#0000FF" alink="#FF0000" leftmargin="0" topmargin="0">
<table border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td valign="top"> <table border="0" cellpadding="0" cellspacing="0" background="../images/default05_ent_26.gif">
<tr>
<td ><IMG SRC="../images/default05_ent_06.gif" border="0"></td>
<td><div align="center"><a href="../ent.htm" class="whitec11"><strong>首页</strong></a>
<font color="#FFFFFF">|</font> <a href="../zhi_nan/ZN_default.htm" class="whitec11"><strong>招生指南</strong></a>
<font color="#FFFFFF">|</font> <a href="../zhuan_ye/ZSZY_default.htm" class="whitec11"><strong>招生专业</strong></a>
<font color="#FFFFFF">|</font> <a href="../fu_dao_ban/FDB_default.htm" class="whitec11"><strong>辅导班</strong></a>
<font color="#FFFFFF">|</font><a href="../xing_xi/XX_default.htm" class="whitec11"><strong>
最新信息</strong></a></div></td>
<td ><IMG SRC="../images/default05_ent_08.gif" ></td>
</tr>
</table>
<table border="0" align="center" cellpadding="2" cellspacing="0">
<tr>
<td><div align="right">您现在的位置:<a href="http://www.sjtuce.net" target="_blank">上海交通大学继续教育学院成人教育部</a>
<span class="bluearrow">>></span> 成人高考辅导班 <span class="bluearrow">>></span>
<a href="#">招生专业</a></div></td>
</tr>
</table>
<table border="0" align="center" cellpadding="2" cellspacing="0">
<tr>
<td ><strong><img src="../zmages/arrow3.jpg" >
</strong></td>
';
-----------------------------------我要提取表格里面包含 “招生指南”这个关键字的表格内容,提取到的表格内容不包含<table>与</table>标签。
我整了好久,整理出一个这样的。单结果还是一次提取出了多个表格。
$sRules ='/<(div|table)[^<]*>{1}([\s\S]+招生指南[\s\S]+)(<\/\\1>){1}/i';
preg_match_all($sRules, $sTmp, $aResult, PREG_PATTERN_ORDER);
print_r($aResult);通过正则,我想取到这样一个代码段,但是老调不好。
<table border="0" cellpadding="0" cellspacing="0" background="../images/default05_ent_26.gif">
<tr>
<td ><IMG SRC="../images/default05_ent_06.gif" border="0"></td>
<td><div align="center"><a href="../ent.htm" class="whitec11"><strong>首页</strong></a>
<font color="#FFFFFF">|</font> <a href="../zhi_nan/ZN_default.htm" class="whitec11"><strong>招生指南</strong></a>
<font color="#FFFFFF">|</font> <a href="../zhuan_ye/ZSZY_default.htm" class="whitec11"><strong>招生专业</strong></a>
<font color="#FFFFFF">|</font> <a href="../fu_dao_ban/FDB_default.htm" class="whitec11"><strong>辅导班</strong></a>
<font color="#FFFFFF">|</font><a href="../xing_xi/XX_default.htm" class="whitec11"><strong>
最新信息</strong></a></div></td>
<td ><IMG SRC="../images/default05_ent_08.gif" ></td>
</tr>
</table>或者这样的块:
<div align="center"><a href="../ent.htm" class="whitec11"><strong>首页</strong></a>
<font color="#FFFFFF">|</font> <a href="../zhi_nan/ZN_default.htm" class="whitec11"><strong>招生指南</strong></a>
<font color="#FFFFFF">|</font> <a href="../zhuan_ye/ZSZY_default.htm" class="whitec11"><strong>招生专业</strong></a>
<font color="#FFFFFF">|</font> <a href="../fu_dao_ban/FDB_default.htm" class="whitec11"><strong>辅导班</strong></a>
<font color="#FFFFFF">|</font><a href="../xing_xi/XX_default.htm" class="whitec11"><strong>
最新信息</strong></a></div>
提取出信息块后,我后期还得要对这个信息块进行处理。
------------------------------
$sTmp = '
<body bgcolor="#FFFFFF" link="#0000FF" vlink="#0000FF" alink="#FF0000" leftmargin="0" topmargin="0">
<table border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td valign="top"> <table border="0" cellpadding="0" cellspacing="0" background="../images/default05_ent_26.gif">
<tr>
<td ><IMG SRC="../images/default05_ent_06.gif" border="0"></td>
<td><div align="center"><a href="../ent.htm" class="whitec11"><strong>首页</strong></a>
<font color="#FFFFFF">|</font> <a href="../zhi_nan/ZN_default.htm" class="whitec11"><strong>招生指南</strong></a>
<font color="#FFFFFF">|</font> <a href="../zhuan_ye/ZSZY_default.htm" class="whitec11"><strong>招生专业</strong></a>
<font color="#FFFFFF">|</font> <a href="../fu_dao_ban/FDB_default.htm" class="whitec11"><strong>辅导班</strong></a>
<font color="#FFFFFF">|</font><a href="../xing_xi/XX_default.htm" class="whitec11"><strong>
最新信息</strong></a></div></td>
<td ><IMG SRC="../images/default05_ent_08.gif" ></td>
</tr>
</table>
<table border="0" align="center" cellpadding="2" cellspacing="0">
<tr>
<td><div align="right">您现在的位置:<a href="http://www.sjtuce.net" target="_blank">上海交通大学继续教育学院成人教育部</a>
<span class="bluearrow">>></span> 成人高考辅导班 <span class="bluearrow">>></span>
<a href="#">招生专业</a></div></td>
</tr>
</table>
<table border="0" align="center" cellpadding="2" cellspacing="0">
<tr>
<td ><strong><img src="../zmages/arrow3.jpg" >
</strong></td>
';
-----------------------------------我要提取表格里面包含 “招生指南”这个关键字的表格内容,提取到的表格内容不包含<table>与</table>标签。
我整了好久,整理出一个这样的。单结果还是一次提取出了多个表格。
$sRules ='/<(div|table)[^<]*>{1}([\s\S]+招生指南[\s\S]+)(<\/\\1>){1}/i';
preg_match_all($sRules, $sTmp, $aResult, PREG_PATTERN_ORDER);
print_r($aResult);通过正则,我想取到这样一个代码段,但是老调不好。
<table border="0" cellpadding="0" cellspacing="0" background="../images/default05_ent_26.gif">
<tr>
<td ><IMG SRC="../images/default05_ent_06.gif" border="0"></td>
<td><div align="center"><a href="../ent.htm" class="whitec11"><strong>首页</strong></a>
<font color="#FFFFFF">|</font> <a href="../zhi_nan/ZN_default.htm" class="whitec11"><strong>招生指南</strong></a>
<font color="#FFFFFF">|</font> <a href="../zhuan_ye/ZSZY_default.htm" class="whitec11"><strong>招生专业</strong></a>
<font color="#FFFFFF">|</font> <a href="../fu_dao_ban/FDB_default.htm" class="whitec11"><strong>辅导班</strong></a>
<font color="#FFFFFF">|</font><a href="../xing_xi/XX_default.htm" class="whitec11"><strong>
最新信息</strong></a></div></td>
<td ><IMG SRC="../images/default05_ent_08.gif" ></td>
</tr>
</table>或者这样的块:
<div align="center"><a href="../ent.htm" class="whitec11"><strong>首页</strong></a>
<font color="#FFFFFF">|</font> <a href="../zhi_nan/ZN_default.htm" class="whitec11"><strong>招生指南</strong></a>
<font color="#FFFFFF">|</font> <a href="../zhuan_ye/ZSZY_default.htm" class="whitec11"><strong>招生专业</strong></a>
<font color="#FFFFFF">|</font> <a href="../fu_dao_ban/FDB_default.htm" class="whitec11"><strong>辅导班</strong></a>
<font color="#FFFFFF">|</font><a href="../xing_xi/XX_default.htm" class="whitec11"><strong>
最新信息</strong></a></div>
提取出信息块后,我后期还得要对这个信息块进行处理。
preg_match_all($preg, $orders, $a);
print_r($preg);
试试看
echo $arr[1];
$sTmp = '
<table border="0" align="center" cellpadding="2" cellspacing="0">
<tr> <td>您现在的位置</td> </tr>
</table>
<table cellspacing="0">
<tr>
<td valign="top"> <table border="0" cellpadding="0" cellspacing="0" background="../images/default05_ent_26.gif">
<tr>
<td><a href="../ent.htm" class="whitec11"><strong>首页</strong></a>
<a href="../xing_xi/XX_default.htm" class="whitec11"><strong>
最新信息</strong></a></div></td>
</tr>
</table>
<table border="0" align="center" cellpadding="2" cellspacing="0">
<tr>
<td><div align="right">您现在的位置:</td>
</tr>
</table>
<table border="0" align="center" cellpadding="2" cellspacing="0">
<tr>
<td ><strong><img src="../zmages/arrow3.jpg" >
</strong></td>
';
这个能够提取出 table部分,但是如果是上下都有table的时候,他会取出的是最前面的<table,跟最末尾的,</table>。
这样一来,取出的内容就会包含多个table了但是我测试了一下,这样写:'/ <table.*?>[^(<table)]+?(.*?首页.*?) <\/table>/sm'
没有效果,我是想在<table 后面不要在出现“<table”标记,这样就能保证取出的是最小范围的一个<table。所以想请教一下 正则里面限定某个字符穿不能出现的正则 怎么写?还有啊,请教8楼的师傅,最后面的"/sm"这个是表示什么意思
preg_match('/<table.*?>(.*?招生指南.*?)<\/table>/sm', $sTmp, $arr);
echo $arr[1];
preg_match('/(<table.*?>.*?招生指南.*?<\/table>)/sm', $sTmp, $arr);
echo $arr[1];
preg_match('/(<table(?:.(?<!<table))*招生指南.*?<\/table>)/sm', $sTmp, $arr);
echo $arr[1];
$sTmp = '
<table border="0">
<tr> <td></td> </tr>
</table>
<table cellspacing="0">
<tr>
<td valign="top"> <table border="0">
<tr>
<td><a href="../ent.htm" class="whitec11"><strong>首页</strong></a></td>
</tr>
</table>
<table border="0">
<tr>
<td><div align="right">您现在的位置:</td>
</tr>
</table>
<table cellspacing="0">
<tr>
<td ><strong><img src="../zmages/arrow3.jpg" >
</strong></td>
';
$sRules ='/(<table.*?>.*?首页.*?<\/table>)/sm';
preg_match_all($sRules, $sTmp, $aResult, PREG_PATTERN_ORDER);
print_r($aResult[1]);
不好意思,我没有表达清楚,上面是你的代码提取的结果:<table border="0" align="center" cellpadding="2" cellspacing="0">
<tr> <td>您现在的位置</td> </tr>
</table>
<table cellspacing="0">
<tr>
<td valign="top"> <table border="0" cellpadding="0" cellspacing="0" background="../images/default05_ent_26.gif">
<tr>
<td><a href="../ent.htm" class="whitec11"><strong>首页</strong></a>
<a href="../xing_xi/XX_default.htm" class="whitec11"><strong>
最新信息</strong></a></div></td>
</tr>
</table>
我想要的结果是:<table border="0" cellpadding="0" cellspacing="0" background="../images/default05_ent_26.gif">
<tr>
<td><a href="../ent.htm" class="whitec11"><strong>首页</strong></a>
<a href="../xing_xi/XX_default.htm" class="whitec11"><strong>
最新信息</strong></a></div></td>
</tr>
</table>
就是想得到最小范围内的<table>....</table>这个区域。
我一直在想怎么排除在<table >后面不再出现<table >标签的方式,即取出最小范围的<table>....</table>区域。
preg_match('/(<table(?:.(?<!<table))*招生指南.*?<\/table>)/sm', $sTmp, $arr);
echo $arr[1];
m是把很多行看成一行来处理,s的话.号匹配所有字符包括换行符