Help needed in choosing a function in PHP - php

Well i am trying to make XML from database..but one the field has the following value
Noobé
As you can see there is small dash like above 'e'
the output i obtained is Noobe?
I tried to utf8_encode the field but it does not work
I even tried to utf8 in header it did not work..
Can u guys suggest me a function by which i can overcome the above problem..
I also have a similar problem regarding a '–' obtained by Microsoft word
Help Appreciated..

I had a problem with accented characters not long ago. What worked for me was to add this line after my PHP code used to connect to the database :
mysql_query("SET NAMES 'utf8'");

What is the encoding of the HTML page?
I have a french website that have this in the header:
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
And I can see all é à and others characters.

The problem is not encoding, as the "é" will become something like "À€". You may have something in you code that remove accents, maybe like http://php.net/manual/fr/function.strtr.php

Related

PHP/MySQL Special Characters aren't displayed properly [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Special characters in PHP / MySQL
I have a problem. I have a piece of text in my database (MySQL 5.5.20) with characters like 'é' and " ' " who aren't displaying properly after executing the MySQL query and displaying it with echo($...). With every special character I've inputted in the database, it displays a small question mark inside a diamond. If I look at the text in the database itself, it is an 'é' and " ' ", so I figured the problem isn't MySQL.
One thing I could do is str_replace everything like " ' " --> "'" on input, but then I have to do this for every character there is.
Oh and I already have included
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
and this didn't work.
Hopefully you've all the information to help me, if not just say :) Thanks in advance!
Milaan
You need to have everything in utf-8:
The database field
The database connection (mysql_set_charset('utf8'); in classic mysql, something like $db->exec('SET CHARACTER SET utf8'); in PDO)
The content type (like you have already)
I was using the SQL query SET NAMES utf8 right after the connection to a DB is done successfully for over a years.
But this is not neccessary when You have everything in the same encoding
source files encoding
table columns collations
web page encoding (both in PHP header('Content-Type: text/html; charset=utf-8'); and in <header> <meta name="Content-Type" value="text/html; charset=utf-8" />)
I usually format all the input text with str_replace an replace all uncommon symbols with their &#xxx; equivalent, this is actually useful to prevent injection and bad html rendering
i.e. if someone inputs html tags they'll be active in your page and so on.

Character encoding in PHP

I never had this problem before, it was usually my database or the html page. But now i think its my php. I import text from a csv or from a text area and in both ways it goes wrong.
for example é changes to é. I used htmlentities to fix this but it didn't work. The htmlentities function didn't return é in html but é in html entities, so it already loses the real characters before htmlentities comes in to place... So does that mean my php file has the wrong encoding or something?
I hope someone can help me out..
Thanks!
Chris
A file is usually ISO-8859-1 (Latin) or UTF-8 ... ISO-8859-1 is 1 byte per char, UTF-8 is 1-4 bytes per char. So if you get 2 chars when you expect one, then you are reading UTF-8 and showing it as ISO-8859-1 ... if you get strange chars, then you are reading ISO-8859-1 and showing it as UTF-8.
If you provide more details, it would be easier to pinpoint, but in short, you have inconsistent charsets and need to convert one or the other so they're all the same. But from what it seems, you're using ISO-8859-1 in your project, but you are reading some UTF-8 from somewhere... use utf8_decode($text) if that data should be indeed be stored as UTF-8, or find the data and convert it manually.
EDIT: If you are using AJAX somewhere, then you will ALWAYS get UTF-8 from it, and you'll have to decode it yourself with utf8_decode() if you want to keep using ISO-8859-1.
Try opening your php file and change the encoding to UTF-8
if that doesn't help, add this to your php:
header('Content-Type: text/html; charset=utf-8');
Or this to your html:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Take a look at PHP's iconv().

Unicode Character Display Problem

I am trying to show नेपाल at my page, but is shows नेपाल. What is causing the unicode to render like this.
It's caused by something (likely the web browser) interpreting the characters as something else than Unicode. Browsers are quite bad at guessing the proper encoding, so it must be explicitly defined. Perhaps you should have something like this in the head section:
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
It's also possible that the font being used does not cover those characters.
Write at the top of the script <?php header('Content-Type: text/html; charset=UTF-8');?>
If the data comes from a database then this
$mysqli->query('set character set utf8');
should help. Put it inside your db connection :-)
Reading from unicode.org:
If you are unable to read some Unicode
characters in your browser, it may be
because your system is not properly
configured. Here are some basic
instructions for doing that. There are
two basic steps:
Install fonts that cover the
characters you need
Configure your
browser to use them.

