willsavin Posted April 23, 2008 Share Posted April 23, 2008 hello all, I'm trying to create a script that (maybe)will use preg_replace to replace a very large list(in a .txt file) of weather terms such as "a shower or two"(caps dont matter). I want to be able to replace these descriptions with another set of descriptions(in a .txt file). Both of the weather descriptions are separated by newlines. The list are about 2000 lines each. Thanks for your time. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted April 23, 2008 Share Posted April 23, 2008 Obviously you don't want to replace all of the content in the first file, since that's what you have in the second. So what do you want to replace? Show a sample of both files. Quote Link to comment Share on other sites More sharing options...
willsavin Posted April 23, 2008 Author Share Posted April 23, 2008 the first file contains this(only a small piece):: becoming sunny. dry. becoming sunny. fine, becoming sunny. fine. becoming sunny. fine, cloud clearing. cloudy early, then fine. morning cloud. mainly fine. fine, sunny afternoon. sunny breaks. fine. becoming sunny. fine. morning cloud. fine. partly cloudy morning. mainly fine. cloudy morning. becoming fine. mostly sunny afternoon. fine and sunny afternoon. fine. afternoon sunny breaks. fine. mostly sunny afternoon. fine. partly sunny afternoon. fine. sunny afternoon. fine, becoming sunny. fine. becoming sunny. morning cloud then fine. sunny afternoon. fine. morning cloud. dry. becoming mainly sunny. becoming mostly sunny. fine. becoming mostly sunny. mainly sunny. mostly sunny. mostly sunny. mainly sunny. dry mostly sunny. dry, mainly sunny. dry, mostly sunny. dry. mainly sunny. dry. mostly sunny. dry. sunny periods. dry. mostly sunny. and i want to replace it with this(small sample):: becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny mostly sunny mostly sunny mostly sunny mostly sunny mostly sunny, dry mostly sunny, dry mostly sunny, dry mostly sunny, dry mostly sunny, dry mostly sunny, dry mostly sunny, dry thanks for your help Quote Link to comment Share on other sites More sharing options...
discomatt Posted April 23, 2008 Share Posted April 23, 2008 I'd just use str_ireplace() for that Quote Link to comment Share on other sites More sharing options...
thebadbad Posted April 23, 2008 Share Posted April 23, 2008 I still don't get you. Doesn't the second file contain what you are looking for? Quote Link to comment Share on other sites More sharing options...
willsavin Posted April 23, 2008 Author Share Posted April 23, 2008 im trying to make a script that fill first get some weather data form the meteorolgy place then i want it to replace the very long list of descriptiive weather terms that they give me with a smaller list which ill then use to change into images(like sunny and rianing etc.the first is the desrcitions i want replaced. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted April 23, 2008 Share Posted April 23, 2008 Elaborate on the "replacing a long list with a smaller list" part. I thought the lists were the same length, like your sample suggested? Quote Link to comment Share on other sites More sharing options...
willsavin Posted April 23, 2008 Author Share Posted April 23, 2008 The lists are the same size but they are modified so that it has less descriptions(like :: early morning fog replaces with fog, late morning fog replaces with fog etc I therefore have less images to make! (BTW sorry about the spelling previously(i was typing real fast)) Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted April 23, 2008 Share Posted April 23, 2008 After going through your post I still don't understand what your trying to do is there some sort a algorithm of what your trying to achieve is so please do share it, We will be able to assists you better. Quote Link to comment Share on other sites More sharing options...
willsavin Posted April 23, 2008 Author Share Posted April 23, 2008 No, I just want to have a broad range of weather descriptions in a file that can then, be changed to a list of easier to manipulate descriptions IE changing becoming sunny. dry. becoming sunny. fine, becoming sunny. fine. becoming sunny. fine, cloud clearing. cloudy early, then fine. morning cloud. mainly fine. fine, sunny afternoon. sunny breaks. fine. becoming sunny. fine. morning cloud. fine. partly cloudy morning. mainly fine. cloudy morning. becoming fine. mostly sunny afternoon. fine and sunny afternoon. fine. afternoon sunny breaks. fine. mostly sunny afternoon. fine. partly sunny afternoon. fine. sunny afternoon. fine, becoming sunny. fine. becoming sunny. morning cloud then fine. sunny afternoon. fine. morning cloud. dry. becoming mainly sunny. becoming mostly sunny. fine. becoming mostly sunny. mainly sunny. mostly sunny. mostly sunny. mainly sunny. dry mostly sunny. dry, mainly sunny. dry, mostly sunny. dry. mainly sunny. dry. mostly sunny. dry. sunny periods. dry. mostly sunny. fine and mainly sunny. fine, mainly sunny day. fine, mainly sunny. fine, mostly sunny day. fine, mostly sunny. fine, sunny periods. fine. lengthy sunny periods . fine. lengthy sunny periods. fine. mainly sunny. fine. mostly sunny. fine. sunny periods. fine and mostly sunny. fine, mostly sunny. fine. mainly sunny day. fine. mainly sunny. fine. mostly sunny day. fine. mostly sunny. fine. sunny periods. fine and mostly sunny. fine, mainly sunny. fine, mostly sunny day. fine. mainly sunny day. fine. mainly sunny. fine. mostly sunny. fine. mostly sunny. fine. sunny periods. hot, mostly sunny. sunny. sunny. sunny, less humid. sunny, but cool. dry and sunny. dry, sunny. dry. sunny. cool, sunny and dry. dry and sunny. dry, sunny. sunny and dry. sunny, dry. fine, dry and sunny. fine, dry, sunny. dry and sunny. dry. sunny day. dry. sunny. to becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny becoming sunny mostly sunny mostly sunny mostly sunny mostly sunny mostly sunny, dry mostly sunny, dry mostly sunny, dry mostly sunny, dry mostly sunny, dry mostly sunny, dry mostly sunny, dry mostly sunny, fine mostly sunny, fine mostly sunny, fine mostly sunny, fine mostly sunny, fine mostly sunny, fine mostly sunny, fine mostly sunny, fine mostly sunny, fine mostly sunny, fine mostly sunny, fine mostly sunny, fine mostly sunny, fine mostly sunny, fine mostly sunny, fine mostly sunny, fine mostly sunny, fine mostly sunny, fine mostly sunny, fine mostly sunny, fine mostly sunny, fine mostly sunny, fine mostly sunny, fine mostly sunny, fine mostly sunny, fine mostly sunny, fine mostly sunny, hot sunny sunny sunny sunny sunny, dry sunny, dry sunny, dry sunny, dry sunny, dry sunny, dry sunny, dry sunny, dry sunny, dry sunny, dry sunny, dry sunny, dry sunny, dry thanks Quote Link to comment Share on other sites More sharing options...
thebadbad Posted April 23, 2008 Share Posted April 23, 2008 ?! You haven't answered my question: Doesn't the second file (or the second list) contain what you are looking for? I still fail to see why you would replace a whole list with another list. Quote Link to comment Share on other sites More sharing options...
willsavin Posted April 23, 2008 Author Share Posted April 23, 2008 I dont want it to replace the list, i want a script that will be able to replace those variables in the first list with the variables(second list) in another file that automatically prints the file. I show you a working example(this one only replaces basic data like fog, sunny etc with images:::: I want this one to replace it with more concise weather descriptions, that i can easily make images for(other than replacing basic keywords. its available at http://corocc.org/weather/capital2.php Quote Link to comment Share on other sites More sharing options...
thebadbad Posted April 23, 2008 Share Posted April 23, 2008 I understand where you wanna end with this. But the replacing is still pretty unclear. Can you show us the two text files? It seems to me, that you only have the original list, and then want to replace similar descriptions with a simpler description, but without having the second list of same length with corresponding replacements. Am I right? Because if you HAVE the second concise list, why not use that?! This is really confusing. Quote Link to comment Share on other sites More sharing options...
willsavin Posted April 23, 2008 Author Share Posted April 23, 2008 the reason i have the first list is because i get the data from the BOM(Australian weather) then i use the first list as the find argument then the second list is what i want it replaced with inside the page. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted April 23, 2008 Share Posted April 23, 2008 I really can't help you when you don't answer my simple questions, sorry mate. Quote Link to comment Share on other sites More sharing options...
willsavin Posted April 23, 2008 Author Share Posted April 23, 2008 I'm sorry, ill try and figure this one out myself.. ??? thanks for all the help willsavin Quote Link to comment Share on other sites More sharing options...
effigy Posted April 23, 2008 Share Posted April 23, 2008 <pre> <?php $data = <<<DATA changing becoming sunny. dry. becoming sunny. fine, becoming sunny. fine. becoming sunny. fine, cloud clearing. cloudy early, then fine. morning cloud. mainly fine. fine, sunny afternoon. sunny breaks. fine. becoming sunny. fine. morning cloud. fine. partly cloudy morning. mainly fine. cloudy morning. becoming fine. mostly sunny afternoon. fine and sunny afternoon. fine. afternoon sunny breaks. fine. mostly sunny afternoon. fine. partly sunny afternoon. fine. sunny afternoon. fine, becoming sunny. fine. becoming sunny. morning cloud then fine. sunny afternoon. fine. morning cloud. dry. becoming mainly sunny. becoming mostly sunny. fine. becoming mostly sunny. mainly sunny. mostly sunny. mostly sunny. mainly sunny. dry mostly sunny. dry, mainly sunny. dry, mostly sunny. dry. mainly sunny. dry. mostly sunny. dry. sunny periods. dry. mostly sunny. fine and mainly sunny. fine, mainly sunny day. fine, mainly sunny. fine, mostly sunny day. fine, mostly sunny. fine, sunny periods. fine. lengthy sunny periods . fine. lengthy sunny periods. fine. mainly sunny. fine. mostly sunny. fine. sunny periods. fine and mostly sunny. fine, mostly sunny. fine. mainly sunny day. fine. mainly sunny. fine. mostly sunny day. fine. mostly sunny. fine. sunny periods. fine and mostly sunny. fine, mainly sunny. fine, mostly sunny day. fine. mainly sunny day. fine. mainly sunny. fine. mostly sunny. fine. mostly sunny. fine. sunny periods. hot, mostly sunny. sunny. sunny. sunny, less humid. sunny, but cool. dry and sunny. dry, sunny. dry. sunny. cool, sunny and dry. dry and sunny. dry, sunny. sunny and dry. sunny, dry. fine, dry and sunny. fine, dry, sunny. dry and sunny. dry. sunny day. dry. sunny. DATA; ### Split into sections. $pieces = preg_split('/^\s*$/m', $data); ### Split into lines. foreach ($pieces as &$piece) { $piece = preg_split('/\n/', $piece, -1, PREG_SPLIT_NO_EMPTY); } ### Result. print_r($pieces); ### Make the replacements. $conditions = array( 'becoming sunny', 'mostly sunny', 'mostly sunny, dry', 'mostly sunny, fine', 'mostly sunny, hot', 'sunny', 'sunny, dry' ); $num_pieces = count($pieces); for ($i = 0; $i < $num_pieces; $i++) { $num_sub_pieces = count($pieces[$i]); for ($j = 0; $j < $num_sub_pieces; $j++) { $pattern = '/^' . preg_quote($pieces[$i][$j]) . '$/m'; $data = preg_replace($pattern, $conditions[$i], $data); } } echo $data; ?> </pre> Quote Link to comment Share on other sites More sharing options...
willsavin Posted April 23, 2008 Author Share Posted April 23, 2008 Thanks for the post effigy, This is not really what i meant, I meant that, the first part $data is the find argument and the second part $conditions is the replace argument. The $data variable should be the file which I'm searching through(like a html file) so the final command should (maybe) be $final = preg_replace($data, $conditions[$i], $weatherfile); Thanks for the help, willsavin Quote Link to comment Share on other sites More sharing options...
effigy Posted April 24, 2008 Share Posted April 24, 2008 Right. But for the sake of example, that was the easiest way to do it. Quote Link to comment Share on other sites More sharing options...
dumdumsareyum Posted May 6, 2008 Share Posted May 6, 2008 I think what you're getting at is that you have a list of the weather descriptions used by the weather agency. You need to select a picture based on this description. You don't have as many pictures as there are descriptions so you would like to replace some of the descriptions with your own shorter description and then use that to select the image. But what you haven't told us is what the criteria are for the replacement. We need something more specific, for example, I want to replace all the descriptions with "becoming sunny" or "early cloud" with "mostly sunny". If the lists are already grouped in the fashion you display them, which each chunk relating to a single concise definition, you could save the main list in a database with another column with a code for which description or picture you want to use and another column with a serial id#. Then just run some sql queries to add the code for the description(or even the new description) to your database....i.e. look at the lines in your text editor and subtract to tell you how many lines there are for each description, then update your db where lower limit<id<upperlimit. Then all you have to do is match the exact more detailed description, get the code, then output the corresponding description.....anyway, just saying maybe there's an easier way than trying to get regex to make the judgement call. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.