<td class=bilgi_satir width="45%"><b>( PLACE I WANT TO FETCH ) </b></td>
( OTHER HTML TAGS )
<td class=bilgi_satir width="25%"><b>( PLACE I WANT TO FETCH ) </b></td>
( OTHER HTML TAGS )
<td class=bilgi_satir width="35%"><b>( PLACE I WANT TO FETCH ) </b></td>
( OTHER HTML TAGS )
如何将它们提取到数组?
宽度变量正在改变......
答案 0 :(得分:1)
尝试使用正则表达式解决HTML问题,很快就会遇到两个问题。 但是如果你真的想用脏的正则表达式来做这件事,那就可以了。你不应该依赖它。最简单的正则表达式如下:
preg_match_all(
"/<td class=bilgi_satir width=\"..%\"><b>(.*)<\/b>/i",
$your_html_here, $m
);
这会大致返回print_r( $m[1] );
所需的内容(我添加1,2,3分别告诉他们)
Array (
[0] => ( PLACE I WANT TO FETCH 1 )
[1] => ( PLACE I WANT TO FETCH 2 )
[2] => ( PLACE I WANT TO FETCH 3 )
)
执行此操作的最佳方法是使用DOM解析。例如:
$doc = new DOMDocument();
$doc->loadHTML( $your_html_here );
$x = new DOMXPath( $doc );
$results = $x->query("//td[@class='bilgi_satir']//b");
# $results->length now shows 3
$ret = array();
foreach( $results as $count => $result ) {
printf( "item # %s = %s\n", $count, $result->nodeValue ); # debug only
$ret[] = $result->nodeValue;
}
这将显示:
item # 0 = ( PLACE I WANT TO FETCH 1)
item # 1 = ( PLACE I WANT TO FETCH 2)
item # 2 = ( PLACE I WANT TO FETCH 3)
编辑:显然,将节点值转储到数组中,可以根据需要通过数组进行访问。