开发者

Get contents page using regular expression

开发者 https://www.devze.com 2023-02-27 07:06 出处:网络
<?php $source=\'http://www.google.com/finance\'; //$source=\'sample.txt\'; $page_all = file_get_contents($source);
<?php
$source='http://www.google.com/finance';
//$source='sample.txt';
$page_all = file_get_contents($source);
$div_array=array();
preg_match_all('#<div id="markets">(.*?)</div>#sim', $page_all, $div_array);
//print_r($div_开发者_开发百科array);
print_r($div_array[1]);
?>

I have this peice of code. I am trying to return the contents of a specific div from google/finance.

All I endup on screen though is array()

Any ideas.

Regards


Don't use regex for these kind of things, try a DOM parser such as SimpleHTMLDom.

<?php 
require_once('simple_html_dom.php');
echo file_get_html('http://www.google.com/finance')->find('#markets', 0);
?>

Yeah... it's that easy :)

edit:

In response to your comment, behold the awesomeness of SimpleHTMLDom:

<?php 
require_once('simple_html_dom.php');

$html = file_get_contents('http://www.google.com/finance');
$tidy = tidy_parse_string($html);
$tidy->cleanRepair();
$html = str_get_html((string)$tidy);

foreach($html->find('#markets .quotes', 0)->find('tr') as $line) {
    printf("%s - %s - %s %s<br />", 
        $line->find('.symbol a', 0)->innertext,
        $line->find('.price span', 0)->innertext,
        $line->find('.change span', 0)->innertext,
        $line->find('.change span', 1)->innertext);
}
?>

Yeah, I had to use Tidy for that page... I don't know who Google hired to do that HTML but it's absolutely horrendous. Unclosed td's, multiple elements with same id's etc... Parser choked on those :(


I have not found <div id="markets"> in 'http://www.google.com/finance' HTML-page, but found <div id=markets>, then try:

<?php
$source='http://www.google.com/finance';
//$source='sample.txt';
$page_all = file_get_contents($source);
$div_array=array();
preg_match_all('#<div id=markets>(.*?)</div>#sim', $page_all, $div_array);
//print_r($div_array);
print_r($div_array[1]);
?>
0

精彩评论

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