I'm having a hard time trying to replace this weird right single quote character. I'm using str_replace like this:
str_replace("’", '\u1234', $string);
It looks like I cannot figure out what character the quote really is. Even when I copy paste it directly from PHPMyAdmin it still doesn't work. Do I have to escape it somehow?
The character: http://www.lukomon.com/Afbeelding%204.png
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
EDIT: It turned out to be a Microsoft left single quote which I could replace with this function from Phill Paffords comment. Not sure which answer I should mark now..
This had happend to me too. Couple of things:
Use htmlentities
function for your text
$my_text = htmlentities($string, ENT_QUOTES, 'UTF-8');
More info about the htmlentities function.
Use proper document type, this did the trick for me.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Use utf-8 encoding type in your page:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Here is the final prototype for your page:
<!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>Untitled Document</title>
</head>
<body>
<?php
// your code related to database
$my_text = htmlentities($string, ENT_QUOTES, 'UTF-8');
?>
</body>
</html>
.
If you want to replace it however, try the mb_ereg_replace
function.
Example:
mb_internal_encoding("UTF-8");
mb_regex_encoding("UTF-8");
$my_text = mb_ereg_replace("’","'", $string);
I had the same issue and found this to work:
function replace_rsquote($haystack,$replacewith){
$pos = strpos($haystack,chr("226"));
if($pos > -1){
return substr_replace($haystack,$replacewith,$pos,3);
} else return $haystack;
}
Example:
echo replace_rsquote("Nick’s","'"); //Nick's
To find what character it is, run it through the ord
function, which will give you the ASCII code of the character:
echo ord('’'); // 226
Now that you know what it is, you can do this:
str_replace('’', chr(226), $string);
If you are using non-ASCII characters in your PHP code, you need to make sure that you’re using the same character encoding as in the data you are processing. Your attempt probably fails because you are using a different character encoding in your PHP script than in $string
.
Additionally, if you’re using a multibyte character encoding such as UTF-8, you should also use the multibyte aware string functions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With