I've got a problem with some specials characters in PHP. I have a table in mysql (utf8_hungarian_ci) that contains some text with special characters like á, á, Ó, Ö, ö, ü, and I would like to show this text on my page. I've tested:
$text = htmlentities($text); //to convert the simple spec chars
$search = array("& otilde;","&O tilde;","& ucirc;","&U circ;");
$replace = array("& #337;","& #336;","& #369;","& #368;");
$text = str_replace($search, $replace, $text);
echo $text;
But this code works only if $text isn't set from database. If I use this code and my $text is selected from database, it doesn't shows me any text, and if I only use:
echo $text; without htmlentities and replacements
I get characters like this one: �
I know there were some questions about this and I have tried accepted answers, but it still doesn't work, so please help me if you want and if you have time. Thank you anyway. A good day to you all!
Also try setting in your header to use UTF-8 encoding.
In your PHP file, add
header('Content-type: text/html; charset=utf-8');
as well as specifying the encoding to be UTF-8 in your <meta> tag, to ensure that you told the browser. And see if it fixes the issue.
As well as including UTF-8 encoding in your meta tag.
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
...
</head>
Edit:
If you have access to Apache configuration, see if AddDefaultCharset is set to another encoding.
Try using mysql_set_charset() (mysqli_set_charset() if you're using MySQLi).
Try to put this in you html header:
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
(Also, you may need to save your file in "utf-8" file encoding)
.
Secondly, you could use this to try to tranlate-or-remove the disturbing char that always prints out in your case:
$str_out = #iconv("ISO-8859-1", "UTF-8//TRANSLIT//IGNORE", $str_in);
This is a slightly generic answer but please read up this article I wrote on common character-encoding pitfalls in the PHP/MySQL stack and if you still have problems let's try to work through them.
http://webmonkeyuk.wordpress.com/2011/04/23/how-to-avoid-character-encoding-problems-in-php/
Related
I've tried converting the text to or from utf8, which didn't seem to help.
I'm getting:
"It’s Getting the Best of Me"
It should be:
"It’s Getting the Best of Me"
I'm getting this data from this url.
To convert to HTML entities:
<?php
echo mb_convert_encoding(
file_get_contents('http://www.tvrage.com/quickinfo.php?show=Surviver&ep=20x02&exact=0'),
"HTML-ENTITIES",
"UTF-8"
);
?>
See docs for mb_convert_encoding for more encoding options.
Make sure your html header specifies utf8
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
That usually does the trick for me (obviously if the content IS utf8).
You don't need to convert to html entities if you set the content-type.
Your content is fine; the problem is with the headers the server is sending:
Connection:Keep-Alive
Content-Length:502
Content-Type:text/html
Date:Thu, 18 Feb 2010 20:45:32 GMT
Keep-Alive:timeout=1, max=25
Server:Apache/2.2.8 (Ubuntu) PHP/5.2.4-2ubuntu5.7 with Suhosin-Patch
X-Powered-By:PHP/5.2.4-2ubuntu5.7
Content-Type should be set to Content-type: text/plain; charset=utf-8, because this page is not HTML and uses the utf-8 encoding. Chromium on Mac guesses ISO-8859-1 and displays the characters you're describing.
If you are not in control of the site, specify the encoding as UTF-8 to whatever function you use to retrieve the content. I'm not familiar enough with PHP to know how exactly.
I know the question was answered but setting meta tag didn't help in my case and selected answer was not clear enough, so I wanted to provide simpler answer.
So to keep it simple, store string into a variable and process that like this
$TVrageGiberish = "It’s Getting the Best of Me";
$notGiberish = mb_convert_encoding($TVrageGiberish, "HTML-ENTITIES", 'UTF-8');
echo $notGiberish;
Which should return what you wanted It’s Getting the Best of Me
If you are parsing something, you can perform conversion while assigning values to a variable like this, where $TVrage is array with all the values, XML in this example from a feed that has tag "Title" which may contain special characters such as ‘ or ’.
$cleanedTitle = mb_convert_encoding($TVrage->title, "HTML-ENTITIES", 'UTF-8');
If you're here because you're experiencing issues with junk characters in your WordPress site, try this:
Open wp-config.php
Comment out define('DB_CHARSET', 'utf8') and define('DB_COLLATE', '')
/** MySQL hostname */
define('DB_HOST', 'localhost');
/** Database Charset to use in creating database tables. */
//define('DB_CHARSET', 'utf8');
/** The Database Collate type. Don't change this if in doubt. */
//define('DB_COLLATE', '');
It sounds like you're using standard string functions on a UTF8 characters (’) that doesn't exist in ISO 8859-1. Check that you are using Unicode compatible PHP settings and functions. See also the multibyte string functions.
We had success going the other direction using this:
mb_convert_encoding($text, "HTML-ENTITIES", "ISO-8859-1");
Just try this
if $text contains strange charaters do this:
$mytext = mb_convert_encoding($text, "HTML-ENTITIES", 'UTF-8');
and you are done..
if all seems not to work, this could be your best solution.
<?php
$content="It’s Getting the Best of Me";
$content = str_replace("’", "'", $content);
echo $content;
?>
==or==
<?php
$content="It’s Getting the Best of Me";
$content = str_replace("’", "'", $content);
echo $content;
?>
try this :
html_entity_decode(mb_convert_encoding(stripslashes($text), "HTML-ENTITIES", 'UTF-8'))
For fopen and file_put_contents, this will work:
str_replace("’", "'", htmlspecialchars_decode(mb_convert_encoding($string_to_be_fixed, "HTML-ENTITIES", "UTF-8")));
You Should check encode encoding origin then try to convert to correct encode type.
In my case, I read csv files then import to db. Some files displays well some not. I check encoding and see that file with encoding ASCII displays well, other file with UTF-8 is broken. So I use following code to convert encoding:
if(mb_detect_encoding($content) == 'UTF-8') {
$content = iconv("UTF-8", "ASCII//TRANSLIT", $content);
file_put_contents($file_path, $content);
} else {
$content = mb_convert_encoding($content, 'UTF-8', 'UTF-8');
file_put_contents($file_path, $content);
}
After convert I push the content to file then process import to DB, now it displays well in front-end
If none of the above solutions work:
In my case I noticed that the single quote was a different style of single quote. Instead of ' my data had a ’. Notice the difference in the single quote? So I simply wrote a str_replace to replace it and it fixed the problem. Probably not the most elegant solution but it got the job done.
$string= str_replace("’","'",$string);
I looked at the link, and it looks like UTF-8 to me. i.e., in Firefox, if you pick View, Character Encoding, UTF-8, it will appear correctly.
So, you just need to figure out how to get your PHP code to process that as UTF-8. Good luck!
use this
<meta http-equiv="Content-Type" content="text/html; charset=utf8_unicode_ci" />
instead of this
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
If nothing works try this mb_convert_encoding($elem->textContent, 'UTF-8', 'utf8mb4');
My PHP script parses a web site and pulls out an HTML DIV that looks like this (and saves it as a string)
<div id="merchantinfo">The following merchants: Nautica®, Brookstone®, Teds® ©2012 Blabla</div>
I store this as $merchantList (string).
However, when I output the data to the webpage
echo $merchantList
The encoding gets messed up and displays as:
Nautica®, Brookstone®, Teds® ©2012 Blabla
I tried adding the following to the display page:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
But that didn't do anything. --Thanks
EDIT:: ------------
For the question, the accepted answer is correct.
But I realized my actual issue was slightly different.
The initial parsing using DOMDocument::loadHTML had already mangled the UTF-8 encoding, causing the string to save as
<div id="merchantinfo">The following merchants: Nauticaî, Brookstoneî, Tedsî ©2012 Blabla</div>
This was solved by:
$html = mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8");
$dom->loadHTML($html);
Use:
ini_set('default_charset', 'UTF-8');
And do not use iso-8859-1. Use UTF-8.
From the mojibake you posted the input string is utf-8, not iso-8859-1.
You need just to Use htmlspecialchars_decode function , exemple :
$string = '"hello dude"';
$decodechars = htmlspecialchars_decode($string);
echo $decodechars; // output : "hello dude"
There is a MySQL database containing data with accentuated letters like é. I want to display it in my PHP page , but the problem is that there are unrecognized characters displayed because of the accent. So is there a function to convert accent to HTML code , for example é is converted to é !
Rather than using htmlentities you should use the unicode charset in your files, e.g.
<?php
header('Content-Type: text/html; charset="utf-8"');
To be on the safe side, you can add the following meta tag to your html files:
<html>
<head>
<meta charset="utf-8" />
Then, make sure that your data base connection uses utf8:
mysql_connect(...);
mysql_select_database(...);
mysql_set_charset('utf-8');
Then, all browsers should display the special characters correctly.
The advantage is that you can easily use unicode characters everywhere in your php files - for example the copyright sign (©) or a dash (–) - given that your php files are encoded in utf-8, too.
Try htmlspecialchars() and/or htmlentities()
you can easily make one yourself with str_replace:
function txtFormat($input){
$output = str_replace('/\à/','É',$input);
$output = str_replace('/\è/','è',$output);
$output = str_replace('/\é/','é',$output);
$output = str_replace('/\ì/','ì',$output);
$output = str_replace('/\ò/','ò',$output);
$output = str_replace('/\ù/','ù',$output);
return $output;
}
Use the following code
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
And don't encode your data via utf8_encode() function before inserting into database
hope this will solve your problem :)
It's a capital A with a ^ on top: Â
It is showing up in strings pulled from webpages. It shows up where there was previously an empty space in the original string on the original site. This is the actual character that is stored in my database. It's also what displays on my website when I echo a string that contains it.
I realize it's a character encoding problem when I originally process the webpage, but I am now stuck with these characters in my database. I have to convert this character when it is displayed, or somewhere else in the php before outputting html that contains it. I cannot reprocess the original documents.
I have tried str_replace() and html_entity_decode() and neither do anything.
What else should I try?
"Latin 1" is your problem here. There are approx 65256 UTF-8 characters available to a web page which you cannot store in a Latin-1 code page.
For your immediate problem you should be able to
$clean = str_replace(chr(194)," ",$dirty)
However I would switch your database to use utf-8 ASAP as the problem will almost certainly reoccur.
This works for me:
$string = "Sentence ‘not-critical’ and \n sorting ‘not-critical’ or this \r and some ‘not-critical’ more. ' ! -.";
$output = preg_replace('/[^(\x20-\x7F)\x0A\x0D]*/','', $string);
It isn't really one character, and is likely caused by misalignment between content encoding and browser's encoding. Try to set the encoding of your outputted page to what you are using.
e.g. In the section, output:
echo "<META http-equiv='Content-Type' content='text/html; charset=UTF-8'>";
(Adjust UTF-8 to whatever you are using)
I use this one a lot
function cleanStr($value){
$value = str_replace('Â', '', $value);
$value = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $value);
return $value;
}
This is coming from database so the best option will be remove from database using a SQL query like:
UPDATE products SET description = REPLACE(description, 'Â', ' ');
Use Bellow codes
echo "<META http-equiv='Content-Type' content='text/html; charset=UTF-8'>";
echo htmlspecialchars_decode($your_string, ENT_QUOTES);
This problem occurs when using different charset in your web.
To solve this (using utf-8 in the examples):
in the <HEAD> of your page add charset:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
In any form you submit add accept-charset:
<form name="..." method=".." id=".." accept-charset="utf-8">
If you are using php+MySQLi to process your form, you should make sure the database connection is also supporting your charset. Procedural style:
mysqli_set_charset($link, "utf8");
and object oriented style:
$mysqli->set_charset("utf8")
I Actually had to have all of this:
<--!DOCTYPE html-->
<--html lang="en-US"-->
<--head-->
<--meta charset="utf-8"-->
<--meta http-equiv="X-UA-Compatible" content="IE=edge"-->
<--meta name="viewport" content="width=device-width, initial-scale=1"-->
<--meta http-equiv="Content-Type" content="text/html; charset=utf-8/" /-->
To remove â character from string
mysqli_set_charset($con,"utf8");
$price = "₹ 250.00";
$price2 = preg_replace('/[^(\x20-\x7F)]*/','', $price);
Result : 250.00
I've tried converting the text to or from utf8, which didn't seem to help.
I'm getting:
"It’s Getting the Best of Me"
It should be:
"It’s Getting the Best of Me"
I'm getting this data from this url.
To convert to HTML entities:
<?php
echo mb_convert_encoding(
file_get_contents('http://www.tvrage.com/quickinfo.php?show=Surviver&ep=20x02&exact=0'),
"HTML-ENTITIES",
"UTF-8"
);
?>
See docs for mb_convert_encoding for more encoding options.
Make sure your html header specifies utf8
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
That usually does the trick for me (obviously if the content IS utf8).
You don't need to convert to html entities if you set the content-type.
Your content is fine; the problem is with the headers the server is sending:
Connection:Keep-Alive
Content-Length:502
Content-Type:text/html
Date:Thu, 18 Feb 2010 20:45:32 GMT
Keep-Alive:timeout=1, max=25
Server:Apache/2.2.8 (Ubuntu) PHP/5.2.4-2ubuntu5.7 with Suhosin-Patch
X-Powered-By:PHP/5.2.4-2ubuntu5.7
Content-Type should be set to Content-type: text/plain; charset=utf-8, because this page is not HTML and uses the utf-8 encoding. Chromium on Mac guesses ISO-8859-1 and displays the characters you're describing.
If you are not in control of the site, specify the encoding as UTF-8 to whatever function you use to retrieve the content. I'm not familiar enough with PHP to know how exactly.
I know the question was answered but setting meta tag didn't help in my case and selected answer was not clear enough, so I wanted to provide simpler answer.
So to keep it simple, store string into a variable and process that like this
$TVrageGiberish = "It’s Getting the Best of Me";
$notGiberish = mb_convert_encoding($TVrageGiberish, "HTML-ENTITIES", 'UTF-8');
echo $notGiberish;
Which should return what you wanted It’s Getting the Best of Me
If you are parsing something, you can perform conversion while assigning values to a variable like this, where $TVrage is array with all the values, XML in this example from a feed that has tag "Title" which may contain special characters such as ‘ or ’.
$cleanedTitle = mb_convert_encoding($TVrage->title, "HTML-ENTITIES", 'UTF-8');
If you're here because you're experiencing issues with junk characters in your WordPress site, try this:
Open wp-config.php
Comment out define('DB_CHARSET', 'utf8') and define('DB_COLLATE', '')
/** MySQL hostname */
define('DB_HOST', 'localhost');
/** Database Charset to use in creating database tables. */
//define('DB_CHARSET', 'utf8');
/** The Database Collate type. Don't change this if in doubt. */
//define('DB_COLLATE', '');
It sounds like you're using standard string functions on a UTF8 characters (’) that doesn't exist in ISO 8859-1. Check that you are using Unicode compatible PHP settings and functions. See also the multibyte string functions.
We had success going the other direction using this:
mb_convert_encoding($text, "HTML-ENTITIES", "ISO-8859-1");
Just try this
if $text contains strange charaters do this:
$mytext = mb_convert_encoding($text, "HTML-ENTITIES", 'UTF-8');
and you are done..
if all seems not to work, this could be your best solution.
<?php
$content="It’s Getting the Best of Me";
$content = str_replace("’", "'", $content);
echo $content;
?>
==or==
<?php
$content="It’s Getting the Best of Me";
$content = str_replace("’", "'", $content);
echo $content;
?>
try this :
html_entity_decode(mb_convert_encoding(stripslashes($text), "HTML-ENTITIES", 'UTF-8'))
For fopen and file_put_contents, this will work:
str_replace("’", "'", htmlspecialchars_decode(mb_convert_encoding($string_to_be_fixed, "HTML-ENTITIES", "UTF-8")));
You Should check encode encoding origin then try to convert to correct encode type.
In my case, I read csv files then import to db. Some files displays well some not. I check encoding and see that file with encoding ASCII displays well, other file with UTF-8 is broken. So I use following code to convert encoding:
if(mb_detect_encoding($content) == 'UTF-8') {
$content = iconv("UTF-8", "ASCII//TRANSLIT", $content);
file_put_contents($file_path, $content);
} else {
$content = mb_convert_encoding($content, 'UTF-8', 'UTF-8');
file_put_contents($file_path, $content);
}
After convert I push the content to file then process import to DB, now it displays well in front-end
If none of the above solutions work:
In my case I noticed that the single quote was a different style of single quote. Instead of ' my data had a ’. Notice the difference in the single quote? So I simply wrote a str_replace to replace it and it fixed the problem. Probably not the most elegant solution but it got the job done.
$string= str_replace("’","'",$string);
I looked at the link, and it looks like UTF-8 to me. i.e., in Firefox, if you pick View, Character Encoding, UTF-8, it will appear correctly.
So, you just need to figure out how to get your PHP code to process that as UTF-8. Good luck!
use this
<meta http-equiv="Content-Type" content="text/html; charset=utf8_unicode_ci" />
instead of this
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
If nothing works try this mb_convert_encoding($elem->textContent, 'UTF-8', 'utf8mb4');