How to display special characters [SOLVED]

Hi All,

I have a problem with German letters: ü, ä etc…

The database contains this letters and they are shown properly in all views.

If I insert html elements in the views and write special characters they are not shown properly…

It is fixed by using ‘charset’=>‘iso-8859-1’ in the config file, but this ruins the ones from database…

How can I make them work in both cases?

Thank you in advance.

You should always align Yii and Db to use UTF-8.

Check if your html contains

<meta charset="utf-8">

By default it is:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

This shows correctly the db values but not new html text that I write.

So I do this:

config file:


and layout:

<meta http-equiv="Content-Type" content="text/html; charset='<?php echo Yii::app()->charset ?>'" />

This shows correctly html text and ruins db characters…

What am I doing wrong?

Are you sure that html page of your form has meta charset to UTF-8 ?

But I am not using any special form…just the ones generated by gii and adding some extra text.

It is using the main layout which has UTF-8. Or I have to define it again?

Try this:

The following function clears any strange characters:

	public function sanitize_string($string)



	    $string = trim($string);


	    $string = str_replace(

	        array('á', 'à', 'ä', 'â', 'ª', 'Á', 'À', 'Â', 'Ä'),

	        array('a', 'a', 'a', 'a', 'a', 'A', 'A', 'A', 'A'),




	    $string = str_replace(

	        array('é', 'è', 'ë', 'ê', 'É', 'È', 'Ê', 'Ë'),

	        array('e', 'e', 'e', 'e', 'E', 'E', 'E', 'E'),




	    $string = str_replace(

	        array('í', 'ì', 'ï', 'î', 'Í', 'Ì', 'Ï', 'Î'),

	        array('i', 'i', 'i', 'i', 'I', 'I', 'I', 'I'),




	    $string = str_replace(

	        array('ó', 'ò', 'ö', 'ô', 'Ó', 'Ò', 'Ö', 'Ô'),

	        array('o', 'o', 'o', 'o', 'O', 'O', 'O', 'O'),




	    $string = str_replace(

	        array('ú', 'ù', 'ü', 'û', 'Ú', 'Ù', 'Û', 'Ü'),

	        array('u', 'u', 'u', 'u', 'U', 'U', 'U', 'U'),




	    $string = str_replace(

	        array('ñ', 'Ñ', 'ç', 'Ç'),

	        array('n', 'N', 'c', 'C',),




	    $string = str_replace(

	        array("\\", "¨", "º", "-", "~",

	             "#", "@", "|", "!", "\"",

	             "·", "$", "%", "&", "/",

	             "(", ")", "?", "'", "¡",

	             "¿", "[", "^", "`", "]",

	             "+", "}", "{", "¨", "´",

	             ">", "< ", ";", ",", ":",






	    return $string;


Hi moginn,

What do you mean by clearing?

And where shall I use this function?

You can use this function to replace “strange characters” by “non-strange characters”. For example, it will change “á” to “a”. However, I’m not sure if this is what you really want.

Actually I don’t want to replace them. I want to display them as they are…

Found the solution to this problem:

in Eclipse: Window/Preferences/Workspace/Text file encoding - Other: UTF-8

So charset need to be specified for the project also…

Default was Cp1252