htmlspecialchars(): Invalid multibyte sequence in argument

I used the Gii CRUD Generator to create a CRUD implementation for a table (MySQL) of mine.

The database and tables are stored in UTF-8.

I’ve followed the directions at http://www.yiiframework.com/wiki/16/how-to-set-up-unicode/

When I load the page at ../index.php?r=chattype I get the following error.

Can someone help?

PHP Error

htmlspecialchars(): Invalid multibyte sequence in argument

X:\_ExcludeFromBackup\furball\yii\framework\web\helpers\CHtml.php(66)

54 */

55 public static $count=0;

56

57 /**

58 * Encodes special characters into HTML entities.

59 * The {@link CApplication::charset application charset} will be used for encoding.

60 * @param string $text data to be encoded

61 * @return string the encoded data

62 * @see http://www.php.net/manual/en/function.htmlspecialchars.php

63 */

64 public static function encode($text)

65 {

66 return htmlspecialchars($text,ENT_QUOTES,Yii::app()->charset);

67 }

Hmm. Can you show the trace and call stack, too?

First idea: Maybe files are saved with BOM?

I’ll post the stack track now. To your 2nd point, which files are you referring to? I haven’t created any files myself (the Gii app did that), and my data is in a database.

Could be that you use a special (unicode) char for a field name?

i have a mysql database and it has a password column of datatype binary and then it is throwing the same problem …i am new to Yii and not good in PHP either please tell me what should i do ??

First thing… check your field names (not values)… that do NOT use unicode characters…

I have the same problem with labels. My application language is Spanish so chars like á é í ó ú or ñ are very common in labels, I’ve looking a lot to solve this with no results, my app is UTF-8 and I have no problems with special characters in the database only in labels, any ideas?


2011/12/06 19:57:14 [error] [php] htmlspecialchars() [<a href='function.htmlspecialchars'>function.htmlspecialchars</a>]: Invalid multibyte sequence in argument (/localPath/YiiRoot/framework/web/helpers/CHtml.php:66)

Stack trace:

#0 /localPath/YiiRoot/framework/zii/widgets/CMenu.php(144): CMenu->normalizeItems()

#1 /localPath/YiiRoot/framework/web/CBaseController.php(140): CMenu->init()

#2 /localPath/YiiRoot/framework/web/CBaseController.php(165): ImageModelController->createWidget()

#3 /webRoot/protected/views/layouts/column2.php(20): ImageModelController->widget()

#4 /localPath/YiiRoot/framework/web/CBaseController.php(119): require()

#5 /localPath/YiiRoot/framework/web/CBaseController.php(88): ImageModelController->renderInternal()

#6 /localPath/YiiRoot/framework/web/CController.php(781): ImageModelController->renderFile()

#7 /webRoot/protected/controllers/ImageModelController.php(188): ImageModelController->render()

#8 /localPath/YiiRoot/framework/web/actions/CInlineAction.php(50): ImageModelController->actionAdmin()

#9 /localPath/YiiRoot/framework/web/CController.php(300): CInlineAction->runWithParams()

#10 /localPath/YiiRoot/framework/web/filters/CFilterChain.php(134): ImageModelController->runAction()

#11 /localPath/YiiRoot/framework/web/filters/CFilter.php(41): CFilterChain->run()

#12 /localPath/YiiRoot/framework/web/CController.php(1144): CAccessControlFilter->filter()

#13 /localPath/YiiRoot/framework/web/filters/CInlineFilter.php(59): ImageModelController->filterAccessControl()

#14 /localPath/YiiRoot/framework/web/filters/CFilterChain.php(131): CInlineFilter->filter()

#15 /localPath/YiiRoot/framework/web/CController.php(283): CFilterChain->run()

#16 /localPath/YiiRoot/framework/web/CController.php(257): ImageModelController->runActionWithFilters()

#17 /localPath/YiiRoot/framework/web/CWebApplication.php(277): ImageModelController->run()

#18 /localPath/YiiRoot/framework/web/CWebApplication.php(136): CWebApplication->runController()

#19 /localPath/YiiRoot/framework/base/CApplication.php(158): CWebApplication->processRequest()

#20 /webRoot/index.php(13): CWebApplication->run()

REQUEST_URI=/webapp/imageModel/admin

Can you boil down your problem to the most simple code that still produces the issue? Like: Start with CHtml::encode($text), then try to render a simple CMenu widget. Also play around with different texts. If you have something reproducible, post it here.

Thanks for your reply, this is my most simple code that produces the error,


<?php

$this->breadcrumbs=array(

	'Fotos'=>array('index'),

	$model->id,

);


