willsavin Posted April 17, 2008 Share Posted April 17, 2008 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 Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted April 17, 2008 Share Posted April 17, 2008 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. Quote Link to comment Share on other sites More sharing options...
willsavin Posted April 17, 2008 Author Share Posted April 17, 2008 thanks for the quick reply.. Ill test it now would these be cap sensitive? Quote Link to comment Share on other sites More sharing options...
willsavin Posted April 17, 2008 Author Share Posted April 17, 2008 It worked perfectly. Thank you for the quick help! ;D ;D ;D Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted April 17, 2008 Share Posted April 17, 2008 thanks for the quick reply.. Ill test it now 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] Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted April 17, 2008 Share Posted April 17, 2008 You may want to add the modifier that treats a new line as white space (or does it do that by default???) just in case it goes over to a new line... 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.