html如下
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" id="test1" >
......
</table>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" id="test2" >
......
</table>
我想根据table, id提取出想要的table,如table, id=test1,提取出第一个,table,id=test2就提取出第二个,请问正则该怎么做呀,我试了下这个,好像不行。
/<table.*?id=\"test1\"*>(.*?)<\/table>/sm顺便说下,感觉正则蛮麻烦的,有好的正则资料参考推荐下吗?呵
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" id="test1" >
......
</table>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" id="test2" >
......
</table>
我想根据table, id提取出想要的table,如table, id=test1,提取出第一个,table,id=test2就提取出第二个,请问正则该怎么做呀,我试了下这个,好像不行。
/<table.*?id=\"test1\"*>(.*?)<\/table>/sm顺便说下,感觉正则蛮麻烦的,有好的正则资料参考推荐下吗?呵
preg_match("/<table[\s\S]*?id=\"test1\"[\s\S]*?>[\s\S]*?<\/table>/", $str, $match);
var_dump($match);
http://manual.phpv.net/regular_expression.html
preg_match_all('/<table[^>]*id\S*=\S*[\'"]?test1[\'"]?[^>]*>[\d\D]*<\/table>/i',$string,$matches);var_dump($matches);
$snoopy = new Snoopy;
$snoopy->fetch("http://www.17u.com/price_report/linelist_0_80_80_0_0_0_0__1_0_7_20_1_1_0.html");
$line = $snoopy->results;
$pat="/<table.*?id=\"lineshowtype1\".*?>(.*?)<\/table>/sm";
preg_match_all($pat, $line, $match);
(.*?) //这个不可以匹配所有东西,比如换行之类的id=\"lineshowtype1\" //你这个太绝对了点,属性不一定用 双引号,可以单引号,或者没有的
问题似乎在红色的部分,会取出其它的,我换成/<table[^>]*id\S*=\S*[\'"]?test1[\'"]?[^>]*>(.*?)<\/table>/i就正常了。请问,这样有什么风险吗?
print_r($match);
我用这个正则试了下,怎么输出是2个呀?
Array ( [0] => Array ( [0] => ......1 ) [1] => Array ( [0] => ......1 ) )
$text = '<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" id="test1" >......1</table><table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" id="test2" >......2</table>';preg_match_all('/<table[^>]*id\S*=\S*[\'"]?test1[\'"]?[^>]*>[\d\D]*<\/table>/i',$text, $match);
print_r($match);
?>
输出
Array ( [0] => Array ( [0] => ......1 ......2 ) )
不是应该输出id=test1的吗?
$text = '<table width="100%"< border="0" align="center" cellpadding="0" cellspacing="0" id="test1" >.....1</table><table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" id="test2" >......2</table>';preg_match_all('/<table[\s\S.]*?(id="test1")[\s\S.]*?<\/table>/i',$text, $match);
print_r($match);
?>
$text = '<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" id="test1" >...如果这里如果包含table你就取不到你想要的东西了...1</table><table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" id="test2" >......2</table>';
$text = '<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" id="test1" >.....1</table><table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" id="test2" >......2</table>';
$p = get_tags_pos($text, '<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" id="test1" >', '<table', '</table>');
echo trim(substr($text, $p[0], $p[1] - $p[0]));
function get_tags_pos($html, $btags1 = "<div>", $btags2 = "<div", $etags1 = "</div>"){
$p1 = strpos($html, $btags1);
if($p1 === false)return array(0 => 0, 1 => 0); $p1 += strlen($btags1);
$p2 = $p1;
$p3 = $p1;
while(true){
$p_t1 = strpos($html, $btags2, $p2 + 1);
$p_t2 = strpos($html, $etags1, $p3 + 1);
if($p_t2 === false)break;
$p2 = $p_t1;
$p3 = $p_t2;
if($p_t1 === false || $p_t2 < $p_t1){
$p2 = $p_t2;
break;
}
}
return array(0 => $p1, 1 => $p2);
}
?>