PHP: Cyrillic characters not displayed correctly

Recently I switched hosting from one provider to the other and I have problems displaying Cyrillic characters. The characters which are read from the database are displayed correctly, but characters which are hardcoded in the php file aren't (they are displayed as question marks).
The files which contain the php source code are saved in utf-8 form. Help anybody?
Try placing a meta tag indicating the encoding in the head section:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
My PHP module was exactly with same problem all text was like "?????????????????"
And my code was written with Notepad++. I found the solution for the problem. The problem wasn't in the header charset or meta tag because the browser actually knows that it is the UTF-8 charset. I tried all encodings from the browser and the result was the same so I knew the problem is somewhere else, not in the browser character encoding at all.
I just opened the PHP module with Notepad++ and selected all code. After that in the Encoding menu I selected "Convert to UTF-8." After uploading to the server, everything worked like a charm.
put this after connecting database:
mysql_query("SET NAMES UTF8");
for mysqli
mysqli_query($connecDB,"SET NAMES UTF8");
and in the header of the page:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
The problem seems quite strange.
What's the form of these question marks? Is it black diamonds with questions or just plain question marks?
First of all double check if your files are really utf-8 encoded.
Try to add this header to your code (above all output)
header('Content-Type: text/html; charset=utf-8');
But I doubt it would help, as your database text already looks good.
Do you have any SET NAMES queries in your code? What charset it is set?
It had something to do with the encoding of the php files. The files were created using Windows Notepad and saved with utf-8 encoding.
When I used Notepad2 to open the files, the encoding of the files was "utf-8 with signature". When I changed encoding to "utf-8", the text displayed correctly.
The reason for your problem is often accidental re-encoding the script files by a programmer's editor. It isn't a good practice to hardcode strings which rely on encoding in your php files.
Try switching your browser's encoding to find what encoding is used for hardcoded text, it might help you address the issue. Also make sure to send proper http headers for each page:
header('Content-Type: text/html; charset=utf-8');
Optionaly you can insert meta tag in you HTML:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
For me, the line that made the difference is the following:
$mysqli->set_charset("utf8")
Straight from the PHP documentation page.
The server was returning latin, after setting the charset to utf8 now works fine.
I have been fighting with this exact same problem as I'm trying to add a bit of french/german internationalization to a few controls on a widget.
Characters with accents that are stored in my db print fine as UTF-8. However, characters that are hardcoded into arrays in PHP files either display as the black diamond with a question mark inside or the little square box.
I've tried encoding/decoding the hardcoded strings from my php file every which way, but couldn't get the characters to display properly.
Since I have such a finite set of characters and am working strictly with HTML, I just added a bit of functionality to my intl class to substitute the characters for html entities.
I have these properties.
static $accentEntities = array('á' => 'á',
'É' => 'É',
'é' => 'é',
'í' => 'í',
'û' => 'û',
'ü' => 'ü');
static $accents = array();
static $entities = array();
I setup some my replacement arrays in my constructor...
foreach (self::$accentEntities as $char => $entity) {
self::$accents[] = $char;
self::$entities[] = $entity;
}
And then when I need one of my hardcoded strings in my class I just return it like so...
return str_replace(self::$accents,self::$entities,$str);
It's a totally ghetto solution... but for now, it works. I'd definitely like to hear the correct way to display accents/special characters that are hardcoded into a PHP file.

Arabic characters corrupt on landing, fine after refresh - UTF8

I have an php page with mixed Latin and Arabic characters. The charset declaration tag is in the html code
and the file is saved as UTF-8. All the text is static and in the php file (does not come from a DB or an external source)
When I browse to the site some pages randomly get corrupt in IE and FF and display all question marks. After I refresh the page, text is displayed properly though... I have been working with Arabic and Hebrew for a long time and this is the first time I run in to this issue. Can anybody think of a cause?
Chrome is always fine...
Turns out the script reference that was before the meta description was causing the problem. I moved
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
to be the first item after the opening head tag and this is no longer an issue. Thanks for all the comments..
P.S I wasn't the one who code this page, and only working on localizing it, thats why I didn't even think that meta tag being after script would even make a difference...
Try to send appropriate header, something like this:
header("Content-Type: text/xml; charset=utf-8");
Try using UTF8_encode on your content:
http://php.net/manual/en/function.utf8-encode.php
If you have some text you want to store in a DB and display even if the page encoding is latin-1, there is a free tool that can convert Unicode to escaped HTML:
http://www.sprawk.com/tools/escapeUnicode

Categories