Welcome Guest, Not a member yet? Register   Sign In
Character encoding changes suddenly
#1

[eluser]Swedie[/eluser]
New to CI and started making a method that uses views to display headers and footers from another website of mine. I've encountered a problem, maybe not specifically a CI issue, but an issue nevertheless that I must solve.

The problem being that if I include the header and footer, the swedish characters are turned into symbols:


* ���
* ���
* �#%�$?

If I leave out the header and footer, the swedish characters show up correctly. Thus the file_get_content or CI causes something to change. Why and how do I do this right?

Here is all my code!

Controller
Code:
<?php

/**
* Description of test
*
* @author Johnny
*/
class Test extends Controller {

   var $website_url = "http://localhost:88";

   function Test() {
      parent::Controller();
   }


   function index()
   {
      $this->setHeaderAndFooter();
      $data = array(
         "header" => $this->header,
         "footer" => $this->footer,
         "title" => "Sveriges MotorCyklister",
         "swedish_characters" => array("åäö", "ÅÄÖ", "¤#%£$€")
      $this->load->view('test', $data);
   }

   function setHeaderAndFooter()
   {
      /* Scans main website for break points to put content
       * @todo Save cache to db or file for faster and better control
       */

      $page = file_get_contents($this->website_url);
      $bits = explode("<!-- content start -->", $page);
      $this->header = str_replace('"/', '"' . $this->website_url . '/', $bits['0']);
      $bits = explode("<!-- content end -->", $bits['1']);
      $this->footer = str_replace('"/', '"' . $this->website_url . '/', $bits['1']);
   }

}

?>

The view file
Code:
<?=$header;?>

<ul>
&lt;?php foreach($swedish_characters as $bit): ?&gt;
<li>&lt;?=$bit;?&gt;</li>
&lt;?php endforeach; ?&gt;
</ul>

&lt;?=$footer;?&gt;
#2

[eluser]Dennis Rasmussen[/eluser]
file_get_contents() will cause your site to use the encoding used on the website you are fetching data from (if that website has meta data), so you probably need to convert your fetched data/file to use utf-8 as well.

Code:
$page = file_get_contents($this->website_url);
$page = mb_convert_encoding($page, 'UTF-8', mb_detect_encoding($page, 'UTF-8, ISO-8859-1', true));

If that doesn't work, then I don't know Smile
#3

[eluser]Swedie[/eluser]
That didn't work.

But changing the meta tag to ISO-8859-1 on the other side, plus doing a utf8_encode on $page made it appear correctly.

Not sure though that this is the best of ways. I rather not have to change it like this.
#4

[eluser]Swedie[/eluser]
Noticed I had changed the project's character encoding. This caused all files to be ISO-5589-1 making a conflict with all other pages I have that are UTF-8. Atleast I found the cause.

I post this for future reference to everyone else that might stumble across the same dilemma.




Theme © iAndrew 2016 - Forum software by © MyBB