截取复杂table和img的正则

caotian2000 2006-08-07 05:44:19
function splitcontent($content,$length)
{
preg_match_all("#(?=(<table(?:(?:[^<]|<(?!table))*?|(?1))*</table>))#",$content,$ar,PREG_OFFSET_CAPTURE
);
if (is_array($ar[0][0]))
{
reset($ar[0]);
for ($i=0;$i<count($ar[0]);$i++)
{
$num=$ar[0][0][1];
if ($ar[0][$i][1]>$length) {
$num=$ar[0][$i][1];
break;
}
else
{
$num=$ar[0][$i][1];
}
}
}
else
{
preg_match_all("/\<img[^\>]*?\>/i",$content, $msg,PREG_OFFSET_CAPTURE);
if (is_array($msg[0][0]))
{
reset($msg[0]);
$num=$msg[0][0][1];
if (count($msg[0])==1) {
$num=strlen($content);
}
else
{
for ($i=0;$i<count($msg[0]);$i++)
{
if ($msg[0][$i][1]>$length) {
$num=$msg[0][$i][1];
break;
}
else
{
$num=$msg[0][$i][1];
}
}
}
}
else
{
$num=$length;
}
}
return $num;
}
这个是我总结的一个截取带table和img的函数,能帮我完善一下嘛!
现在遇到复杂的table还是不行的。
希望高手指点一下!
...全文
401 28 打赏 收藏 举报
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
Gdj 2006-08-09
  • 打赏
  • 举报
回复
这个正则只能把表格分离出来。要他自动去找文章的特征还没这么智能。你得自己找特征然后另写正则。
Gdj 2006-08-08
  • 打赏
  • 举报
回复
……

既然8个table都截出来了,哪一个是你要的你比较一下不就知道了吗。我又怎么可以预知到你想要的是哪一个呢?
caotian2000 2006-08-08
  • 打赏
  • 举报
回复
象我帖的那个函数一样,我想把我想要的长度和内容带进去。
我就想要出最接近我的那个完整的TABLE!
而且之前如果有字啊,一些别的东西也需要要着,前面的TABLE如果没截全在后面还要补全代码!
caotian2000 2006-08-08
  • 打赏
  • 举报
回复
对啊!是截了八个

但是哪个是我要的那个结果呢?
Gdj 2006-08-08
  • 打赏
  • 举报
回复
preg_match_all("#(?=(<table(?:(?:[^<]|<(?!table))*?|(?1))*</table>))#is",$content,$ar);
print_r($ar);

你是不是指的这个?
Gdj 2006-08-08
  • 打赏
  • 举报
回复
不明白你的意思。上面的例子已经取出8个这样的效果的table了。
caotian2000 2006-08-08
  • 打赏
  • 举报
回复
那你指点一下?我该怎么个取法?
能达到这个效果?
<table cellspacing="0" cellpadding="0" width="1002" border="0"> <tbody> <tr> <td> <table height="236" width="100%" bgcolor="#00626E" border="0"> <tbody> <tr> <td valign="top" width="250" rowspan="3"> <table width="100%" border="0"> <tbody> <tr> <td><img height="72" src= "http://blog.sina.com.cn/control/writing/scriber/images/logo.jpg" width="250" /></td> </tr> <tr> <td> <table width="85%" border="0"> <tbody> <tr> <td bgcolor="#FFFFFF" colspan="3" height="2"></td> </tr> <tr> <td width="50" height="25"></td> <td> </td> <td align="right"> </td> </tr> <tr> <td width="50" height="25"></td> <td><a class="a3" href= "http://blog.sina.com.cn/control/writing/scriber/blog/index.php">博客园地</a></td> <td align="right"><a class="a3" href= "http://blog.sina.com.cn/control/writing/scriber/iask/index.php">寻医问药</a></td> </tr> <tr> <td width="50" height="25"></td> <td><a class="a3" href= "http://blog.sina.com.cn/control/writing/scriber/modules/index.php?moduleid=1"> 人文时事</a></td> <td align="right"><a class="a3" href= "http://blog.sina.com.cn/control/writing/scriber/modules/index.php?moduleid=2"> 营养饮食</a></td> </tr> <tr> <td width="50" height="25"></td> <td><a class="a3" href= "http://blog.sina.com.cn/control/writing/scriber/modules/index.php?moduleid=5"> 母婴园地</a></td> <td align="right"><a class="a3" href= "http://blog.sina.com.cn/control/writing/scriber/modules/index.php?moduleid=6"> 健康资讯</a></td> </tr> <tr> <td width="50" height="25"></td> <td><a class="a3" href= "http://blog.sina.com.cn/control/writing/scriber/modules/index.php?moduleid=3"> 美容健身</a></td> <td align="right"><a class="a3" href= "http://blog.sina.com.cn/control/writing/scriber/modules/index.php?moduleid=4"> 心理天地</a></td> </tr> <tr> <td width="50" height="25"></td> <td><a class="a3" href= "http://blog.sina.com.cn/control/writing/scriber/modules/index.php?moduleid=7"> 就医通道</a></td> <td align="right"><a class="a3" href= "http://blog.sina.com.cn/control/writing/scriber/modules/index.php?"> </a></td> </tr> </tbody> </table> </td> </tr> </tbody> </table> </td> </tr> </tbody> </table> </td> </tr> </tbody> </table> <br /><br /><a href="/u/553c6d60010004fo" style="font-style: italic;" target="_blank">点击此处查看原文</a>
Gdj 2006-08-08
  • 打赏
  • 举报
回复
第一段载出来8个table,根本没错。0到7不是8个是什么……
caotian2000 2006-08-08
  • 打赏
  • 举报
回复
你能把MSN或者QQ或邮箱给我吗?我把测试的那个文件给你!方便沟通
caotian2000 2006-08-08
  • 打赏
  • 举报
回复
我添上了也是这样!Gdj!还是不行的
Gdj 2006-08-08
  • 打赏
  • 举报
回复
不是说你漏掉了is吗

preg_match_all("#(?=(<table(?:(?:[^<]|<(?!table))*?|(?1))*</table>))#is",$content,$ar,PREG_OFFSET_CAPTURE
);
print_r($ar);
caotian2000 2006-08-08
  • 打赏
  • 举报
回复
preg_match_all("#(?=(<table(?:(?:[^<]|<(?!table))*?|(?1))*</table>))#",$content,$ar,PREG_OFFSET_CAPTURE
);
print_r($ar);
caotian2000 2006-08-08
  • 打赏
  • 举报
回复
这是我用那个正则截出来的!一共截出七个符合条件的,截的是上面我贴的那段代码
caotian2000 2006-08-08
  • 打赏
  • 举报
回复
Array
(
[0] => Array
(
[0] => Array
(
[0] =>
[1] => 2
)

[1] => Array
(
[0] =>
[1] => 76
)

[2] => Array
(
[0] =>
[1] => 182
)

[3] => Array
(
[0] =>
[1] => 351
)

[4] => Array
(
[0] =>
[1] => 2016
)

[5] => Array
(
[0] =>
[1] => 2236
)

[6] => Array
(
[0] =>
[1] => 3074
)

[7] => Array
(
[0] =>
[1] => 3167
)

)
)
Gdj 2006-08-08
  • 打赏
  • 举报
回复
新浪的我测了一共4个table根本没问题呀。
Gdj 2006-08-08
  • 打赏
  • 举报
回复
"#(?=(<table(?:(?:[^<]|<(?!table))*?|(?1))*</table>))#is

我原来的正则是这样写的。你似乎漏掉了后面的is吧
caotian2000 2006-08-08
  • 打赏
  • 举报
回复
<title>sdfsfsdf</title>
<link>http://blog.sina.com.cn/u/553c6d60010004fo</link>
<description><?xml version="1.0" encoding="gbk"?> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> </head> <body> <table cellspacing="0" cellpadding="0" width="1002" border="0"> <tbody> <tr> <td> <table height="236" width="100%" bgcolor="#00626E" border="0"> <tbody> <tr> <td valign="top" width="250" rowspan="3"> <table width="100%" border="0"> <tbody> <tr> <td><img height="72" src= "http://blog.sina.com.cn/control/writing/scriber/images/logo.jpg" width="250" /></td> </tr> <tr> <td> <table width="85%" border="0"> <tbody> <tr> <td bgcolor="#FFFFFF" colspan="3" height="2"></td> </tr> <tr> <td width="50" height="25"></td> <td> </td> <td align="right"> </td> </tr> <tr> <td width="50" height="25"></td> <td><a class="a3" href= "http://blog.sina.com.cn/control/writing/scriber/blog/index.php">博客园地</a></td> <td align="right"><a class="a3" href= "http://blog.sina.com.cn/control/writing/scriber/iask/index.php">寻医问药</a></td> </tr> <tr> <td width="50" height="25"></td> <td><a class="a3" href= "http://blog.sina.com.cn/control/writing/scriber/modules/index.php?moduleid=1"> 人文时事</a></td> <td align="right"><a class="a3" href= "http://blog.sina.com.cn/control/writing/scriber/modules/index.php?moduleid=2"> 营养饮食</a></td> </tr> <tr> <td width="50" height="25"></td> <td><a class="a3" href= "http://blog.sina.com.cn/control/writing/scriber/modules/index.php?moduleid=5"> 母婴园地</a></td> <td align="right"><a class="a3" href= "http://blog.sina.com.cn/control/writing/scriber/modules/index.php?moduleid=6"> 健康资讯</a></td> </tr> <tr> <td width="50" height="25"></td> <td><a class="a3" href= "http://blog.sina.com.cn/control/writing/scriber/modules/index.php?moduleid=3"> 美容健身</a></td> <td align="right"><a class="a3" href= "http://blog.sina.com.cn/control/writing/scriber/modules/index.php?moduleid=4"> 心理天地</a></td> </tr> <tr> <td width="50" height="25"></td> <td><a class="a3" href= "http://blog.sina.com.cn/control/writing/scriber/modules/index.php?moduleid=7"> 就医通道</a></td> <td align="right"><a class="a3" href= "http://blog.sina.com.cn/control/writing/scriber/modules/index.php?"> </a></td> </tr> </tbody> </table> </td> </tr> </tbody> </table> </td> </tr> </tbody> </table> </td> </tr> </tbody> </table> <br /><br /><a href="/u/553c6d60010004fo" style="font-style: italic;" target="_blank">点击此处查看原文</a></description>
caotian2000 2006-08-08
  • 打赏
  • 举报
回复
把这段截了你看应该如何写,下面是新浪BLOG截取的XML!
caotian2000 2006-08-08
  • 打赏
  • 举报
回复
<TABLE cellSpacing=0 cellPadding=0 width=1002 border=0>
<TBODY>
<TR>
<TD>
<TABLE height=236 width="100%" bgColor=#00626e border=0>
<TBODY>
<TR>
<TD vAlign=top width=250 rowSpan=3>
<TABLE width="100%" border=0>
<TBODY>
<TR>
<TD><IMG height=72 src="http://blog.sina.com.cn/control/writing/scriber/images/logo.jpg" width=250></IMG></TD></TR>
<TR>
<TD>
<TABLE width="85%" border=0>
<TBODY>
<TR>
<TD bgColor=#ffffff colSpan=3 height=2></TD></TR>
<TR>
<TD width=50 height=25></TD>
<TD> </TD>
<TD align=right> </TD></TR>
<TR>
<TD width=50 height=25></TD>
<TD><A class=a3 href="http://blog.sina.com.cn/control/writing/scriber/blog/index.php">博客园地</A></TD>
<TD align=right><A class=a3 href="http://blog.sina.com.cn/control/writing/scriber/iask/index.php">寻医问药</A></TD></TR>
<TR>
<TD width=50 height=25></TD>
<TD><A class=a3 href="http://blog.sina.com.cn/control/writing/scriber/modules/index.php?moduleid=1">人文时事</A></TD>
<TD align=right><A class=a3 href="http://blog.sina.com.cn/control/writing/scriber/modules/index.php?moduleid=2">营养饮食</A></TD></TR>
<TR>
<TD width=50 height=25></TD>
<TD><A class=a3 href="http://blog.sina.com.cn/control/writing/scriber/modules/index.php?moduleid=5">母婴园地</A></TD>
<TD align=right><A class=a3 href="http://blog.sina.com.cn/control/writing/scriber/modules/index.php?moduleid=6">健康资讯</A></TD></TR>
<TR>
<TD width=50 height=25></TD>
<TD><A class=a3 href="http://blog.sina.com.cn/control/writing/scriber/modules/index.php?moduleid=3">美容健身</A></TD>
<TD align=right><A class=a3 href="http://blog.sina.com.cn/control/writing/scriber/modules/index.php?moduleid=4">心理天地</A></TD></TR>
<TR>
<TD width=50 height=25></TD>
<TD><A class=a3 href="http://blog.sina.com.cn/control/writing/scriber/modules/index.php?moduleid=7">就医通道</A></TD>
<TD align=right><A class=a3 href="http://blog.sina.com.cn/control/writing/scriber/modules/index.php?moduleid=8">学习园地</A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></TD>
<TD vAlign=top width=470 rowSpan=3>
<TABLE width="100%" border=0>
<TBODY>
<TR>
<TD height=40> </TD></TR>
<TR>
<TD align=middle><IMG height=58 src="http://blog.sina.com.cn/control/writing/scriber/images/doctor.jpg" width=318></IMG></TD></TR>
<TR>
<TD>
<TABLE width="99%" align=center border=0>
<TBODY>
<TR>
<TD style="LINE-HEIGHT: 150%" align=left>    在我<BR><BR></TD></TR></TBODY></TABLE></TD></TR>
<TR>
<TD bgColor=#cccccc height=1></TD></TR>
<TR>
<TD height=14></TD></TR>
<TR>
<TD class=td04 align=middle>  <A class=a3 href="http://blog.sina.com/" target=_blank>产品与服务</A>     <A class=a3 href="http://blog.sina.com.cn/control" target=_blank>关于我们</A>     </TD></TR></TBODY></TABLE></TD>
<TD height=40></TD></TR>
<TR>
<TD><IMG height=137 src="http://blog.sina.com.cn/control/writing/scriber/images/map.jpg" width=248></IMG></TD></TR>
<TR>
<TD align=middle> </TD></TR></TBODY></TABLE></TD></TR>
<TR>
<TD background=images/%C3%83%C3%BB%C3%92%C2%BD%C3%8D%C3%B8.jpg height=130></TD></TR>
<TR>
<TD>
<TABLE width="100%" bgColor=#ffffff border=0>
<TBODY>
<TR height=5>
<TD></TD></TR>
<TR>
<TD>
<TABLE width="100%" border=0>
<TBODY>
<TR>
<TD class=td01 width=256> </TD>
<TD class=td02 align=right width=256> </TD>
<TD class=td02 align=right colSpan=2> </TD></TR>
<TR>
<TD bgColor=#000000 colSpan=2 height=2></TD>
<TD colSpan=2></TD></TR>
<TR>
<TD class=td03 align=middle colSpan=2 height=25 rowSpan=2 HREF="http://blog.sina.com.cn/control/writing.php" TARGET="_blank">忘记密码</TD>
<TD class=td02 vAlign=bottom align=right width=262 rowSpan=2><A href="http://www.hd315.gov.cn/beian/view.asp?bianhao=010202005030200018" target=_blank><IMG height=48 src="http://blog.sina.com.cn/control/writing/scriber/images/biaoshi.gif" width=40 border=0></IMG></A></TD>
<TD class=td02 align=right width=204 height=25>E-mail: <A class=a04 href="mailto:office@goodr.net">office@goodr.net</A></TD></TR>
<TR>
<TD class=td02 align=right></TD></TR>
<TR>
<TD>
<FORM action=./modules/index_userlogin.php method=post>    健康会员 <INPUT id=username style="HEIGHT: 12px" size=18 name=username></INPUT>     密 码 <INPUT id=password style="HEIGHT: 12px" type=password size=18 name=password></INPUT>     <INPUT style="FONT-SIZE: 12px" type=submit value=登录></INPUT> <INPUT type=hidden value=normal name=usertype></INPUT> <INPUT type=hidden value=login name=act></INPUT></FORM></TD>
<TD class=td02 align=right colSpan=2 height=25></TD></TR>
<TR>
<TD class=td02 align=right colSpan=2 height=25></TD></TR>
<TR>
<TD>
<FORM action=./modules/index_userlogin.php method=post>    医生会员 <INPUT id=username style="HEIGHT: 12px" size=18 name=username></INPUT>     密 码 <INPUT id=password style="HEIGHT: 12px" type=password size=18 name=password></INPUT>     <INPUT style="FONT-SIZE: 12px" type=submit value=登录></INPUT> <INPUT type=hidden value=doctor name=usertype></INPUT> <INPUT type=hidden value=login name=act></INPUT></FORM></TD>
<TD class=td02 align=right colSpan=2></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE>
<DIV></DIV>
Gdj 2006-08-08
  • 打赏
  • 举报
回复
给个不行的例子
加载更多回复(8)

21,889

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