Minikoopa
Minikoopa

Reputation: 1

Get contents page using regular expression

<?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

Upvotes: 0

Views: 670

Answers (2)

Daniel Sloof
Daniel Sloof

Reputation: 12706

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 :(

Upvotes: 1

axelcoon
axelcoon

Reputation: 81

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]);
?>

Upvotes: 0

Related Questions