Codeigniter UTF-8 in loaded view within another view - php

I'm having a strange issue with loaded views in Codeigniter 3.0.1.
I'm trying to display Spanish characters within my application. I have set these settings below to get Spanish displaying properly in the initial view loaded from my controller.
This is set in the main index.php of codeigniter.
header('Content-Type: text/html; charset=utf-8');
I also have set this on the top of my HTML page mark up.
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
So Spanish characters in the main HTML page is displaying properly. The problem is when I try to load another view inside the main HTML file.
<html>
<body>
spanish here //displays properly
<?php $this->load->view("page/somepage"); //Spanish in view does not format properly ?>
</body>
</html>
Any Spanish characters loaded inside this view is not formatting properly.
Anybody know a work around?

Related

dompdf working fine with non-latin languages but for Arabic, the words are broken not proper

I am using the dompdf library with CodeIgniter PHP and in my application, I have to convert the HTML to PDF through DOMPDF but I am facing issue while the HTML content contains the Arabic text.
Have you included meta tag ?
Please write this line at top of your view file if not there.
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

Dropdown is not listing French characters

I have a dropdown list, which lists things that has french characters in it. Although I see characters like this:
MySQL database is set to UTF-8. So, there is no problem in database. I've checked the database and characters are okay.
I also added these lines to my header.php but still doesn't work.
header('Content-Type: text/html; charset=utf-8');
<meta charset="UTF-8">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
What else should I do?
EDIT
I can print french letters everywhere in the site but still dropdown doesn't work.
SOLUTION
First of all, if you would like to use utf-8 in your site you should add this right after you connect to database:
mysqli_set_charset($connectionName, 'utf8mb4');
Second, you should also add this in your header tags:
<meta charset="utf-8">
That's all. After all these, my problem still wasn't fixed until I figure out my stupidity. I have header.php file, which is included on top of every page but there is another php file, which outputs dropbox options from database. So, this file don't need header.php at top. So, I had to add mysqli_set_charset($connectionName, 'utf8mb4'); specially for that page.

php characters in pdf turn to question marks when set to utf-8

I'm using the "wordpress pdf templates" plugin.
In my php pdf generating code, I set the type to utf-8 with this:
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
When I do this, it turns the word workers' to:
workers?
If I remove the utf-8 designation it looks like this:
workers’
My code is very simple and looks like this:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>
<body>
<div class="post-content">
<?php the_content(); ?>
</div>
</body>
</html>
What am I doing wrong?
This could be because of an encoding mismatch.
One of the places to check is your wp-config.ph since that is one place that the encoding is also defined. Ref: https://www.tipsandtricks-hq.com/how-to-fix-the-character-encoding-problem-in-wordpress-1480
Also note that you have a potential for problems to occur anywhere that one part of your system talks to another. Some of these components are:
Your editor that you’re creating the PHP/HTML files in
The web browser people are viewing your site through
Your PHP web application running on the web server
The MySQL database
Anywhere else external you’re reading/writing data from (memcached, APIs, RSS feeds, etc)
Ref:
https://webmonkeyuk.wordpress.com/2011/04/23/how-to-avoid-character-encoding-problems-in-php

character encoding of the HTML document was not declared codeigniter

My CodeIgniter app has started to a freak error:
The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must be declared in the document or in the transfer protocol.
In the first visit, the page appears fine, just a message "guimbu". If you refresh the page, the error appears until you clear the history of browser.
This happens only after second page-view. Take a look: http://guimbu.com/soon
I've just opened ALL FILES with Sublime and saved to UTF-8; twice.
I've just tried with all charset meta tags;
This happens with ALL pages, including this simple page;
My autoload.php has this:
$autoload['packages'] = array();
$autoload['libraries'] = array('session', 'database', 'firephp', 'auth');
$autoload['helper'] = array('url', 'log_helper', 'api_helper', 'string_helper');
$autoload['config'] = array('ci_opauth', 'rest', 'ci_auth');
$autoload['language'] = array();
$autoload['model'] = array();
How can I debug this problem?
UPDATE:
#sudhir, thanks for the answer, but I tried this many times. Actually the source code is just:
<!DOCTYPE html>
<head>
<title>guimbu</title>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
</head>
<body>
guimbu =)
</body>
</html>
And the error persists after second page-view =/
Maybe a problem with sessions?
UPDATE with possibility:
When I remove session and auth libraries from autoload, the problem doesn't happen. WTF? auth is a library I made for user authentication.
When you refresh that page you get a blank one, if you view source that page you'll see it's blank that is has a number count. My guess is that you're doing something when the page refreshes that sends a white space to the browser. I can only suggest not to close your files with ?> and let php handle how your scripts will exit/die. There's some whitespace in your code that's causing that.
try adding these in head section of your view:
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
FOUND SOLUTION
In the first lines of Session class constructor, it loads the String helper.
Yesterday I've created a helper called string_helper.php and this conflict was what crashing application.
I renamed my string_helper.php file and now everything works fine.
Thanks all!

