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

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
georgeYacoub
Forum Newbie
Posts: 2
Joined: Wed Jul 16, 2008 12:15 am

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

Post 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.
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

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

Post 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
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

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

Post by itsmani1 »

i think you need to use regular express.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

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

Post 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
georgeYacoub
Forum Newbie
Posts: 2
Joined: Wed Jul 16, 2008 12:15 am

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

Post 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];
}
?>
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

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

Post 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';
Post Reply