Jump to content

[SOLVED] Australian Weather Script


willsavin

Recommended Posts

Hi, all

 

I'm trying to make an Australian-based weather script that will download a html file from a FTP server, then it will change the weather info from "Adelaide : 22C : fine and sunny" to "Adelaide : 22C : <img: fineandsunny.jpg". I want it to change the text to image for a (very) broad range of keywords. I have gotten this far:

 

<?php

$source = "/anon/gen/fwo/IDA00100.html";// pull data file from bom

$target = fopen("data.html", "w");

$conn = ftp_connect("ftp2.bom.gov.au") or die("error");

ftp_login($conn,"anonymous","guest");

ftp_fget($conn,$target,$source,FTP_ASCII);

ftp_close($conn);

 

 

$fh2 = fopen("data.html","a+");//open temp file

fwrite($fh2,'Back to main page link');//manipulate temp file

fclose($fh2);//close temp file

$fh3 = fopen("data.html","r+");//open temp file again

 

 

fclose($fh3);//close temp file again

$data = file_get_contents("data.html");//read temp file

echo $data;//print temp file

?>

 

but I cant figure out how to use str_replace to replace the keyword such as "Fine and Sunny" to an image.

 

Thanks ;D

Link to comment
https://forums.phpfreaks.com/topic/101536-solved-australian-weather-script/
Share on other sites

I would do a preg replace for this - then you can specify a whole range of replacements in two arrays - keep them manageable.  The need for the preg_repalce is that the format may not be EXACTLY the same - which is waht str_replace needs; you couold get the odd extra space in the content.

 


$find = array (
'/fine[ ]+and[ ]+sunny/i' ,
'/cold[ ]+and[ ]+wet/i'
);
$replace = array (
'<img src="fineandsuny.jpg" width="20" height="20" />' ,
'<img src="coldandwet.jpg" width="20" height="20" />'
);
$str = preg_replace($find, $replace, $data);

echo $str;

 

Something along those lines will be the bunny...

 

Its all about ensuring you match the correct format of information - this could be made easier if the elements that contain them have a class or id that is consistent and you could then simply grab contents of relevant elements...

 

Good luck - post back if you still need a bit of help.

 

 

thanks for the quick reply.. Ill test it now  :D

 

 

 

would these be cap sensitive?

 

No - the modifier on the end of the pattern (thats the bit after the foward slash) - means the pattern is case insensitve. See[http://uk3.php.net/manual/en/reference.pcre.pattern.modifiers.php]the manual [/url]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.