xtract values from webpage

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

New php-forum User
New php-forum User
Posts: 1
Joined: Fri Mar 29, 2013 3:17 pm

xtract values from webpage

Postby prw0800 » Fri Mar 29, 2013 5:21 pm

This has been stumping me for 3 nights!

Code is designed to read a set of 6 digit (URN) numbers from a text file.
The numbers added to a URL give a web page from which we want to extract a number.

// read text file
$file_handle = fopen("urns.txt", "rb");
while (!feof($file_handle) )

$count +=1;
$urn = fgets($file_handle); // get next URN

if ($urn <>"") // for each urn
// load web page into $filedump
$filedump = file_get_contents ("http://url.of.web.page?urn=".$urn);

// extract required value from page dump
preg_match('{pretext(.*)posttext}', $filedump, $output);
$extracted_value = $output[1];

// store URN and extracted_value in results Table
$insert = mysql_query("INSERT INTO results (urn, value) VALUES ('$urn', '$extracted_value')") or die("Insert query failed");


all the seperate parts of the code seem to be working correctly; URN numbers are correctly read in, value extracts correctly and is inserted in reults Table.

when fed with 4 input numbers, only the 4th value is extracted correctly; the other 3 are null
when fed with 3 input numbers, only the 3rd value is extracted correctly; others are null;
when fed with 1 input number, that value is extracted correctly;

I've tried reading the numbers into an array and then sending them to the main code; doesn't help.
I've also tried putting the main code into a function to keep the variables private? doesn't help.
I can't see why it will work with one input value but only with the second of two values, etc.


php-forum Fan User
php-forum Fan User
Posts: 973
Joined: Mon Oct 01, 2012 12:32 pm

Re: xtract values from webpage

Postby seandisanti » Sun Mar 31, 2013 8:47 pm

your issue is here:

Code: Select all

fgets($file_handle); // get next URN

$urn is only ever assigned one character, and it's overwritten on every iteration. the tip off to the error should be that only the last is saved. change the line to read:

Code: Select all

.= fgets($file_handle); // get next URN

and it should work fine for you. some of the most annoying bugs come down to one character like that.

Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 5 guests