PHP代码如下:
<?PHP$string = '<table border="0" cellspacing="0"><tr><td class=f>测试标题</a></td></tr></table>';
$match = '/<table border="0" cellspacing="0"><tr><td class=f>(.+?)<\/a><\/td><\/tr><\/table>/i';
preg_match($match, $string,$res );
print_r($res);
?>结果:Array
(
[0] => <table border="0" cellspacing="0"><tr><td class=f>测试标题</a></td></tr></table>
[1] => 测试标题
)
<?PHP$string = '<table border="0" cellspacing="0"><tr><td class=f>测试标题</a></td></tr></table>';
$match = '/<table border="0" cellspacing="0"><tr><td class=f>(.+?)<\/a><\/td><\/tr><\/table>/i';
preg_match($match, $string,$res );
print_r($res);
?>结果:Array
(
[0] => <table border="0" cellspacing="0"><tr><td class=f>测试标题</a></td></tr></table>
[1] => 测试标题
)
网页中有好多table,部分table中有“测试标题”这个关键词,
我想要的是这些含有“测试标题”关键词的table。比如网页的内容是:
【随机内容0】<table border="0" cellspacing="0"><tr><td class=f>【随机内容1】测试标题</a>【随机内容3】</td></tr></table>【随机内容xxxx】<table border="0" cellspacing="0"><tr><td class=f>【随机内容x】测试标题</a>【随机内容xx】</td></tr></table>【随机内容xxxx】<table border="0" cellspacing="0"><tr><td class=f>【随机内容x】测试标题</a>【随机内容xx】</td></tr></table>在【 】内的内容(包括【 】)都是没有规则的内容,想得到的是:
<table border="0" cellspacing="0"><tr><td class=f>【随机内容1】测试标题</a>【随机内容3】</td></tr></table><table border="0" cellspacing="0"><tr><td class=f>【随机内容x】测试标题</a>【随机内容xx】</td></tr></table><table border="0" cellspacing="0"><tr><td class=f>【随机内容x】测试标题</a>【随机内容xx】</td></tr></table>"
>【随机内容xxxx】<table border="0" cellspacing="0"><tr><td class=f>【随机内容x】测试标题</a>【随机内容xx】</td></tr>< /table>
【随机内容xxxx】<table border="0" cellspacing="0"><tr><td class=f>【随机内容x】测试标题</a>【随机内容xx】</td></tr>< /table>'
;if (preg_match_all("/(<table.*?\/table>)/",$s,$m)) {
print_r($m[1]);
}
谢谢您!
你是把所有table都输出出来了
我要的是含有“测试标题”这样关键词的table
print_r($m[1]);
}得到想要的结果
谢谢Aylazhang(春暖花开)兄
马上结贴