|
-
April 20th, 2011, 03:34 PM
#1
How to change encoding with simplexml_load_file()?
Hi, I'm using simplexml_load_file() on a RSS feed to then get the titles. The problem is that when for example a ' appears on the title, strange characters appear instead of '. How do I fix this :S? Thanks.
-
April 20th, 2011, 10:43 PM
#2
Re: How to change encoding with simplexml_load_file()?
That means the file you're loading is not UTF-8. Since SimpleXML was created to read/write UTF-8, the file your reading needs to be of that character encoding.
An alternative...which will be slower processing, is to read the file using file_get_contents(), convert to UTF-8, and then use simplexml_load_string().
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
April 21st, 2011, 04:43 AM
#3
Re: How to change encoding with simplexml_load_file()?
 Originally Posted by PeejAvery
That means the file you're loading is not UTF-8. Since SimpleXML was created to read/write UTF-8, the file your reading needs to be of that character encoding.
An alternative...which will be slower processing, is to read the file using file_get_contents(), convert to UTF-8, and then use simplexml_load_string().
I tried that, but there are still strange characters instead of '. The code is:
Code:
$temp = mb_convert_encoding( file_get_contents("feed_url"), 'UTF-8' );
$feed = simplexml_load_string($temp);
I also tried utf8_encode() instead of mb_convert_encoding and the same happened.
When I get the title I send it by e-mail and I see it by e-mail, could the problem be that?
-
April 21st, 2011, 07:57 AM
#4
Re: How to change encoding with simplexml_load_file()?
What is the character encoding of the e-mail?
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
April 21st, 2011, 09:02 AM
#5
Re: How to change encoding with simplexml_load_file()?
 Originally Posted by PeejAvery
What is the character encoding of the e-mail?
I wrote the email's header like this:
Code:
$headers = "Content-type: text/html; charset=UTF-8\r\n";
It shouldn't be the problem though, I just tried using echo and the strange characters appear.
Last edited by Toshioo; April 21st, 2011 at 09:05 AM.
-
April 21st, 2011, 10:17 AM
#6
Re: How to change encoding with simplexml_load_file()?
Save the file and upload it here.
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
April 21st, 2011, 01:50 PM
#7
Re: How to change encoding with simplexml_load_file()?
Ok, here it is:
Code:
<html>
<body>
<?php
$max_news = 5;
$i = 0;
$str = file_get_contents("http://feeds.feedburner.com/PokerNewsDaily?format=xml");
$temp = mb_convert_encoding( $str, "UTF-8" );
$feed = simplexml_load_string($temp);
foreach ($feed -> channel -> item as $item )
{
if ($i > $max_news)
break;
sleep(10);
$subject = $item -> title;
$link = $item -> link;
$description = $item -> description;
$body = "<html><body>" . $description . "<br /><br /><i>PokerDailyNews</i>: <a href=" . $link . ">Read the full report</a></body></html>";
$to = "[email protected]";
$headers = "Content-type: text/html; charset=UTF-8\r\n";
if ( mail($to, $subject, $body, $headers) )
{
echo "<p>" . $subject . " - sent</p>";
}
else
{
echo "<p>" . $subject . " - NOT sent</p>";
}
$i++;
}
?>
</body>
</html>
-
April 21st, 2011, 08:06 PM
#8
Re: How to change encoding with simplexml_load_file()?
I'm not seeing any invalid characters.
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
April 22nd, 2011, 04:45 AM
#9
Re: How to change encoding with simplexml_load_file()?
 Originally Posted by PeejAvery
I'm not seeing any invalid characters.
Hmm, then could it be because of the server I use?
-
April 23rd, 2011, 08:06 AM
#10
Re: How to change encoding with simplexml_load_file()?
I'd doubt it.
Is your PHP document saved in UTF-8? Are you also outputting a UTF-8 header?
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
April 23rd, 2011, 03:05 PM
#11
Re: How to change encoding with simplexml_load_file()?
 Originally Posted by PeejAvery
I'd doubt it.
Is your PHP document saved in UTF-8? Are you also outputting a UTF-8 header?
It's good now The problem was that the internal encoding wasn't UTF-8, I wrote this in the beggining of the file:
Code:
header('Content-Type:text/html; charset=UTF-8');
And I didn't have to convert the string, because the feeds are in UTF-8.
Thanks a lot for your help
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|