Yii CHtml textField special characters in value - php

Code:
CHtml::textField('username', $username, array('class'=>'text'));
If my input field has Å type of special characters the value get disappeared. It solves if I put as htmlentities($username) in value field. But it prints the character as Å
I think it's because the values are get printed by going through CHtml::encode() function.
How should I print the correct value?. Any help please.

Make sure you have in the html head
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
If you work with multibyte strings, this may help:
mb_internal_encoding("UTF-8");
mb_regex_encoding('UTF-8');
Put the above 2 lines at the beginning of the code. I always include these at the first 2 lines of my index.php

Related

Troubles with text encoding on spanish

I have a webpage that has some text on spanish and some words have accents and spanish characters, on the webpage i have a datatable retrieving information, the thing is, when i use the tag:
<meta charset="iso-8859-1">
http://i.imgur.com/sjq53ee.png
it displays correctly the text on the Footer, with accents, but the information from the datatable gets a lot of trash chars.
but then again, if i use the tag:
<meta charset="utf8"> or
<meta charset="latin1_spanish_ci">
http://i.imgur.com/M5eUyHA.png
i get the datatable information correctly, but the text on the footer now gets the trash chars.
Already tried with different encodings and combinatios of them and still not working.
Tables and database are on latin1_spanish_ci collate
I would use the unicode encoding for the letters with diacritical marks.
please take this solution :
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"><!-- Your HTML file can still use UTF-8-->
<title>Untitled Document</title>
</head>
<body>
<?php
/*Just for your server-side code*/
header('Content-Type: text/html; charset=ISO-8859-1');
echo "àè";
?>
</body>
</html>`
I got it from here How to display special characters in PHP
What bytes to you have in the source? (Provide hex)
What did you use when calling mysqli_set_charset()
What is the CHARACTER SET on the column in the table?
What <meta ... charset...> did you have.
Please state all for, plus any output.
To show the hex from PHP, use
$hex = unpack('H*', $text);
echo implode('', $hex);
To show the hex in the table, do
SELECT col, HEX(col) FROM tbl WHERE ...
With the answers we can pin down what you did wrong and how to fix it.
Ok, what i did was use the second option, the one where the information from the database was correct, and for the footer information i used escape characters ÓÑ maybe not the best solution, but after breaking my head all afternoon i think this one is the easier.

Html Special Chars PHP

fm API to get event discription, venue name etc...
Now sometimes I get special chars back like: ' é à , but they show up scrabled.
So how can I display them properly? Also with the descrioption I get html-tags back, but I do want to keep these.
Can someone help me out fot those both cases? The language I'm using is php
Thanks in advance
specify encoding in the header:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
...
encode string when handling the input
$str=utf8_encode($str);
if you are displaying the input back as-is, no encoding is required;
however, if the value is the content of an input or textarea, escape the html characters
<?php echo htmlspecialchars($str); ?>
For Latin characters, use
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
in your section.
you need to be sure about two things, the meta header referring to which enconding you will be using, and the encoding you are using for the text served.
If you are using a utf8 header just be sure to convert the text served to utf8, refer to functions for encoding conversion like : mb_convert_encoding

How to remove unnecessary characters from string?

There is a problem in character when I fetch data from database.
doesn�t
It�s
I have tried to remove it using str_replace function but it's not working.
str_replace('�','',$str);
The encoding is
<meta charset="UTF-8" />
How can i solve these problem?
Try utf8_encode() function.
It will remove that characters
utf8_decode('doesn�t'); // doesnt
this is not good because it removes ' from doesn't
OR
Just update your Meta tag
<meta charset="UTF-8" />
to
<meta charset="ISO-8859-1" />
Output
doesn't
It's
no need to remove those character, Try to use htmlentities() or utfdecode() function to display those properly
This is because character encoding
Just removing the "�"s would give you wrong output. For instance, the string is probably doesn't, removing � from doesn�t gives you doesnt which is wrong.
What you should do is fix your encoding. You are probably saving or retrieving the strings in an encoding different from your code.

UTF-8 and ISO-8859-1: Why does it work for the most of the time and why sometimes not?

I have a osCommerce 2.2 MST which has some custom additions to it. osCommerce itself is in ISO-8859-1. The addition has a table in a MySQL database which is now in utf8_general_ci (the others are all in latin1_swedish_ci). The php-file I'm calling outputs
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
As I mentioned before the data from the database is in UTF-8. But letters like ö,ä,ü are correct displayed. How can this be? There should be no utf8_decode. But the letter č is displayed as ?. I get this directly as result array. If I make the query with phpmyadmin it is correct displayed.
I managed to get all letters correct displayed (only in one section of the script). This is what I made
mysql_query("SET NAMES 'utf8'");
In the php-script I also added
header('content-type: text/html; charset=utf-8');
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
But then other problems occured.
What I want to know why data in UTF-8 is "correctly" displayed when it should be not. And how do I get the letter č correctly displayed?
The system is I find rather complex. Where and how can I look what is wrong here?
I don't know the sequence of encodings/decodings that your data go through, but the reason that letters like ö, ä, and ü are correct, while č is not, is that ö, ä, and ü can be encoded in ISO-8859-1, but č cannot. You will need to use UTF-8 instead of ISO-8859-1 in your HTML to get č to display.

PHP htmlspecialchars and character sets issue

I've got a page that loads content from a database. I have the page set to use utf-8 as such
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
I've set the collation on the db to utf8_unicode_ci, and I access the field from mysql using this:
$desc = htmlspecialchars($row['description'],ENT_QUOTES,'UTF-8');
I've done this to fix smart quotes and em-dashes. However, any field that contains those characters is being returned blank now. If I take away the 'UTF-8' parameter in my htmlspecialchars call I get the full text just with questionmark-in-a-diamond characters where the quotes should be. Is there something I'm missing?
If you're getting an empty string back, it probably means there's an invalid UTF-8 character in the content from the database. You could also set the ENT_IGNORE flag, but I'd recommend doing a binary search with the offending string to try to figure out exactly what the offending character is.
http://www.php.net/manual/en/function.htmlspecialchars.php. See the section under return value.

Categories