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

 

 

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.