Replacing ’ character in PHP

27,704

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)

Share:
27,704

Related videos on Youtube

richard
Author by

richard

Updated on July 09, 2022

Comments

  • richard
    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
      zneak about 14 years
      Why do you want to escape it? How does it interfere with anything?
    • Amit Patil
      Amit Patil about 14 years
      Chances 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 using htmlentities instead of proper htmlspecialchars.)
    • Phill Pafford
      Phill Pafford about 14 years
  • richard
    richard about 14 years
    I tried that but nothing happens. That comma stays as it is :(
  • richard
    richard about 14 years
    Thanks 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
    richard about 14 years
    The 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
    Sarfraz about 14 years
    Try this: str_replace("’", "'", $string); and also try removing the htmlentites function and then see.
  • richard
    richard about 14 years
    With 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
    Sarfraz about 14 years
    @richard: yup, you can change the encoding in phpmyadmin, there is an option once you select a database.
  • richard
    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
    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 the latin charset too.
  • kingjeffrey
    kingjeffrey about 14 years
    This just replaces the character with a copy of itself.
  • Casey Chu
    Casey Chu about 14 years
    Good point, but the original poster's code does that too, so I figured I'd do the same.