I have a small problem, I'm using a .php page to show Russian characters. Now, I do this in two different ways. The text I take from the database shows fine, but if I show text like this
<h2>привет </h2> it doesn't show it at all. One thing worth mentioning is that I include my navigation file require_once "navigation.php" where I declare all the meta data etc.
So from navigation.php:
<meta charset="Cyrillic(Windows-1251)">
Solutions I've tried so far:
Adding encoding to .htaccess
Using php to output header encoding
Using Cyrillic(Windows-1251), UTF8, UTF16, iso-8859-5
Saving the file itself as UTF8
And I'm out of ideas. What I don't understand is how echoing the text from database works, but the HTML version doesn't.
Here this should work
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-5">
<h2>привет </h2>
So the problem was somewhere in the JS. For this project, I was given complete HTML works, just to hook PHP to it. The HTML, among other things, had this:
<script type="text/javascript" src="js/cufon-yui.js"></script>
<script type="text/javascript" src="js/cufon-replace.js"></script>
I have no idea what it does except it makes fonts smaller (why use JS for that is beyond me). So simple removal of that left me with a bit bigger font size, but working Cyrillic characters.
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
Related
I have a php page. The code is:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<meta name="viewport" content="width=device-width">
<?php include("some_file.php"); ?>
<title>Calculadora dos Custos do Automóvel</title>
</head>
<body>
<p>aaa</p>
</body>
</html>
As you can see nothing is wrong.
Though, view source of Firefox gives red erros and w3 validator gives also errors.
I'm around this for two days, and it is so strange. I suppose it has to do with some strange characters php might be inputting into the HTML code but I'm not sure. Or perhaps the encoding considering that the code has Portuguese characters.
There are two BOMs just before the <title> tag. Remove them (e.g. delete everything between the second <meta> and the <title>) and everything should work fine.
From what I see here you are using Notepad++ and we already cleared that the problem is because of BOM of the php files that you include before title.
To solve this for each file that you use open in Notepad++ and from menu choose Encoding -> Encode in UTF-8 without BOM and save the file. Do this for all your files in the probject (php files).
You should not have anymore that special characters in front of title.
Also make sure you start the <?php tag at the beginning of the file (no white spaces or new lines before) and if the file contains only php you don't have to use closing ?> tag. This way you are sure that you will not have spaces at the end of the file.
As others have said BOMs are a problem when including PHP files, even if those files do not echo anything
PHP includes
At the time this article was written, if you include some external
file in a page using PHP and that file starts with a BOM, it may
create blank lines.
This is because the BOM is not stripped before inclusion into the
page, and acts like a character occupying a line of text.
You should ensure that the included files do not start with a BOM.
in: http://www.w3.org/International/questions/qa-byte-order-mark
Detecting BOMs
You can online detect BOMs within the content of your HTML page using the W3C Internationalization Checker
There is something before
<title>
remove everything before title tag
try to save your file with UTF-8 encoding
You certainly have a problem with the white space before in your tag. Please rewrite your code afresh using your replacing the spaces with spaces from your keyboard.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<meta name="viewport" content="width=device-width">
<title>Calculadora dos Custos do Automóvel</title>
</head>
<body>
<p>aaa</p>
</body>
</html>
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.
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
I am wondering why php does certain things twice, instead of once, when a certain meta tag is in the html portion of the file and the file is browsed by Firefox.
The code is like this:
<? /*...normal php code, including writing record to MySQL...*/
send('dan#example.com',$subject,$body);
?>
<!DOCTYPE html><html>
<!--PROBLEM on next line-->
<META http-equiv=Content-Type content="text/html; charset=utf-8">
<head>
<title><?= $thisPage?></title>
<link href="<?= $cssURL?>css.freedom-and-purpose.css" rel="stylesheet" type="text/css" media="screen, projection" />
<?
include $dataPath . 'data.php';
?>
The result is TWO records written the database and TWO emails sent, whenever the page is called by FIREFOX. IE and Chrome not producing the problem.
There is a lot of other code in the program, but the reason I showed the portion above is that removing the line that starts with
<META...
solves the problem.
That meta tag is in there because one of the packages I run included it in their code sample.
So, what is that meta tag causes php to double do on DB-writes? And same thing on sending email?
Chances are this is actually a request for favicon.ico being caught by your main PHP file. Putting an empty file in favicon.ico or preventing your PHP from handling that URL should do the trick
enter code hereI would suggest you go through your code in some details and check its formatting.
Phil mentioned the meta tag about which I agree with. His suggestion of <meta charset="utf-8"> would be my preference.
Secondly the line sending the email looks odd. Single quotes aren't an option in php for data replacement, so the line send('dan#example.com','$subject','$body); would result in an email with the subject "$subject" and body just "$body".
Additionally send('dan#example.com','$subject','$body); appears to be missing a quote after $body.
I would advise you to move away from short php tags for opening and closing chunks of php <? ?> and get in the habit of <?php for clarity and to ensure the server you're using processes the code correctly.
Finally, I hope include $dataPath . 'data.php'; adds a </head> and a <body> to the html, as you're currently missing those too.
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');