PHP Japanese echo string becomes question marks

I'm working on a php site that needs both Japanese and English. In most places I have no trouble getting the Japanese to display correctly, but I can't get the characters to display if I use a function like this:
echo.php:
<?php
function draw(){
echo "日本語";
}
draw();
?>
I get "日本語"
but if I try this :
index.php:
<?php
some stuff
include "echo.php";
draw();
?>
I get "???".
Is there any way for me to get my echo file to send the data to the first file in a way it can read it?
EDIT:
The website works, and shows Japanese characters correctly, except when it tries to pull a function from a php file
Here's the code:
<html lang="ja">
<head>
<title>Running Projects</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body>
<div id="header">
<? php include "layout.php" ?>
</div>
</body>
</html>
Where layout.php is just a file with a list of links, I have this on each page so the links are on every page.
For a different part, I have to use functions.php to get some data from a database and write it to the page, so, I tried putting layout.php into the functions.php and calling it: The English links appeared, but the Japanese links appeared as question marks.
You should change your file encoding to UTF-8 and set the header on the website to UTF-8.
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
in HTML5 you should use:
<meta charset="utf-8" />
or in php
header('content-type: text/html; charset=utf-8');
You problem definitely has something to do with the character encoding. Try to check the following:
All your strings in all PHP scripts are Unicode (UTF-8 is a very common choice). Use utf8_encode() and/or utf8_decode() to force UTF-8 on your strings where necessary.
Your server sends PHP output as Unicode (UTF-8 and preferably, but not necessarily, gzipped data)
Your browser understands and accepts Unicode (UTF-8). Typically browser would send Accept-Charset: UTF-8,*;q=0.5 in the GET request to hint it's Unicode capability.
Finally, I have checked your code with PHP version 5.3.6 and Firefox, Chrome and IE 9. Firefox and Chrome prints the Japanese characters as expected. It's only IE 9 which doesn't print it correctly. Snooping on the GET request from IE reveals, it is indeed not sending any Accept-Charset: UTF-8,*;q=0.5. I am not entirely sure how to force IE to send that because in my case, clearly my server (Apache) together with PHP was definitely sending UTF-8 string back.
One more hint - you may try converting your strings into HTML entities. For example - echo "©"; would print ©. But, I am not 100% sure how to convert all strings into HTML entities using PHP. I have unsuccessfully attempted with htmlentities() and htmlspecialchars(). But, it didn't change anything for IE.
i have same problem. But i handle it.
convert "echo.php"'s encoding to EUC-JP or SHIFT-JIS.
<?php
function draw(){
echo mb_convert_encoding("日本語", 'UTF-8', array('EUC-JP', 'SHIFT-JIS', 'AUTO'));
}
draw();
?>
This works for me :)
That happens when charset is not defined or is incorrect.
You can use meta tags to define the charsets. Place the following meta tags as needed on the head section of the page, and It will be rendered correctly.
HTML 4
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
HTML 5
<meta charset="utf-8" />
add below meta tags to header
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta http-equiv="Content-Language" content="ja" />
MySQL and PHP need to be handling the same character set.
Try adding ..
mysqli_set_charset("utf8");
.. after connecting to your database. Always works for me!
If you are using php and mssql you will also need to specify characterset in the mssql connection string.
Like this:
$connectionInfo = array("Database"=>"db name", "UID"=>"username", "PWD"=>"pw","CharacterSet" => "UTF-8");
$serverName = "server";
$conn = sqlsrv_connect($serverName, $connectionInfo);
Please note you still need to do the things suggested above
declare your html charset like this HTML 5:
<meta charset="utf-8" />
php charset like this:
header('content-type: text/html; charset=utf-8');

Categories