edfialk Posted March 6, 2009 Share Posted March 6, 2009 Hi all, got an unusual problem I'm really hoping I can get some help with. I'm using: PHP 5.2.8 (cli) (built: Dec 13 2008 18:38:00) and Apache After writing a big long script to retrieve an rss and do some things with it, I noticed some special characters appearing that broke my xml display. I made a small php file to test: $xmlobj = simplexml_load_file("http://pipes.yahoo.com/pipes/pipe.run?_id=d4e3810bb02299384206deb47536ac5c&_render=rss"); print header("Content-type: text/plain"); print $xmlobj->channel->item[0]->description; which, when viewed in Apache, results in: DES MOINES, Iowa —Adopting California-style vehicle emissions standards might be Iowa’s best... but when viewed in php from command line results in: DES MOINES, Iowa âAdopting California-style vehicle emissions standards might be Iowaâs best... Where did those come from?! if I copy that exact php file to another location (even same directory), then immediately run it from Apache, I get the results with special characters. One script works correctly in apache, but not command line. Exact same script renamed doesn't work in either. To be honest, I'm pretty amazed I got it to print out correctly the first time, since I can't seem to do it again in a fresh file or my long script. I'm too scared to modify the script that works correctly because I might break it. I want the hyphen and single quotes to come through, reliably, every time. Anyone have any ideas? Thanks for any and all suggestions! -Ed Link to comment https://forums.phpfreaks.com/topic/148278-printing-retrieved-xml-hyphen-and-quotes-turning-into-special-characters/ Share on other sites More sharing options...
WolfRage Posted March 8, 2009 Share Posted March 8, 2009 The command line is not interuprting the source encoding and that is why the special characters are being displayed. Can you post the code that grabs and outputs the xml from both of the documents so we can compare and contrast, and maybe we will be able to provide a more in depth reason and possible solutions. Link to comment https://forums.phpfreaks.com/topic/148278-printing-retrieved-xml-hyphen-and-quotes-turning-into-special-characters/#findComment-779493 Share on other sites More sharing options...
edfialk Posted March 10, 2009 Author Share Posted March 10, 2009 Well, I edited the one file that occasionally worked, so I have nothing successful to compare it to. Either way, the code was the same in both places, now they're both officially acting the same way. Unfortunately, not the way I'd like. So: <?php $file = file_get_contents("http://pipes.yahoo.com/pipes/pipe.run?_id=d4e3810bb02299384206deb47536ac5c&_render=rss"); $xmlobj = simplexml_load_string($file); print header("Content-type: text/plain"); print $xmlobj->channel->item[0]->description; ?> is hosted at http://pocus.wustl.edu/delicious/simplexml.php and prints out: (Washington, D.C. – March 10, 2009) The U.S. Environmental Protection Agency both in command line and in Apache. I need it to print out: (Washington, D.C. – March 10, 2009) The U.S. Environmental Protection Agency which is description[0] (until something new is posted) directly from the source: http://pipes.yahoo.com/pipes/pipe.run?_id=d4e3810bb02299384206deb47536ac5c&_render=rss The problem, again, is I'm grabbing items from the feed, inserting into database, reading later with php from the database, printing out xml, reading from javascript, and doing some displaying. When I read items from the database and print out xml, the xml breaks as soon as a single one of these characters appears, breaking my javascript display. So I need to either allow the xml to not break if it sees a character, which I can't figure out how to do, or stop the characters from showing up. Any other questions I'll try to answer asap. Thanks for any suggestions! -Ed Link to comment https://forums.phpfreaks.com/topic/148278-printing-retrieved-xml-hyphen-and-quotes-turning-into-special-characters/#findComment-781300 Share on other sites More sharing options...
WolfRage Posted March 11, 2009 Share Posted March 11, 2009 Can you try using htmlspecialentities() before the code is inserted into the xml? Maybe that will protect your xml document and when it is latter displayed in html it should still be presented correctly. Link to comment https://forums.phpfreaks.com/topic/148278-printing-retrieved-xml-hyphen-and-quotes-turning-into-special-characters/#findComment-781835 Share on other sites More sharing options...
edfialk Posted March 12, 2009 Author Share Posted March 12, 2009 Hey WolfRage, thanks for the suggestion. I am actually using htmlentities() when printing the xml. Here's one example: here's normal: CLEVELAND — Cliffs Natural Resources announced Tuesday htmlspecialchars(): CLEVELAND — Cliffs Natural Resources announced Tuesday htmlentities(): CLEVELAND — Cliffs Natural Resources announced Tuesday still has that one weird character. edit: here's what it's supposed to be: CLEVELAND — Cliffs Natural Resources announced Tuesday retrieved through rss from the site: http://www.virginiamn.com/articles/2009/03/12/news/doc49b723040ada7276745703.txt Link to comment https://forums.phpfreaks.com/topic/148278-printing-retrieved-xml-hyphen-and-quotes-turning-into-special-characters/#findComment-782938 Share on other sites More sharing options...
edfialk Posted March 13, 2009 Author Share Posted March 13, 2009 $string = iconv( "UTF-8", "ISO-8859-1//TRANSLIT", $string ); before inserting into database or before printing xml, works, changes characters, fixes everything. solved ? Link to comment https://forums.phpfreaks.com/topic/148278-printing-retrieved-xml-hyphen-and-quotes-turning-into-special-characters/#findComment-783884 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.