PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sun Apr 21, 2019 6:09 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 25 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Working with Text File.
PostPosted: Wed Jan 12, 2005 2:28 pm 
Offline
Forum Newbie

Joined: Tue Jan 11, 2005 11:58 pm
Posts: 15
I'll get right to the point. I need to store the contents of a remote text file into a variable. I then need to remove the carriage returns and certain characters from the text. Here is what the contents of the text file looks like:

Syntax: [ Download ] [ Hide ]
Broken Bow, Broken Bow Municipal Airport, NE, United States (KBBW) 41-26N 099-39W
Jan 11, 2005 - 10:53 PM EST / 2005.01.12 0353 UTC
Wind: from the SSE (160 degrees) at 8 MPH (7 KT):0
Visibility: 2 1/2 mile(s):0
Sky conditions: overcast
Weather: mist
Temperature: 17.1 F (-8.3 C)
Windchill: 6 F (-14 C):1
Dew Point: 16.0 F (-8.9 C)
Relative Humidity: 95%
Pressure (altimeter): 29.59 in. Hg (1002 hPa)
ob: KBBW 120353Z AUTO 16007KT 2 1/2SM BR OVC002 M08/M09 A2959 RMK AO2 SLP057 T10831089
cycle: 4


First of all, I need the contents of ftp://weather.noaa.gov/data/observation ... d/KBBW.txt to be saved into a variable. But when I use the following statement:

Syntax: [ Download ] [ Hide ]
$weatherdata = file_get_contents('ftp://weather.noaa.gov/data/observations/metar/decoded/KBBW.txt');


I get the following error:

