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

escape modifier return empty string

 
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 -> General
View previous topic :: View next topic  
Author Message
danbru1211
Smarty Rookie


Joined: 15 Jul 2014
Posts: 9
Location: Austria, Graz

PostPosted: Sat Aug 01, 2015 6:39 pm    Post subject: escape modifier return empty string Reply with quote

I need to show german special characters in strings like öäü. It is user-input.

I read the string from a database using mysqli. The string looks ok from phpmyadmin.

But in smarty, it looks empty.

Here is a example code:
Code:
$smarty->assign('description', "i lüke späcíál chars" /*$Description*/);

and the template:
Code:
<p>WITHOUT: {$description}</p>
<p>WITH: {$description|escape}</p>

this results in:


Please help me fix this.
Back to top
View user's profile Send private message Send e-mail
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sat Aug 01, 2015 7:49 pm    Post subject: Reply with quote

Wrong encoding of user input, and you are not converting it to the template character encoding.
Back to top
View user's profile Send private message
danbru1211
Smarty Rookie


Joined: 15 Jul 2014
Posts: 9
Location: Austria, Graz

PostPosted: Sat Aug 01, 2015 9:24 pm    Post subject: Looks like fixed. Reply with quote

It looks like i fixed it. If I convert all strings using PHP's function utf8_encode, they get displayed correctly.

Code:
$smarty->assign('description', utf8_encode($Description));


Thanks for your idea, AnrDaemon!
Back to top
View user's profile Send private message Send e-mail
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sun Aug 02, 2015 1:09 pm    Post subject: Reply with quote

That's the worst possible idea.
Who said that the strings are in ISO-8859-1 ?
What'd happen, when you get a string in other encoding there?
Use iconv and explicitly specify correct encodings. Don't rely on assumptions.
Back to top
View user's profile Send private message
danbru1211
Smarty Rookie


Joined: 15 Jul 2014
Posts: 9
Location: Austria, Graz

PostPosted: Mon Aug 10, 2015 11:25 am    Post subject: Reply with quote

Okay, i looked into the html source code and they dont get encoded at all! There are special characters in the html source. But without utf_encode and |escape, there are only ? displayed.

I want that an "ä" to be converted to "&auml;"

I am not sure what encoding is used in my database and I think it differs from row to row.

What is iconv? Does it help me to autodetect the encoding?

I read about UTF-8 and extended ascii. In my case the "ä" can be encoding in different ways:

UTF-8: 0xC3 0xA4
ext. ascii: 0x84
unicode: 0x00 0x84

what does smarty's encode or php's htmlentities want? I could to a simple str_replace?
Back to top
View user's profile Send private message Send e-mail
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Mon Aug 10, 2015 3:55 pm    Post subject: Reply with quote

What. A. Shame.
http://www.joelonsoftware.com/articles/Unicode.html
Go read it. Now.
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Wed Aug 12, 2015 12:42 am    Post subject: Reply with quote

If your PHP does support the mbstring functions (which normally shall be the case) Smarty defaults to the UTF-8 charset, otherwise ISO-8859-1.

You can set Smarty's manually
Code:
Smarty::$_CHARSET = 'UTF-8';


Anyway your PHP scripts and template source files should be stored in the same charset. Same applies to your database.
Otherwise you get messed up.
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 -> General 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