$this->menu=array(

	array('label'=>'Listar Imágenes', 'url'=>array('index')),

	array('label'=>'Create Image', 'url'=>array('create')),

	array('label'=>'Update Image', 'url'=>array('update', 'id'=>$model->id)),

	array('label'=>'Delete Image', 'url'=>'#', 'linkOptions'=>array('submit'=>array('delete','id'=>$model->id),'confirm'=>'Are you sure you want to delete this item?')),

	array('label'=>'Manage Image', 'url'=>array('admin')),

);

?>





<h1>Foto #<?php echo $model->id; ?></h1>

	<div class="successMessage">

		<?php echo Yii::app()->user->getFlash('success'); ?>

			 </div>

			 </br>


<?php $this->widget('zii.widgets.CDetailView', array(

	'data'=>$model,

	'attributes'=>array(

		'id',

		'description',

		'file_name',

		'create_time',

		'create_user_id',

		'update_time',

		'update_user_id',

	),

)); ?>

I tried with


$this->menu=array(

	array('label'=>CHtml::encode('Listar Imágenes'), 'url'=>array('index')),

	array('label'=>'Create Image', 'url'=>array('create')),

	array('label'=>'Update Image', 'url'=>array('update', 'id'=>$model->id)),

	array('label'=>'Delete Image', 'url'=>'#', 'linkOptions'=>array('submit'=>array('delete','id'=>$model->id),'confirm'=>'Are you sure you want to delete this item?')),

	array('label'=>'Manage Image', 'url'=>array('admin')),

);

?>

with the same result. I will try the CMenu widget and post the results.

I can not reproduce your issue with the code you supplied. Can you try again with a clean fresh webapp where you did not change the default configuration? I’ve tried to insert your menu items into views/layout/main.php for a test - everything works fine.

Did you really save the view file as UTF-8 (without BOM)?

Does the error disappear if you comment the first item?





$this->menu=array(

        //array('label'=>CHtml::encode('Listar Imágenes'), 'url'=>array('index')),

        array('label'=>'Create Image', 'url'=>array('create')),

        array('label'=>'Update Image', 'url'=>array('update', 'id'=>$model->id)),

        array('label'=>'Delete Image', 'url'=>'#', 'linkOptions'=>array('submit'=>array('delete','id'=>$model->id),'confirm'=>'Are you sure you want to delete this item?')),

        array('label'=>'Manage Image', 'url'=>array('admin')),

);

?>



The error disappears if I comment the first label because it’s the only one with special characters. Concerning the UTF-8 configuration, my db is in UTF-8 (no problem with special characters if the data comes from the db), and my main.php config file is also set in UTF-8.


return array(

        'language'=>'es', // Este es el lenguaje en el que querés que muestre las cosas


        //'sourceLanguage'=>'es_es', //  este es el lenguaje por default de los archivos

	//'homeUrl'=>'/ausevyii/project',

	'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',

	'name'=>'Activos Urbanos Sevilla',

	'theme'=>'citylights',

        'charset'=>'UTF-8',



This is the main layout head section




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US" xml:lang="en">

    <head>


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

        <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />

        <title><?php echo CHtml::encode(Yii::app()->name); ?></title>


        <link rel="stylesheet" href="<?php echo Yii::app()->theme->baseUrl; ?>/css/style.css" type="text/css" media="screen, projection" />

        <link rel="stylesheet" href="<?php echo Yii::app()->theme->baseUrl; ?>/css/form.css" type="text/css" media="screen, projection" />

        <!--[if IE 6]><link rel="stylesheet" href="<?php echo Yii::app()->theme->baseUrl; ?>/css/style.ie6.css" type="text/css" media="screen" /><![endif]-->

        <!--[if IE 7]><link rel="stylesheet" href="<?php echo Yii::app()->theme->baseUrl; ?>/css/style.ie7.css" type="text/css" media="screen" /><![endif]-->


        <script type="text/javascript" src="<?php echo Yii::app()->theme->baseUrl; ?>/css/script.js"></script>

    </head> 

Tried changing the "en-US" and the "en" values to "es-ES" and "es" in the head section, it reproduces the same error.

You didn’t answer to my first question.

Your db and your charset of the output is UTF-8.

But is your view php-file saved on the disk as UTF-8?

I don’t know which editor you use, but maybe it’s configurable at the bottom (phpstorm) or in notepad++ in the encoding menu - see screenshots.

Oh I didn’t know about that, thank you very much. I use Komodo Edit and it was saving all my php files in mac roman, now it is fixed and the views are rendering well. Thank you again, I thought I was going crazy with these special characters.

It’s told in the wiki article Setup unicode in 1. PHP script files

Yes, the thing is I was using a different editor when I started learning Yii and later changed to Komodo and forgot about changing the configuration, so I assumed it was right, it’s like when you can’t find your keys anywhere and then you realize they are in your pocket. :smiley: