brent.fraser Posted January 22, 2009 Share Posted January 22, 2009 Hi everyone. It has been some time since I have done any coding (perl) and I am running into a bit of a headache. I am using some PHP and JS I downloaded and I am putting a weather RRS feed on a web-page I am creating. The RSS is working as I am getting the information into my HTML browser but it is having and ° in the output and not a space and degree symbol. I know from my long past days of perl that you can replace those with spaces and characters. I am still looking around the net for answers currently.... I looked into the PHP script and I think I have identified the place for the replacement of the values when outputting it into javascript: // ------------------------------------------------------------------- // outputRSS_JS()- Outputs the "title", "link", "description", and "pubDate" elements of an RSS feed in XML format // ------------------------------------------------------------------- function outputRSS_JS($url, $divid) { global $rss; if ($rs = $rss->get($url)){ echo "rsscontentdata.$divid=new Array();\n"; $i=0; foreach ($rs['items'] as $item) { echo "rsscontentdata.$divid" . "[$i]={link:\"" . slashit($item[link]) . "\", title:\"" . slashit($item[title]) . "\", description:\"" . slashit($item[description]) . "\", date:\"" . slashit($item[pubDate]) . "\"}\n"; $i++; } if ($rs['items_count'] <= 0) { echo "rsscontentdata=\"Sorry, no items found in the RSS file\""; } } else { echo "rsscontentdata=\"Sorry: It's not possible to reach RSS file $url\""; // All else fails } } function slashit($what){ //Encode text for storing in JavaScript array $newstring=str_replace(''', '\'', $what);//replace those half valid apostrophe entities with actual apostrophes return rawurlencode($newstring); } have tried to change $newstring=str_replace(''', '\'', $what); to $newstring=str_replace(' ', ' ', $what); and that isn't working in the output as I still see " ". Any suggestions on how to replace the " " and "°" in the PHP script? Thanks for the patience..... Quote Link to comment https://forums.phpfreaks.com/topic/141995-solved-replacing-nbsp-and-deg-in-php/ Share on other sites More sharing options...
DeanWhitehouse Posted January 22, 2009 Share Posted January 22, 2009 This line is causing them to be converted to HTML characters i believe return rawurlencode($newstring); Try changing it to return $newstring; Quote Link to comment https://forums.phpfreaks.com/topic/141995-solved-replacing-nbsp-and-deg-in-php/#findComment-743496 Share on other sites More sharing options...
brent.fraser Posted January 22, 2009 Author Share Posted January 22, 2009 Hi there Blade280891, I tried your suggestion and it didn't work. I changed rawurlencode with urlencode and I get the output with "+" symbols as separators where spaces are supposed to go. I think using the rawurlencode might be line in question and I also think that the function slashit($what) may be the issue as well. Still working at it..... thanks again. B Quote Link to comment https://forums.phpfreaks.com/topic/141995-solved-replacing-nbsp-and-deg-in-php/#findComment-743670 Share on other sites More sharing options...
DeanWhitehouse Posted January 22, 2009 Share Posted January 22, 2009 Have you tried just doing return $newstring; on its own? Quote Link to comment https://forums.phpfreaks.com/topic/141995-solved-replacing-nbsp-and-deg-in-php/#findComment-743674 Share on other sites More sharing options...
brent.fraser Posted January 22, 2009 Author Share Posted January 22, 2009 Hi, yes return $newstring; is the first thing I did and it did not work. I did the other stuff after I tried return $newstring; by itself. Quote Link to comment https://forums.phpfreaks.com/topic/141995-solved-replacing-nbsp-and-deg-in-php/#findComment-743692 Share on other sites More sharing options...
DeanWhitehouse Posted January 22, 2009 Share Posted January 22, 2009 It must be sent with html chars made special then, try using rawurldecode (i think it is) Quote Link to comment https://forums.phpfreaks.com/topic/141995-solved-replacing-nbsp-and-deg-in-php/#findComment-743696 Share on other sites More sharing options...
brent.fraser Posted January 23, 2009 Author Share Posted January 23, 2009 Hi again... Well, here's all I tried that has not worked: rawurldecode($newstring) urldecode($newstring) htmlspecialchars($newstring) htmlentities($newstring) I have also done a $newstring = str_replace('nbsp', "testing", $what); where I say to replace the nbsp but not the leading "&" and the tail ";" and I get a result like "sunny&testing;Warm". As soon as I add in the "&" at the beginning of the str_replace, it doesn't work any more. It is either with the xxxx($newstring) or with how I call the str_replace(' ', " ", $what); Still trying...... Thanks for the suggestions. Quote Link to comment https://forums.phpfreaks.com/topic/141995-solved-replacing-nbsp-and-deg-in-php/#findComment-744254 Share on other sites More sharing options...
brent.fraser Posted January 23, 2009 Author Share Posted January 23, 2009 I GOT IT.... I GOT IT..... I looked around the net and found an example I copied: function slashit ($string) { $string = str_replace (''', '\'', $string); $string = str_replace ('%23', '\"', $string); $string = str_replace ('"', '\"', $string); $string = str_replace ('<', '<', $string); $string = str_replace ('>', '>', $string); $string = str_replace ('&', '&', $string); $string = str_replace (' ', ' ', $string); $string = stripslashes($string); return rawurlencode($string); } and it worked.... no more  :!!! Thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/141995-solved-replacing-nbsp-and-deg-in-php/#findComment-744455 Share on other sites More sharing options...
DeanWhitehouse Posted January 23, 2009 Share Posted January 23, 2009 You said str_replace didn't work,lol, next time show us the code of your attempts Quote Link to comment https://forums.phpfreaks.com/topic/141995-solved-replacing-nbsp-and-deg-in-php/#findComment-744461 Share on other sites More sharing options...
brent.fraser Posted January 23, 2009 Author Share Posted January 23, 2009 Hi, The part of the existing code $newstring = str_replace(' ', " ", $what); didn't work. I then tried to play with the: rawurldecode($string); urldecode($string); htmlspecialchars($string); htmlentities($string); and none of them worked as well. I then tried to look at some more str_replace and found an example. I just copied and pasted it into the code (along with the rawurldecode($string) and it worked. Still not sure why because the structure of the old and new str_replace looks the same. Old: function slashit($what){ //Encode text for storing in JavaScript array $newstring=str_replace(' ', ' ', $what);//replace those half valid apostrophe entities with actual apostrophes return rawurlencode($newstring); } New: function slashit ($string) { $string = str_replace (''', '\'', $string); $string = str_replace ('%23', '\"', $string); $string = str_replace ('"', '\"', $string); $string = str_replace ('<', '<', $string); $string = str_replace ('>', '>', $string); $string = str_replace ('&', '&', $string); $string = str_replace (' ', ' ', $string); $string = stripslashes($string); return rawurlencode($string); } seemed to do the trick. I didn't change any of the original code. I was only focusing on the : function slashit($what){ //Encode text for storing in JavaScript array $newstring=str_replace(' ', ' ', $what);//replace those half valid apostrophe entities with actual apostrophes return rawurlencode($newstring); } portion of it. Quote Link to comment https://forums.phpfreaks.com/topic/141995-solved-replacing-nbsp-and-deg-in-php/#findComment-744609 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.