View previous topic :: View next topic |
Author |
Message |
danbru1211 Smarty Rookie
Joined: 15 Jul 2014 Posts: 9 Location: Austria, Graz
|
Posted: Sat Aug 01, 2015 6:39 pm Post subject: escape modifier return empty string |
|
|
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Sat Aug 01, 2015 7:49 pm Post subject: |
|
|
Wrong encoding of user input, and you are not converting it to the template character encoding. |
|
Back to top |
|
danbru1211 Smarty Rookie
Joined: 15 Jul 2014 Posts: 9 Location: Austria, Graz
|
Posted: Sat Aug 01, 2015 9:24 pm Post subject: Looks like fixed. |
|
|
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Sun Aug 02, 2015 1:09 pm Post subject: |
|
|
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 |
|
danbru1211 Smarty Rookie
Joined: 15 Jul 2014 Posts: 9 Location: Austria, Graz
|
Posted: Mon Aug 10, 2015 11:25 am Post subject: |
|
|
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 "ä"
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Wed Aug 12, 2015 12:42 am Post subject: |
|
|
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 |
|
|