-5
<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 )

我怎样才能将它们提取到数组中?

宽度变量正在改变......

4

1 回答 1

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) 

编辑:显然将节点值转储到数组中可以让您根据需要通过数组访问。

于 2013-07-23T21:40:06.203 回答