Character Replacement in PHP

I find it difficult to change this strange right single-quote character. I use str_replace as follows:

str_replace("’", '\u1234', $string);

It seems I can’t understand what the nature of the quote really is. Even when I copy it directly from PHPMyAdmin, it still doesn't work. Should I somehow escape from him?

Symbol: http://www.lukomon.com/Afbeelding%204.png

  • MySQL Charset: Unicode UTF-8 (utf8)
  • MySQL Collations: utf8_unicode_ci
  • <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

EDIT: It turned out to be a left single quote from Microsoft, which I could replace with this function from a Phill Paffords comment. Not sure which answer I should mention now.

+5
source share
10

. :

  • htmlentities

    $my_text = htmlentities($string, ENT_QUOTES, 'UTF-8');

htmlentities.

  • , .

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

  • utf-8 :

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

:

<!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>

.

, mb_ereg_replace.

:

mb_internal_encoding("UTF-8");
mb_regex_encoding("UTF-8");

$my_text = mb_ereg_replace("’","'", $string);
+9

, :

function replace_rsquote($haystack,$replacewith){
   $pos = strpos($haystack,chr("226"));
   if($pos > -1){
       return substr_replace($haystack,$replacewith,$pos,3);
   } else return $haystack;
}

:

echo replace_rsquote("Nick’s","'"); //Nick's
+3

, ord, ASCII :

echo ord('’'); // 226

, , , :

str_replace('’', chr(226), $string);
+2

:

script , , , , . UTF-8, , script UTF-8, , .

, , , , .

:

UTF-8, :

$string = htmlentities($string, ENT_QUOTES, "UTF-8");

html... iso-8859-1. , , .

, htmlentities(), .

:

, , - , :

  • script;
  • ;
  • ;
  • .
+1

ASCII PHP-, , , . , , PHP script, $string.

, , ​​ UTF-8, .

+1

-
- script utf-8
- http://php.net/mbstring ( )

+1

htmlspecialchars() , , , , ?

0

Right Single Quotation Mark.

, -

$string = preg_replace( "/\\x{2019}/u", 'replacement', $string );

. , , .

0

(preg_replace mb_ereg_replace). .

str_replace(chr(226),'\u2019' , $string);

, :

<?php 
function mb_str_replace($needle, $replacement, $haystack) {
    $needle_len = mb_strlen($needle);
    $replacement_len = mb_strlen($replacement);
    $pos = mb_strpos($haystack, $needle);
    while ($pos !== false)
    {
        $haystack = mb_substr($haystack, 0, $pos) . $replacement
                . mb_substr($haystack, $pos + $needle_len);
        $pos = mb_strpos($haystack, $needle, $pos + $replacement_len);
    }
    return $haystack; 
} 
?>

: http://www.php.net/manual/en/ref.mbstring.php#86120

0

char ascii ord, :

$asciicode = ord('’'); // 146
$stringfixed = str_replace(chr($asciicode), '\'', $string);
0

All Articles