CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Feb 2011
    Posts
    32

    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.

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    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.

  3. #3
    Join Date
    Feb 2011
    Posts
    32

    Re: How to change encoding with simplexml_load_file()?

    Quote Originally Posted by PeejAvery View Post
    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?

  4. #4
    Join Date
    May 2002
    Posts
    10,943

    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.

  5. #5
    Join Date
    Feb 2011
    Posts
    32

    Re: How to change encoding with simplexml_load_file()?

    Quote Originally Posted by PeejAvery View Post
    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.

  6. #6
    Join Date
    May 2002
    Posts
    10,943

    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.

  7. #7
    Join Date
    Feb 2011
    Posts
    32

    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>

  8. #8
    Join Date
    May 2002
    Posts
    10,943

    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.

  9. #9
    Join Date
    Feb 2011
    Posts
    32

    Re: How to change encoding with simplexml_load_file()?

    Quote Originally Posted by PeejAvery View Post
    I'm not seeing any invalid characters.
    Hmm, then could it be because of the server I use?

  10. #10
    Join Date
    May 2002
    Posts
    10,943

    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.

  11. #11
    Join Date
    Feb 2011
    Posts
    32

    Re: How to change encoding with simplexml_load_file()?

    Quote Originally Posted by PeejAvery View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured