0

I'm working on a website where I have a form, if I enter text everything works as it should but if I enter special characters like ☺ ☻ ♥ ♦ so it just becomes a lot of ?? in the database. So is there any way to convert them to html code?

Example: convention served to ♥

mathielo
  • 6,725
  • 7
  • 50
  • 63
user2668848
  • 45
  • 2
  • 7

1 Answers1

3

The function you are looking for is htmlentities http://php.net/manual/en/function.htmlentities.php

You'll need to know which encoding the input data uses (obviously). The default is UTF8.

$encoded = htmlentities( $input, ENT_COMPAT | ENT_HTML401, 'UTF-8', false );

Note that I've set the final parameter false (default is true). That is so that users can type, for example, & and it will convert to &. But maybe you want the default behaviour (& -> &)

The encoding which is used when sending POST data can be set in the form tag:

<form method="post" action="myscript.php" accept-charset="utf-8">

But see here for a discussion: Is there any benefit to adding accept-charset="UTF-8" to HTML forms, if the page is already in UTF-8?

Community
  • 1
  • 1
Dave
  • 44,275
  • 12
  • 65
  • 105
  • thanks for the answer :) the ♥ and ♦ converted as I wanted byt ☺and ☻ is stil ?? can it be fixed? – user2668848 Aug 09 '13 at 21:16
  • maybe your encoding isn't UTF-8. How are you sending the text to the server? – Dave Aug 09 '13 at 21:21
  • No I mean where is that coming from? It will be sent to your server (from a form) in a particular encoding. See here: http://www.w3.org/TR/html4/interact/forms.html#h-17.13.3.3 (I assume that it defaults to the encoding of your page) – Dave Aug 09 '13 at 21:25
  • I'm using this line in the html head – user2668848 Aug 09 '13 at 21:25