Jump to content

preg_replace horror


willsavin

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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))

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • 2 weeks later...

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.