开发者

Problem with PHP regular expressions

开发者 https://www.devze.com 2023-03-28 20:06 出处:网络
I just can\'t seem to work out how to pull the entire table from a page using regex. This is my PHP: $printable = file_get_contents(\'http://entertainment.soundboxaudio开发者_运维知识库.com/testplay

I just can't seem to work out how to pull the entire table from a page using regex.

This is my PHP:

$printable = file_get_contents('http://entertainment.soundboxaudio开发者_运维知识库.com/testplaylist.htm');
$array = array();
preg_match( '/<TABLE>(.*?)<\/TABLE>/si', $printable, $array ) ;
$findit = "$array[1]";
echo("$findit");

Any help would be appreciated,

Thanks!


Here we go again... do NOT use regex to extract HTML. HTML is not a regular language and cannot be reliably analyzed with a regex. Use DOM instead.

$printable = file_get_conttents('...');
$dom = new DOMDocument;
$dom->loadHTML($printable);
$xpath = new DOMXpath($dom);

$tables = $xpath->query("//table");

$table_html = array();

foreach($tables as $table) { // workaround for PHP DOM not support innerHTML
   $temp = new DOMDocument;
   $temp->appendChild($temp->importNode($table, true));
   $table_html[] = trim($temp->saveHTML());
}

As well, surrounding variables you're echoing is just a waste of a string operation

echo $x
echo "$x";

work identically, except the quoted version wastes some cpu produce a temporary string that only gets thrown away again.

0

精彩评论

暂无评论...
验证码 换一张
取 消