Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Unable to escape some strings with iso-8859-1

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Bugs
View previous topic :: View next topic  
Author Message
valkhorn
Smarty n00b


Joined: 22 Jan 2014
Posts: 2

PostPosted: Thu Feb 06, 2014 4:38 pm    Post subject: Unable to escape some strings with iso-8859-1 Reply with quote

In the latest version of Smarty, if a string is passed to the template and it contains an '®' character - and the charset meta tag header is set to iso-8859-1, the escape modifer returns back a blank string

Use this php code:
Code:

$smarty = new Smarty;
$smarty->assign('test_string', "Smarty®");
$smarty->display('index.tpl');


Make sure file is saved as iso-8859-1 and not utf-8!

Then use this template:

Code:

$smarty->assign('test_string', "Smarty®");


With this template:

Code:

<HTML>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Title</title>
</head>
<BODY>

TEST STRING: {$test_string}
<HR>
TEST STRING ESCAPED: {$test_string|escape}

</BODY>
</html>


The non escaped string prints out fine, but the string with the escaped modifier does not display anything.

I'm not sure if this is a PHP bug or a Smarty bug. I'm using PHP version 5.3.5
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Thu Feb 06, 2014 5:42 pm    Post subject: Reply with quote

The meta tag just gives the browser a hint what charset encoding is on the page. You still need to use the correct encoding server-side.

Try:

{$test_string|escape:"ISO-8859-1"}

Or, set your default char encoding to ISO-8859-1

http://www.smarty.net/docs/en/charset.tpl
Back to top
View user's profile Send private message Visit poster's website
valkhorn
Smarty n00b


Joined: 22 Jan 2014
Posts: 2

PostPosted: Fri Feb 07, 2014 7:08 am    Post subject: Reply with quote

The first solution works (except with a PHP warning message), but I can't add that to hundreds of ecsape modifiers in my code.

I attempted to use

Code:

define('SMARTY_RESOURCE_CHAR_SET', 'ISO-8859-1');


But that doesn't work to fix the problem.

I also get this PHP error message:

Code:

PHP Warning:  htmlspecialchars() [<a href='function.htmlspecialchars'>function.htmlspecialchars</a>]: Invalid multibyte sequence in argument in /var/www/.../Smarty/demo/templates_c/c0360d049dff10f364dfc53ba2cc3958abf6ee6d.file.index.tpl.php on line 39


And it is caused by using the escape modifier in that fashion.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Fri Feb 07, 2014 2:38 pm    Post subject: Reply with quote

That error probably means you are feeding UTF-8 characters into ISO-8859-1, or vise-versa. Ideally you should encode all of your characters in UTF-8, the now defacto standard. However you don't want to mix them. Pick one and stick with it. UTF-8 is going to be much easier to deal with in the long run.
Back to top
View user's profile Send private message Visit poster's website
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Fri Feb 07, 2014 2:42 pm    Post subject: Reply with quote

If you get the "Invalid multibyte sequence in argument" notice
it means that the escape modifier did run on data which did multibyte chars of a different charset.

Maybe you specified 'ISO-8859-1', but the variables still contain 'UTF-8'.

You can use {$foo|mb_detect_encoding} to get the character set of variables dispayed
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Bugs All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP