一个正则匹配问题,麻烦解答

tony-杨 2006-12-27 12:30:18
<table width="100%" height="300" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td valign="top">
<table width="600" border="3" align="center" cellpadding="2" cellspacing="0">
<tr height="25">
<td width="120" class="TDTitle">名称</td>
<td width="480">数据</td>
</tr>
<tr height="25">
<td width="120" class="TDTitle">名称</td>
<td width="480">数据</td>
</tr>
<!--重复上面的两列n行格式-->
</table>
</td>
</tr>
</table>
=================================================
问题:我需要嵌套表格中每行的[名称][数据]。

我用
preg_match_all("/<td width=\"120\" class=\"TDTitle\">(.*)<\/td>.*<td width=\"480\">(.*)<\/td>/isU", $content, $matches)
得不到数据,why?
...全文
139 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
zairwolf 2006-12-27
  • 打赏
  • 举报
回复
-_-~~~
Cain 2006-12-27
  • 打赏
  • 举报
回复
jf
four498 2006-12-27
  • 打赏
  • 举报
回复
接分
懒得去死 2006-12-27
  • 打赏
  • 举报
回复
回复人:gzty(风逍遥) ( 五级(中级)) 信誉:99 2006-12-27 12:40:42 得分:0
?
用preg_match_all("/<td[\s\S][(align=\"right\")|(width=\"480\")>](.*)[\s]*<\/td>/siU", $filecontent, $matches);

得到的就是
[0] => idth="120" class="TDTitle" align="right">名称
[1] => idth="480">数据
…………
而不是单纯的
[0] => 名称
[1] => 数据
…………
请问怎么实现?
======
第一个都是原来匹配的整个值
你把第一个去掉就行了
$matches = array($matches[1],$matches[2]);
print_r($matches);
tony-杨 2006-12-27
  • 打赏
  • 举报
回复
我找到原因了
谢谢各位
tony-杨 2006-12-27
  • 打赏
  • 举报
回复
君子兰
稻草人
请问哈你们的环境是?
我的preg_match_all("/<td width=\"120\" class=\"TDTitle\">(.*)<\/td>.*<td width=\"480\">(.*)<\/td>/isU", $content, $matches)得到一个空数组
懒得去死 2006-12-27
  • 打赏
  • 举报
回复
可以得到数据啊:
代码:
===================
<?php
$content = '<table width="100%" height="300" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td valign="top">
<table width="600" border="3" align="center" cellpadding="2" cellspacing="0">
<tr height="25">
<td width="120" class="TDTitle">名称</td>
<td width="480">数据</td>
</tr>
<tr height="25">
<td width="120" class="TDTitle">名称</td>
<td width="480">数据</td>
</tr>
<!--重复上面的两列n行格式-->
</table>
</td>
</tr>
</table>';
preg_match_all("/<td width=\"120\" class=\"TDTitle\">(.*)<\/td>.*<td width=\"480\">(.*)<\/td>/isU", $content, $matches);
print_r($matches);
?>
===================
---------- 程序调试 ----------
Array
(
[0] => Array
(
[0] => <td width="120" class="TDTitle">名称</td>
<td width="480">数据</td>
[1] => <td width="120" class="TDTitle">名称</td>
<td width="480">数据</td>
)

[1] => Array
(
[0] => 名称
[1] => 名称
)

[2] => Array
(
[0] => 数据
[1] => 数据
)

)

Output completed (0 sec consumed) - Normal Termination
visam168 2006-12-27
  • 打赏
  • 举报
回复
帮顶了...
tony-杨 2006-12-27
  • 打赏
  • 举报
回复
用preg_match_all("/<td[\s\S][(align=\"right\")|(width=\"480\")>](.*)[\s]*<\/td>/siU", $filecontent, $matches);

得到的就是
[0] => idth="120" class="TDTitle" align="right">名称
[1] => idth="480">数据
…………
而不是单纯的
[0] => 名称
[1] => 数据
…………
请问怎么实现?
zairwolf 2006-12-27
  • 打赏
  • 举报
回复
rt
ice_berg16 2006-12-27
  • 打赏
  • 举报
回复
我测试可以得到数据
tony-杨 2006-12-27
  • 打赏
  • 举报
回复
多谢君子兰~

21,887

社区成员

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

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