Solved

Reading a file using file_get_contents issue

Posted on 2011-09-14
9
340 Views
Last Modified: 2012-06-27
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
Comment
Question by:dolythgoe
9 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 36534505
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
 

Author Comment

by:dolythgoe
ID: 36534558
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
 
LVL 2

Expert Comment

by:montasirma
ID: 36534575
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:dolythgoe
ID: 36534862
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
 
LVL 15

Expert Comment

by:Insoftservice
ID: 36534916
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
 

Author Comment

by:dolythgoe
ID: 36534999
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
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 36536686
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
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 36536791
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
 

Author Closing Comment

by:dolythgoe
ID: 36537203
Thanks a lot for the comprehensive solution - well layed out and added extras!
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question