Author Topic: preg_replace horror  (Read 2591 times)

0 Members and 1 Guest are viewing this topic.

Offline willsavinTopic starter

  • Irregular
  • Posts: 23
  • Gender: Male
    • View Profile
    • CoroCC.org
Re: preg_replace horror
« Reply #15 on: April 23, 2008, 07:30:52 AM »
I'm sorry, ill try and figure this one out myself..
 ???

thanks for all the help
willsavin

Offline effigy

  • Staff Alumni
  • Freak!
  • *
  • Posts: 7,301
  • Gender: Male
  • We must be the change we wish to see in the world.
    • View Profile
Re: preg_replace horror
« Reply #16 on: April 23, 2008, 11:26:43 AM »
Code: [Select]
<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, -1PREG_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>
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

Offline willsavinTopic starter

  • Irregular
  • Posts: 23
  • Gender: Male
    • View Profile
    • CoroCC.org
Re: preg_replace horror
« Reply #17 on: April 23, 2008, 06:09:18 PM »
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

Offline effigy

  • Staff Alumni
  • Freak!
  • *
  • Posts: 7,301
  • Gender: Male
  • We must be the change we wish to see in the world.
    • View Profile
Re: preg_replace horror
« Reply #18 on: April 24, 2008, 10:12:30 AM »
Right. But for the sake of example, that was the easiest way to do it.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

Offline dumdumsareyum

  • Enthusiast
  • Posts: 97
    • View Profile
    • Slant Graphics
Re: preg_replace horror
« Reply #19 on: May 06, 2008, 05:40:07 PM »
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.