Jump to content

[SOLVED] Replacing   and ° in PHP


brent.fraser

Recommended Posts

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 "&nbsp".

 

Any suggestions on how to replace the " " and "°" in the PHP script?

 

Thanks for the patience.....

 

Link to comment
https://forums.phpfreaks.com/topic/141995-solved-replacing-nbsp-and-deg-in-php/
Share on other sites

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

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.

I GOT IT.... I GOT IT.....

 

I looked around the net and found an example I copied:

function slashit ($string) {
$string = str_replace ('&#039;', '\'', $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 &nbsp:!!!

Thanks for the help!

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 ('&#039;', '\'', $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.

Archived

This topic is now archived and is closed to further replies.

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