Replacing ’ character in PHP
Solution 1
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);
Solution 2
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
Solution 3
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);
Solution 4
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.
Solution 5
Gumbo sad right -
- save your script as utf-8 file
- and use http://php.net/mbstring (as Sarfraz pointed in his last example)
Related videos on Youtube
richard
Updated on July 09, 2022Comments
-
richard almost 2 years
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
- MySQL Charset: UTF-8 Unicode (utf8)
- MySQL Collations: utf8_unicode_ci
<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..
-
zneak about 14 yearsWhy do you want to escape it? How does it interfere with anything?
-
Amit Patil about 14 yearsChances are, if
’
isn't behaving how you want, you'll be breaking all non-ASCII characters. Time to check one of the 2,000,000 SO questions about “why doesn't Unicode make it through my PHP?”. (Usually because of lack of UTF-8,mysql_set_charset
or usinghtmlentities
instead of properhtmlspecialchars
.) -
Phill Pafford about 14 yearsstackoverflow.com/questions/1262038/… might help
-
richard about 14 yearsI tried that but nothing happens. That comma stays as it is :(
-
richard about 14 yearsThanks for your answer. I was indeed missing a doctype or any other HTML in fact. Once I added it and your htmlentities rule the
’
became a�
. Once I switched Firefox to Western ISO charset it changed back to’
. str_replace still doesn't work and unfortunately mb_ereg_replace does not work either. -
richard about 14 yearsThe file is saved UTF-8. HTML meta tag is UTF-8. Database UTF-8. Database connection... Call to undefined function. That's unfortunate, I believe that might be the problem.
-
Sarfraz about 14 yearsTry this:
str_replace("’", "'", $string);
and also try removing thehtmlentites
function and then see. -
richard about 14 yearsWith the html entities function the whole record just disappears. If I remove the function it shows the
�
. My database is UTF-8 but I can change it in PHPMyAdmin? -
Sarfraz about 14 years@richard: yup, you can change the encoding in phpmyadmin, there is an option once you select a database.
-
richard about 14 years@Sarfraz: what should I change it to? I tried setting it to utf8_unicode_ci but that didn't change anything in the output. Thanks.
-
Sarfraz about 14 years@richard: it should be set to
utf8_unicode_ci
to allow foreign languages but not sure which charset that character belongs to. You might want to give a try to any of thelatin
charset too. -
kingjeffrey about 14 yearsThis just replaces the character with a copy of itself.
-
Casey Chu about 14 yearsGood point, but the original poster's code does that too, so I figured I'd do the same.