-1

I have a problem with a replacing characters, I do not know how to do that. We in Slovakia have characters with interpunctions.

How do I change (eg. á) for html code at input. If I have a string like Áno (translated yes), how do I change á to html code at the string.

I want make input where smiles like :-) will change to image. Or my interpunctioned characters to html code.

  • You mean you want to implement [`htmlentities()`](http://php.net/htmlentities)? – mario Apr 13 '13 at 16:44
  • No I do not want to use it. Because there is it more. Like smiles and other. – Matej Kolec'ko Apr 13 '13 at 16:45
  • I don't think you have a problem replacing characters. You more likely have a problem understanding characters and HTML. Take care to not fall into [the X/Y problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – M8R-1jmw5r Apr 13 '13 at 17:00

1 Answers1

0

You can use strtr for such purposes. I do not know what problems you have to solve with smileys etc, so I'll give you an example for German umlauts (however not to HTML entities, but to standard ASCII characters):

$string = strtr($string, array('ä' => 'ae', 'ö' => 'oe', 'ü' => 'ue'));

Of course you can also use HTML entities instead of ae etc, you just have to look them up.

Edit

Judging from your update (I want make input where smiles like :-) will change to image. Or my interpunctioned characters to html code.) I think you want to use both htmlentities and strtr.

htmlentities will make sure that all non-ASCII characters are displayed correctly. Also have a look at UTF-8. With UTF-8, you will not have to translate your czech characters.

And strtr will replace your smileys by the proper HTML code.

aufziehvogel
  • 7,167
  • 5
  • 34
  • 56
  • 1
    `str_replace` has not been made for translation purposes. The function for translation purposes is called `strtr` in PHP. – M8R-1jmw5r Apr 13 '13 at 17:02
  • If anyone else is interested in the reason: http://stackoverflow.com/questions/8177296/when-to-use-strtr-vs-str-replace Corrected my example. – aufziehvogel Apr 13 '13 at 17:05