5

I'm trying to replace an apostroph ( ' ) with acute ( ´ ) from a string after entered into a form and submitted it.

<?= str_replace("'","´",$_POST['string']) ?>

string for example is: "Jan's Motel" > should become "Jan´s Motel"

This works good when using charset iso-8859-1, but I need my website to be in utf-8.

I utf-8 the result string is "Jan´s Motel"

I don't understand why it becomes " ´ " instead of " ´ "

Here ist my example code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>notitle</title>
  </head>

  <body>
    <form action="?" method="post">
      <input type="text" name="string" value="<?= str_replace("'","´",$_POST['name']) ?>" />
    </form>
  </body>
</html>

Can anyone please help?

Aracthor
  • 5,757
  • 6
  • 31
  • 59
cypher75
  • 2,099
  • 4
  • 18
  • 18
  • 2
    Make sure your source file is encoded using UTF-8 also. – Phylogenesis Jun 09 '15 at 09:10
  • 1
    Also, make sure the web server is not overriding your `` tag with a `Content-Type` HTTP header (it's actually *much* better to set your character set using a header than a tag, if you have the ability). – Phylogenesis Jun 09 '15 at 09:16
  • 1
    Most browser now default to default utf8 charset. But You can force it by setting a header in your http response `header("Content-Type: text/html; charset=utf-8");`. Make sure every text is utf8 encoded (source code, database content ...) – Mat Jun 09 '15 at 09:18
  • Phylogenesis: yes it is, but i cannot change that server behaviour. Mat: works in my example code, but not in the whole context of my website (will force other characters to be corrupted) - I know, bad programmed ;-) – cypher75 Jun 09 '15 at 09:29
  • Your problem is that you're not handling your encodings as UTF-8 all the way through. If you require UTF-8, then you need to fix every step to properly produce and handle UTF-8. The solution with `utf8_decode` below is not a solution since it doesn't produce UTF-8 as output. – deceze Jun 09 '15 at 09:30
  • BTW, why an *acute accent* where an *apostrophe* must be used? At the very least use some curly apostrophe if you want a fancy character, but not an accent. – deceze Jun 09 '15 at 09:33
  • @deceze: Why is my question a duplicate to "UTF-8 all the way through"?? On that thread someone wants to "set up a new server"... Maybe the preferred answer includes something which also solves my problem, but the question is completely different. So, this question has NOT been asked before. I do not agree with that duplicate mark, sorry. – cypher75 Jun 09 '15 at 10:07
  • Again, your immediate problem is that your output is not handled correctly as UTF-8. To fix that, you need to handle *UTF-8 all the way through.* Though, since apparently you're happy with *not* actually using UTF-8 this question doesn't really make sense anymore. – deceze Jun 09 '15 at 10:22

2 Answers2

1

try to utf8_decode('')

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>notitle</title>
  </head>

  <body>
    <form action="?" method="post">
      <input type="text" name="string" value="<?= str_replace("'",utf8_decode('`'),$_POST['name']) ?>" />
    </form>
  </body>
</html>
  • Thanks a lot - this works in my case – cypher75 Jun 09 '15 at 09:24
  • Then the result is not UTF-8 though! – deceze Jun 09 '15 at 09:29
  • @deceze: correct, the server DefaultCharset is iso-8859-1 and I cannot change it myself. Most parts of that website also needs it to be kept (otherwise many changes have to be done) – cypher75 Jun 09 '15 at 09:32
  • *"but I need my website to be in utf-8"* – Hmm, impedance mismatch... :P – deceze Jun 09 '15 at 09:34
  • @deceze: sorry you are right. To clarify: I need this single page to be in utf-8 most of the website runs with iso... – cypher75 Jun 09 '15 at 09:51
  • But you're converting all UTF-8 output to ISO-8859-1. That's fine when your example character (acute accent) has an equivalent in ISO-8859-1, but as soon as you're trying to display anything outside it, you'll get a page filled with question marks. The correct way to fix this is to use a proper HTTP header as shown in Imran Hafeez's answer below. – Phylogenesis Jun 09 '15 at 10:58
1

you can use header("Content-type: text/html; charset=utf-8"); or HTML tag .

header("Content-type: text/html; charset=utf-8");
$string = "My name is Jan's";
echo str_replace("'", "´", $string);
Imran Hafeez
  • 62
  • 10