在 PHP 中替换 ' 字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2741091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:31:40  来源:igfitidea点击:

Replacing ’ character in PHP

phpreplacespecial-characters

提问by richard

I'm having a hard time trying to replace this weird right single quote character. I'm using str_replace like this:

我很难尝试替换这个奇怪的右单引号字符。我正在使用 str_replace 这样的:

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

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?

看起来我无法弄清楚这句话的真正含义。即使我直接从 PHPMyAdmin 复制粘贴它仍然不起作用。我必须以某种方式逃避它吗?

The character:http://www.lukomon.com/Afbeelding%204.png

人物: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" />
  • MySQL 字符集:UTF-8 Unicode (utf8)
  • MySQL 排序规则: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 functionfrom Phill Paffords comment. Not sure which answer I should mark now..

编辑:原来是微软留下的单引号,我可以用Phill Paffords 评论中的这个函数替换。不知道我现在应该标记哪个答案..

回答by Sarfraz

This had happend to me too. Couple of things:

这也发生在我身上。几件事:

  • Use htmlentitiesfunction for your text

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

  • htmlentities为您的文本使用函数

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

More info about the htmlentities function.

有关 htmlentities 函数的更多信息。

  • 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-8encoding type in your page:

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

  • 在页面中使用utf-8编码类型:

    <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 replaceit however, try the mb_ereg_replacefunction.

但是,如果您想替换它,请尝试该mb_ereg_replace功能。

Example:

例子:

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

$my_text = mb_ereg_replace("'","'", $string);

回答by David Kinkead

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

回答by Casey Chu

To find what character it is, run it through the ordfunction, which will give you the ASCII code of the character:

要查找它是什么字符,请通过该ord函数运行它,该函数将为您提供该字符的 ASCII 代码:

echo ord('''); // 226

Now that you know what it is, you can do this:

现在您知道它是什么了,您可以这样做:

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

回答by arena-ru

Gumbo sad right -
- save your script as utf-8 file
- and use http://php.net/mbstring(as Sarfraz pointed in his last example)

Gumbo 悲伤的权利 -
- 将你的脚本保存为 utf-8 文件
- 并使用http://php.net/mbstring(正如 Sarfraz 在他的最后一个例子中指出的那样)

回答by Gumbo

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.

如果您在 PHP 代码中使用非 ASCII 字符,则需要确保使用与正在处理的数据中相同的字符编码。您的尝试可能会失败,因为您在 PHP 脚本中使用的字符编码与$string.

Additionally, if you're using a multibyte character encoding such as UTF-8, you should also use the multibyte aware string functions.

此外,如果您使用多字节字符编码(如 UTF-8),则还应使用多字节识别字符串函数

回答by Pekka

To replace it:

要替换它:

If your script file is encoded in the same encoding as the data you are trying to do the replacement in, it should work the way you posted it. If you're working with UTF-8 data, make sure the script is encoded in UTF-8 and it's not your editor silently transliterating the character when you paste it.

如果您的脚本文件使用与您尝试替换的数据相同的编码进行编码,则它应该按照您发布的方式工作。如果您正在处理 UTF-8 数据,请确保脚本以 UTF-8 编码,并且在您粘贴该字符时,它不是您的编辑器默默地音译该字符。

If it won't work, try escaping it as described below and see what code it returns.

如果它不起作用,请尝试按如下所述对其进行转义并查看它返回的代码。

To escape it:

逃避它:

If your source file is encoded in UTF-8, this should work:

如果你的源文件是用 UTF-8 编码的,这应该可以工作:

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

the default character set of html...is iso-8859-1. Anything differing from that must be explicitly stated.

的默认字符集html...iso-8859-1. 任何与此不同的内容都必须明确说明。

For more complex character conversion issues, always check out the User Contributed Notes to functions like htmlentities(), there are often real gems to be found there.

对于更复杂的字符转换问题,请始终查看用户对函数的贡献注释,例如htmlentities(),通常可以在那里找到真正的宝石。

In General:

一般来说:

Bobince is right in his comment, systemic character set problems should be sorted systematically so they don't bite you in the ass - if only by defining which character set is used on every step of the way:

Bobince 在他的评论中是正确的,应该系统地对系统字符集问题进行排序,这样它们就不会让你感到困扰——如果只是通过定义在每个步骤中使用哪个字符集:

  • How the script file is encoded;
  • How the document is served;
  • How the data is stored in the database;
  • How the database connection is encoded.
  • 脚本文件是如何编码的;
  • 如何送达文件;
  • 数据如何存储在数据库中;
  • 数据库连接的编码方式。

回答by Emanuel A.

You can get the char ascii code with ordthen replace it with your desired character:

您可以使用ord获取 char ascii 代码,然后将其替换为您想要的字符:

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

回答by user97410

Why not run the string through htmlspecialchars() and output it to see what it turns that character into, so you know what to use as your replace expression?

为什么不通过 h​​tmlspecialchars() 运行字符串并输出它以查看它将该字符变成什么,以便您知道使用什么作为替换表达式?

回答by kingjeffrey

Don't use any regex functions ( preg_replace or mb_ereg_replace ). They are way to heavy for this.

不要使用任何正则表达式函数( preg_replace 或 mb_ereg_replace )。他们对此很重。

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

If your needle is a multibyte character, you may have better luck with this bespoke function:

如果您的指针是多字节字符,那么使用此定制功能可能会更好:

<?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; 
} 
?>

credit for this last function: http://www.php.net/manual/en/ref.mbstring.php#86120

最后一个功能的功劳:http: //www.php.net/manual/en/ref.mbstring.php#86120

回答by Peter Bailey

This character you have is the Right Single Quotation Mark.

您拥有的这个字符是Right Single Quotation Mark

To replace it with a pattern you'll want to do something like this

要用模式替换它,你需要做这样的事情

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

But that really only addresses the symptom. The problem is that you don't have consistent use of character encodings throughout your application, as others have noted.

但这实际上只是解决了症状。问题在于,正如其他人所指出的那样,您在整个应用程序中并没有一致地使用字符编码。