PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Thu Aug 22, 2019 3:14 am

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


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


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


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


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


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


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


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


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


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


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: Majestic-12 [Bot] and 8 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:  
Powered by phpBB® Forum Software © phpBB Group