• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 354
  • Last Modified:

Reading a file using file_get_contents issue

Hello,

I'm having trouble reading a file and using explode on the contents to delimit the ID numbers.

At first, I thought this was a case of the below:

$file = file_get_contents($feed_ids_path);
$ids = explode(' ', $file);

Open in new window


But it appears not! A foreach loop on $ids just returned the entire contents of the file in the first loop!

I've provided the header dump and a sample of output below - any help much appreciated!

array
  0 => string 'HTTP/1.1 200 OK' (length=15)
  1 => string 'Server: nginx' (length=13)
  2 => string 'Date: Wed, 14 Sep 2011 07:12:02 GMT' (length=35)
  3 => string 'Content-Type: text/html' (length=23)
  4 => string 'Connection: close' (length=17)
  5 => string 'P3P: policyref="/p3p.xml", CP="NOI CURa ADMa DEVa TAIa OUR BUS IND UNI COM NAV INT"' (length=83)

Open in new window


A sample of the output is:

1262685 1209443 1263606 1262684 1261250 1261551 1256723 1256626 1255529 1255917 1259276 1258699 1258042 1256218 1255968 1255498 1256410 1258628 1261001 1260563 1258052 1257781 1257157 1254856 1252811 1250009 1247282 1241664 1241473 1241275 1240849 1237008 1230915 1229907 1223238 1201572 1195189 1194883 1187393 1177694 1145879 1141240 1104668 1104665 1084525 1062698 1051076 1040896 1040813 1141234 1023674 995203 864377 841166 791318 773235 791400 648115  ...(and so on)...

PS. I'm working on WAMP from localhost at home if that makes a difference...
0
dolythgoe
Asked:
dolythgoe
1 Solution
 
Dave BaldwinFixer of ProblemsCommented:
Look at the contents in something that will show the character codes and see if that really is a space or another code that will display like a space.  I use PSPad http://www.pspad.com/ for things like that.
0
 
dolythgoeAuthor Commented:
Thanks for that - I'm using Notepad++ and just tried pspad but it does seem to be a space. With show/hide showing a faint dot in the space...
0
 
montasirmaCommented:
Hi dolythgoe,

I tried the following code on the ids you provided and it is working fine.
<?php
$filename = "ids.txt";

$handle = fopen($filename, "r");

while(!feof($handle))
{
  $data = fgets($handle,1024);
  $values = explode(' ', $data);
  foreach($values as $ids)
    echo $ids ."<BR>\n"; 
}

fclose($handle);
?>

Open in new window

 
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
dolythgoeAuthor Commented:
Thanks but they're just an example of the dump to the browser from the rss link.

I'll attached the file here but it's fetched over http:// so reading from a local file doesn't replicate the scenario true to form as there may be other factors at play (might not but just in case)


del.xml.htm
0
 
InsoftserviceCommented:
I hope u have dumped the rss data in this xml and posted it.
as it does not have any property of rss.
please provide the rss link as rss has to handle in different way
0
 
dolythgoeAuthor Commented:
This is the file directly saved from the browser after pasting the so-called 'rss' link in the browser - unfortunately can't post the link for a number of reasons.

It's a rubbish source I know but one I need to handle nonetheless! They call it rss but it has no xml form whatesoever which is frustrating!

Consider this is the data (contents and header dump in original post) - I need to grab the contents and parse the contents into an array.

Does anyone know what the P3P part is? I might be asking an impossible question without providing you the link but was hoping someone has seen this type of thing before and can identify with it.

Cheers

I think it's an odd

0
 
Ray PaseurCommented:
Please post the URL that is contained in this variable: $feed_ids_path

I will read it and show you how to parse the response string.
0
 
Ray PaseurCommented:
This parses the file posted here at EE, however the actual file may be different.
http://www.laprbass.com/RAY_temp_dolythgoe.php
<?php // RAY_temp_dolythgoe.php
error_reporting(E_ALL);


// THE URL OF THE DATA AT EE
$url = 'http://filedb.experts-exchange.com/incoming/2011/09_w38/499902/del.xml.htm';

// READ IT
$str = file_get_contents($url);

// PROCESS EACH LINE
$arr = explode(PHP_EOL, $str);
foreach ($arr as $new)
{
    $new = trim(preg_replace('/\s\s+/', ' ', $new));
    $sub = explode(' ', $new);

    // PROCESS EACH FIELD
    foreach ($sub as $num)
    {
        $all[] = $num;
        $unq[$num] = $num;
    }
}

// SHOW THE WORK PRODUCT
echo "<pre>";
echo PHP_EOL;
echo "NUMBER OF DATA ELEMENTS ";
echo number_format(count($all));
echo PHP_EOL;
echo "NUMBER OF UNIQUE ELEMENTS ";
echo number_format(count($unq));
echo PHP_EOL;
print_r($all);

Open in new window

0
 
dolythgoeAuthor Commented:
Thanks a lot for the comprehensive solution - well layed out and added extras!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now