help with str_replace()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
legaxy
Forum Newbie
Posts: 19
Joined: Thu Mar 20, 2003 3:26 am
Location: Auckland, New Zealand
Contact:

help with str_replace()

Post 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.
Last edited by legaxy on Sat Feb 28, 2004 6:12 pm, edited 1 time in total.
User avatar
Pointybeard
Forum Commoner
Posts: 71
Joined: Wed Sep 03, 2003 7:23 pm
Location: Brisbane, AUS
Contact:

Post by Pointybeard »

I think you would need todo this:

Code: Select all

$weather = str_replace(array($junk, $junk2), "", $weather);
Hope that helps
legaxy
Forum Newbie
Posts: 19
Joined: Thu Mar 20, 2003 3:26 am
Location: Auckland, New Zealand
Contact:

Post 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.
User avatar
Pointybeard
Forum Commoner
Posts: 71
Joined: Wed Sep 03, 2003 7:23 pm
Location: Brisbane, AUS
Contact:

Post by Pointybeard »

Well, your replace is very specific. If a single char is not right, then it wont work.
legaxy
Forum Newbie
Posts: 19
Joined: Thu Mar 20, 2003 3:26 am
Location: Auckland, New Zealand
Contact:

Post 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. ?
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post 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.
ilovetoast
Forum Contributor
Posts: 142
Joined: Thu Jan 15, 2004 7:34 pm

Post by ilovetoast »

This is best done with a regular expression for the reasons pointed out by Pointybeard and Sami. Use preg_replace() instead.

peace
legaxy
Forum Newbie
Posts: 19
Joined: Thu Mar 20, 2003 3:26 am
Location: Auckland, New Zealand
Contact:

Post 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
Post Reply