Page 1 of 1

search the contents of a certain html file and locate a numb

Posted: Wed Jul 16, 2008 12:23 am
by georgeYacoub
I would like to search the contents of a certain html file and locate the number that comes between a certain span tag.
for example I could have a very large file like this
....
<span id="someId">100</span>
....
And I simply would like to extract the number between the tag which in this case is 100. The number is not know and changes from file to another.

Here is the code I wrote so far. I made several trials with strstr and strpos but failed.

Code: Select all

 
$file = "file.html";
$handle = fopen($file, "r");
$input = fread($handle, filesize($file)); 
fclose($handle);
 
I will appreciate any input.

Re: search the contents of a certain html file and locate a numb

Posted: Wed Jul 16, 2008 12:30 am
by omniuni
I would try with file_get_contents();


so,


$seachme = file_get_contents("myfile.html");

now use strpos to find where the beginning tag is, take a substring with substr() from there to the end of $searchme, use strpos to find the first "<" and cut $searchme after that, again using substr(). The result should be your number.

-Omni

Re: search the contents of a certain html file and locate a numb

Posted: Wed Jul 16, 2008 1:19 am
by itsmani1
i think you need to use regular express.

Re: search the contents of a certain html file and locate a numb

Posted: Wed Jul 16, 2008 2:31 am
by prometheuzz
Try this:

Code: Select all

<?php
 
$html = 'some text <span id="idA">90</span> ... 
    <span id="idB">100</span> text <span id="idC">110</span> more text';
 
$id = 'idB';
$regex = '#<span\s++id="' . $id . '">([^>]+)</span>#si';
 
if(preg_match($regex, $html, $matches)) {
    print 'Found value: ' . $matches[1];
}
 
/*
Output = Found value: 100
*/
 
?>
Feel free to post back if something is unclear.

HTH

Re: search the contents of a certain html file and locate a numb

Posted: Wed Jul 16, 2008 12:40 pm
by georgeYacoub
Thank you all very much for your reply but when I run this script on a large file I simply get no output. The code posted by prometheuzz works fine with a variable but as soon as I have the variable hold the contents of a file that weird behavior occurs (i.e. not output)

Anyway if you would like to test my code please go to http://finance.yahoo.com/q?s=AAPL and save the page as html file and add it in the same directory as the contents of the php file which are posted below. Sorry I couldn't attach any files.

Code: Select all

<?php
$file = "file.html";
$handle = fopen($file, "r");
$html = fread($handle, filesize($file));
echo $html;
fclose($handle);
 
$id = 'idB';
$regex = '#<span\s++id="' . $id . '">([^>]+)</span>#si';
 
if(preg_match($regex, $html, $matches)) {
    print 'Found value: ' . $matches[1];
}
?>

Re: search the contents of a certain html file and locate a numb

Posted: Wed Jul 16, 2008 1:09 pm
by prometheuzz
georgeYacoub wrote:Thank you all very much for your reply but when I run this script on a large file I simply get no output. ...
You need to change the following line of course!

Code: Select all

$id = 'idB';