Invalid output when str_replace with sharp (') on utf-8 website

I am trying to replace an apostrophe (') with a sharp (') from a string after it has been entered into the form and submitted it.

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

: "Jan Motel"> should become "Jan's Motel"

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

I utf-8, result line - "Janâ's Motel"

I do not understand why it becomes "Â" instead of ""

Here is my sample 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 anybody help?

+5
source share
2 answers

try 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> 
+1
source

you can use the 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); 
+1
source

All Articles