Warning: file_get_contents(ftp://weather.noaa.gov/data/observation ... d/KBBW.txt): failed to open stream: FTP server reports 550 /data/observations/metar/decoded/KBBW.txt: not a plain file. in /home/grintfar/public_html/weather.php on line 28

So how can I get PHP to store the contents of the remote file into a variable? I am new to PHP and don't know anything about it so please speak in simple terms.

Then, after I get the contents in a variable, how can I remove the carriage returns from the text so that it is all on one line? That will help me in using the variable in JavaScript. Also, JavaScript does not like some of the characters that are in the text, such as parenthesis and slashes. Is there anyway I can remove them or place escape characters next to them with PHP before I export the contents of the variable to JavaScript? I will then use JavaScript to parse the data because I know JavaScript a lot better than I know PHP. Thanks.

Louie


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 12, 2005 2:46 pm 
Offline
Forum Newbie

Joined: Thu Dec 30, 2004 10:50 pm
Posts: 24
Location: New York
Quick research on that server shows that your using the wrong URL. The URL is case sensitive and you have the .txt in lower case when it should be upper. Try the URL below and see if it works:

ftp://weather.noaa.gov/data/observation ... d/KBBW.TXT


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 12, 2005 2:46 pm 
Offline
Forum Newbie

Joined: Tue Jan 11, 2005 11:58 pm
Posts: 15
I figured out my first question, the .txt was suppsed to be capitolized (.TXT). I still need answers to my other questions.

Louie


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 12, 2005 2:47 pm 
Offline
Forum Newbie

Joined: Tue Jan 11, 2005 11:58 pm
Posts: 15
Oh, sorry, I didn't see your post, yes you were right about the case sensitive. Thanks.

Louie


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 12, 2005 2:50 pm 
Offline
Moderator
User avatar

Joined: Mon Nov 03, 2003 7:13 pm
Posts: 5978
Location: Odessa, Ukraine
there's no such file: KBBW.txt.

But there is the file: KBBW.TXT (note the case difference)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 12, 2005 2:52 pm 
Offline
Forum Regular

Joined: Mon Oct 18, 2004 8:14 pm
Posts: 741
you can strip all the carriage returns and new lines like so

Syntax: [ Download ] [ Hide ]
$modified_data = str_replace(array("\r", "\n"), '', $weatherdata);


you might be able to use str_replace to also do some basic character escaping also. but i would really recomend doing it in php instead of js. not all your users will have js on, and php and js are pretty similar anyway, so it shouldnt be too much effort for you to do it in php. im sure we could help you.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 12, 2005 3:10 pm 
Offline
Neighborhood Spidermoddy
User avatar

Joined: Mon Mar 29, 2004 4:24 pm
Posts: 31559
Location: Bothell, Washington, USA
it seems I've already answered this question, for the most part: http://forums.devnetwork.net/viewtopic.php?t=29300 :?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 12, 2005 10:34 pm 
Offline
Forum Newbie

Joined: Tue Jan 11, 2005 11:58 pm
Posts: 15
feyd, yes you gave an answer, but gave no explanation of what to do with the code you gave me??? Remember, I know ABSOLUTELY NOTHING about PHP. Can you please post on what I should do with the code and what it means and what I should type to get an output?? I have noticed on this site that some people get irritated when someone acts as if they want the forum members to "write their script for them". Well, I am not asking that, I am asking you to show me how to get started and explain the functions, etc... that I need to use and I will take it from there. Also, I created a different thread because I was asking a different question. I wanted to know how to take carriage returns out and put escape characters into a string or block of text that is stored in a variable. The fact that it was the same text that I was asking about in the other thread is irrelevant. I was just asking about text in general.

Now, rehfeld, thank you very much for the code, I will try it out. Also, I would very much like to use PHP, but as I said above, I do not know anything about it. Basically all I need is to parse the above text so that the values are stored in separate variables. I don't even need the labels as I can create those separately with HTML. If you could help me do that, I'd be very grateful. Thanks.

feyd, no hard feelings, just explaining my situation.

Thanks both of you for your help.

Louie


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 12, 2005 10:48 pm 
Offline
Neighborhood Spidermoddy
User avatar

Joined: Mon Mar 29, 2004 4:24 pm
Posts: 31559
Location: Bothell, Washington, USA
I don't take any offense. I specifically try not to post too much information, as it can be overload, along with that the person may just want the code, and not understand how it works. If you or anyone wants to understand what it does or how it works, I'd be happy to explain. My goal is to get people to think more, rather than just seek straight, simple answers. The only reason I posted the link here was because it felt strangely similar in both scope and function. My apologies for that.

To take my previous post in the other question:
Syntax: [ Download ] [ Hide ]
preg_match_all('#^\s*(sky\s+conditions|windchill|temperature)\s*:\s*(.*?)\s*$#mi', $textdata, $matches)
If you use print_r() on $matches, you should get each line you wanted as one set of values, its label as another set, and the values (without trailing whitespace like carriage returns) in yet another:
Syntax: [ Download ] [ Hide ]
print_r($matches);
When I run this code against the posted text I get:
Syntax: [ Download ] [ Hide ]
Array
(
[0] => Array
(
[0] => Sky conditions: overcast
[1] => Temperature: 17.1 F (-8.3 C)
[2] => Windchill: 6 F (-14 C):1
)

[1] => Array
(
[0] => Sky conditions
[1] => Temperature
[2] => Windchill
)

[2] => Array
(
[0] => overcast
[1] => 17.1 F (-8.3 C)
[2] => 6 F (-14 C):1
)

)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 12, 2005 11:35 pm 
Offline
Forum Newbie

Joined: Tue Jan 11, 2005 11:58 pm
Posts: 15
WOW!!!!! Thank you very much, that helps a lot. Now, how would I change it if I wanted to add some more in there, such as wind and visibility?? And, how would I output just one of those values? For example I have the code:

Syntax: [ Download ] [ Hide ]
<b><font color="#FF0000" face="Verdana, Arial, sans-serif">Current Temperature:</font></b>&nbsp; XXXXXXX

<b><font color="#FF0000" face="Verdana, Arial, sans-serif">Windchill:</font></b>&nbsp; XXXXXXX


What PHP code would I put where the XXXXXXX is to display only the value (not the label) for that particular weather value (such as windchill value only)?? I know how to call Array values in JavaScript, you just type ArrayName[object number] however the output of your code looks like sort of an Array inside of an Array or something ??!!??!? See, I told you I don't know anything about PHP.

Also, to respond to your response, I very much love thinking! I took a JavaScript course in college and as soon as it was explained how it worked and the simple theories behind it, I began experimenting with it and soon my teacher said I was making the best projects he had ever seen. So I'm no stranger to learning, if I have the right teacher (which for me is usually a book and google.com).

Louie


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 12, 2005 11:44 pm 
Offline
Neighborhood Spidermoddy
User avatar

Joined: Mon Mar 29, 2004 4:24 pm
Posts: 31559
Location: Bothell, Washington, USA
Quote:
Now, how would I change it if I wanted to add some more in there, such as wind and visibility??
Syntax: [ Download ] [ Hide ]
preg_match_all('#^\s*(sky\s+conditions|windchill|temperature|wind|visibility)\s*:\s*(.*?)\s*$#mi', $textdata, $matches)



Quote:
how would I output just one of those values?
Syntax: [ Download ] [ Hide ]
echo $matches&#1111;2]&#1111;1];
will output the temperature.
Dependant on your code, this can be output in many ways:
Syntax: [ Download ] [ Hide ]
<b><font color="#FF0000" face="Verdana, Arial, sans-serif">Current Temperature:</font></b>&nbsp; <?php echo $matches[2][1]; ?>

or

<?php
...
echo "<b><font color="#FF0000" face="Verdana, Arial, sans-serif">Current Temperature:</font></b>&nbsp; {$matches[2][1]}";
...
?>
there are others, but these illustrate the more common ways. Multidimensional arrays work similarly in Javascript, as far as accessing them goes: objectName[objectIndex][subObjectIndex].


[edit] Fixed a minor code issue that'd affect compilation if copied directly..


Last edited by feyd on Wed Jan 12, 2005 11:56 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 12, 2005 11:50 pm 
Offline
Forum Newbie

Joined: Tue Jan 11, 2005 11:58 pm
Posts: 15
Thank you very much, I will try it out and get back to you.

Louie


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 13, 2005 12:29 am 
Offline
Forum Newbie

Joined: Tue Jan 11, 2005 11:58 pm
Posts: 15
It WORKED!!! I have successfully bonded the code you have given me with some very simple JavaScript code to create a weather output for my site. Thank you very much. I will post the code here when I am finished in case anybody else wants some easy and fast weather on their site.

One more question for the sake of learning. Could you explain exactly what everything in the preg_match_all function is and does. It seems to be a very powerful function, but I have no idea what all of those symbols in there mean. Could somebody please break down this function to its simplest parts and explain it in moron terms. Thanks.

Louie


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 13, 2005 12:46 am 
Offline
Neighborhood Spidermoddy
User avatar

Joined: Mon Mar 29, 2004 4:24 pm
Posts: 31559
Location: Bothell, Washington, USA
Syntax: [ Download ] [ Hide ]
preg_match_all('#^\s*(sky\s+conditions|windchill|temperature|wind|visibility)\s*:\s*(.*?)\s*$#mi', $textdata, $matches)


preg_match_all runs a regular expression find (match:)) against all the text supplied to it.

