Page 1 of 1

help with str_replace()

Posted: Sat Feb 28, 2004 5:39 pm
by legaxy
hey just having a problem using the str_replace() function.
What I am doing is opening up the Yahoo! weather report for my location, which is a javascript document, then removing all the tables and stuff so I can just extract the temperature (and later the weather condition itself - eg. sunny or rainy etc etc) so i can then present it my own way.

The temperature is located between the $junk. (I will deal with the variable image at the end once i solve the first bit :wink: )



Just not sure exactly where I've gone wrong. any help would be great.

Thanks!

Code: Select all

<? 
$weather =  file_get_contents("http://data.geo.yahoo.com/geo/v1?w=c,NZXX0003&hbc=ffffff&hfc=314584&hff=Verdana&hfs=2&ibc=FFFFFF&ifc=0000ff&iff=Verdana&ifs=1&udd=1&ufc=000000&uff=Verdana&ufs=2&ct=NZ&tz=UV&ts=996800400");

$junk = "document.write(''<table border=0 cellpadding=4 cellspacing=0 width="100%" bgcolor="ffffff"><tr><td colspan=3><a href="http://my.yahoo.com/my/p1/?http://weather.yahoo.com"><font color="314584" face="Arial" size="4"><b>Weather</b></font></a></td></tr><tr bgcolor="#ffffff"><td><a href=http://weather.yahoo.com/forecast/NZXX0003_c.html><font face="Times New Roman" size="3" color="#000000">Auckland</font></a><font color=#ff0000>*</font></td><td align=right><font face="Times New Roman" size="3" color="#000000">";

$junk2 = "</td><td align=right><font face="Times New Roman" size="3" color="#000000"><img height=24 width=32 src=http://us.i1.yimg.com/us.yimg.com/i/my/we/12.gif alt="Rain/Windy"></font></td></tr></table>'')";

 $weather = str_replace(array($junk, $junk2), "", $weather); 
echo $weather;
  ?>
//edit - just fixing a missing ;, but that wasnt the issue either :cry:

it works... as far as it wont die horribly, but it doesnt actually replace anything, just appears as per normal.

Posted: Sat Feb 28, 2004 5:53 pm
by Pointybeard
I think you would need todo this:

Code: Select all

$weather = str_replace(array($junk, $junk2), "", $weather);
Hope that helps

Posted: Sat Feb 28, 2004 6:27 pm
by legaxy
oh yeh, thanks :) didnt see your post. A friend also pointed that out, just now. Thanks, it works now... doesnt die.. but it still appears formatted.

What i want to do is get the temperate which is found between junk and junk2, so then i can format it in my own style.

Posted: Sat Feb 28, 2004 6:33 pm
by Pointybeard
Well, your replace is very specific. If a single char is not right, then it wont work.

Posted: Sat Feb 28, 2004 7:26 pm
by legaxy
it was just copy paste, :? but, i suspect maybe the error might lie in the way php reads the html entities here ? and also escaping from double quotes and single quotes. ?

Posted: Sat Feb 28, 2004 8:58 pm
by m3mn0n
Pointybeard wrote:Well, your replace is very specific. If a single char is not right, then it wont work.
On a site like Yahoo, it's not likely they will change their layout often. Infact that's why there is so many scripts on the web like this that get content directly off big sites like this one.

Posted: Sat Feb 28, 2004 9:06 pm
by ilovetoast
This is best done with a regular expression for the reasons pointed out by Pointybeard and Sami. Use preg_replace() instead.

peace

Posted: Sat Feb 28, 2004 10:07 pm
by legaxy

Code: Select all

<?
$weather =  file_get_contents("http://data.geo.yahoo.com/geo/v1?w=c,NZXX0003&hbc=ffffff&hfc=314584&hff=Verdana&hfs=2&ibc=FFFFFF&ifc=0000ff&iff=Verdana&ifs=1&udd=1&ufc=000000&uff=Verdana&ufs=2&ct=NZ&tz=UV&ts=996800400");


$search = array ("'<script[^>]*?>.*?</script>'si",  // Strip out javascript
                 "'<[\/\!]*?[^<>]*?>'si",          // Strip out HTML tags
                 "'([\r\n])[\s]+'",                // Strip out white space
                 "'&(quot|#34);'i",                // Replace HTML entities
                 "'&(amp|#38);'i",
                 "'&(lt|#60);'i",
                 "'&(gt|#62);'i",
                 "'&(nbsp|#160);'i",
                 "'&(iexcl|#161);'i",
                 "'&(cent|#162);'i",
                 "'&(pound|#163);'i",
                 "'&(copy|#169);'i",
                 "'&#(\d+);'e");                    // evaluate as php

$replace = array ("",
                 "",
                 "\\1",
                 """,
                 "&",
                 "<",
                 ">",
                 " ",
                 chr(161),
                 chr(162),
                 chr(163),
                 chr(169),
                 "chr(\\1)");

$text = preg_replace($search, $replace, $weather);
echo "$text";
?>
from the preg_replace() reference on php.net seemed to do the trick, still need to dig out a few stray text chunks though.

Thanks :-)
http://www.southernhosting.net/~crispy/weather.php