Wrong output when str_replace with acute ( ´ ) in utf-8 website

529 views Asked by At

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?

2

There are 2 answers

6
Paulius Sapragonas On BEST ANSWER

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>
0
Imran Hafeez On

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);