Here's how the pattern I supplied above is broken down:

# - denotes the outside (in this case, beginning) of the regular expression pattern. Note that any symbol, typically will work. I tend to use # because of the rarity it appears in the patterns I work with.
^ - start of line marker
\s* - zero or more whitespace characters
(sky\s+conditions|windchill|temperature|wind|visibility) - ask the regex engine to create a match set when it finds "sky conditions", windchill, temperature, wind, or visibility. Note that sky conditions has a \s+ in it, where that means one or more whitespace characters.
\s* - same as before, zero or more whitespace characters
: - the plain old colon symbol
\s* - another zero or more whitespaces
(.*?)\s*$ - remember the rest of the line, excluding trailing whitespaces. ($) is end of line marker
# - outside (end) pattern marker
m - multiline; execute the pattern and matching against every line of the input
i - ignore case.

$textdata is the input data..
$matches is the output array of matches.


put it all together and you get: find the labels and values of lines where the label is sky conditions, windchill, temperature, wind, or visibility.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 13, 2005 9:07 am 
Offline
Forum Newbie

Joined: Tue Jan 11, 2005 11:58 pm
Posts: 15
Thanks, I understand most of if. There are only 2 parts that I didn't quite understand.

First, the : (colon) symbol. What was its purpose? Is that what told it that a colon was separating the label from the value? If not, how did it know?

Second, the (.*?)\s*$. Could you explain this in a little more detail. What did you mean "remember the rest of the line"? And why did the \s* tell it to ignore whitespaces? How would you tell it to include whitespaces?

Those are the only 2 parts that I didn't understand. The rest of it makes sense now that you broke it down. Thank you.

Louie


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 25 posts ]  Go to page 1, 2  Next

All times are UTC - 5 hours


Who is online

Users browsing this forum: Bing [Bot], Exabot [Bot], Google [Bot] and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group