Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将Word智能引号和em破折号转换为字符串?_Php_Unicode_Smart Quotes - Fatal编程技术网

Php 如何将Word智能引号和em破折号转换为字符串?

Php 如何将Word智能引号和em破折号转换为字符串?,php,unicode,smart-quotes,Php,Unicode,Smart Quotes,我有一个带文本区的表单。用户输入存储在数据库中的文本块 有时,用户会从包含智能引号或虚线的Word中粘贴文本。这些字符在数据库中显示为:“-”,或“-”™, 一欧元,一欧元 我应该调用什么函数来将智能引号转换为常规引号,将emdash转换为常规破折号 我在PHP工作 更新:感谢到目前为止所有的精彩回复。Joel网站上关于编码的页面内容非常丰富: 关于我的环境的一些注意事项: MySQL数据库使用UTF-8编码。同样,显示内容的HTML页面也通过显式设置元内容类型使用UTF-8(更新:) 在这些页

我有一个带文本区的表单。用户输入存储在数据库中的文本块

有时,用户会从包含智能引号或虚线的Word中粘贴文本。这些字符在数据库中显示为:“-”,或“-”™, 一欧元,一欧元

我应该调用什么函数来将智能引号转换为常规引号,将emdash转换为常规破折号

我在PHP工作

更新:感谢到目前为止所有的精彩回复。Joel网站上关于编码的页面内容非常丰富:

关于我的环境的一些注意事项:

MySQL数据库使用UTF-8编码。同样,显示内容的HTML页面也通过显式设置元内容类型使用UTF-8(更新:)

在这些页面上,智能引号和虚线显示为带问号的菱形

解决方案:

再次感谢您的回复。解决方案有两方面:

  • 确保数据库和HTML 文件被显式设置为使用 UTF-8编码
  • 使用
    htmlspecialchars()
    而不是
    htmlentities()

  • 这听起来像是一个Unicode问题。Joel Spolsky在这个主题上有一个很好的出发点:

    我们经常使用标准的字符串替换函数。尽管ASCII/Unicode在该上下文中的性质非常模糊,但它可以工作。只要确保您的php文件以正确的编码格式保存,等等。

    听起来像真正的问题是,您的数据库没有使用与页面相同的字符编码(可能应该是UTF-8)。在这种情况下,如果任何用户提交非ASCII字符,您可能会在数据库中看到奇怪的字符。查找和修复其中的一些字符(卷曲引号和em破折号)并不能解决真正的问题


    以下是关于MySQL数据库的一些信息。

    根据我的经验,只接受智能引号并确保您在所有地方都使用相同的编码更容易。首先,请将此添加到表单标记中:
    accept charset=“utf-8”
    您可以尝试从ISO-8859-1到utf-8

    $str = mb_convert_encoding($str, 'UTF-8', 'ISO-8859-1');
    

    这假设您需要UTF-8,convert可以找到合理的替换…如果不需要,请自行替换或preg_替换它们。

    不幸的是,这是一个非常常见的问题,PHP对字符集的处理非常糟糕,这并没有帮助

    我们所做的是强迫文本通过

    //IGNORE
    标志意味着任何无法翻译的内容都将被丢弃

    如果附加字符串//IGNORE,则无法在目标字符集中表示的字符将被自动丢弃

    mysql数据库使用UTF-8 编码。同样,html页面 显示正在使用的内容的 UTF-8

    HTML的内容可以是UTF-8格式,是的,但是您是否也明确地将HTML页面(通过PHP生成)的内容类型(编码)设置为UTF-8格式?尝试返回
    “text/HTML;charset=UTF-8”
    内容类型
    标题或将
    标记添加到HTMLs:

    <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
    
    
    
    这样,提交给PHP的数据的内容类型也将相同

    我有一个类似的问题,添加
    标记对我来说很有效。

    这可能不是最好的解决方案,但我会尝试测试PHP看到的内容。假设它看到了“–”(还有一些其他的可能性,比如简单的“”,或者可能是““;”)。然后在将答案填入数据库之前,先进行str_替换,去掉所有这些内容,并用普通引号替换它们


    更好的解决方案可能包括使端到端数据通过所有UTF-8,因为人们正试图在其他答案中提供帮助。

    您必须确保您的数据库连接配置为从客户端接受并向客户端提供UTF-8(否则它将转换为“默认值”,通常为1)

    实际上,这意味着运行一个名为“utf8”的查询集


    此外,智能引号是windows-1252字符集的一部分,不是iso-8859-1(拉丁语-1)。与您的问题不太相关,仅供参考。欧元符号也在其中。

    问题在于mysql字符集,我用这行代码修复了我的问题

    mysql_set_charset('utf8',$link); 
    

    您必须手动将单个列的排序规则更改为UTF8;更改数据库整体不会改变这些字符。

    如果您希望在保留这些字符外观的同时为web转义这些字符,那么您的字符串将如下所示:“太好了!”而不是“很无聊”

    您可以使用自己的自定义htmlEncode函数来代替PHP的htmlentities()

    $trans_tbl = false;
    
    function htmlEncode($text) {
    
      global $trans_tbl;
    
      // create translation table once
      if(!$trans_tbl) {
        // start with the default set of conversions and add more.
    
        $trans_tbl = get_html_translation_table(HTML_ENTITIES); 
    
        $trans_tbl[chr(130)] = '&sbquo;';    // Single Low-9 Quotation Mark
        $trans_tbl[chr(131)] = '&fnof;';    // Latin Small Letter F With Hook
        $trans_tbl[chr(132)] = '&bdquo;';    // Double Low-9 Quotation Mark
        $trans_tbl[chr(133)] = '&hellip;';    // Horizontal Ellipsis
        $trans_tbl[chr(134)] = '&dagger;';    // Dagger
        $trans_tbl[chr(135)] = '&Dagger;';    // Double Dagger
        $trans_tbl[chr(136)] = '&circ;';    // Modifier Letter Circumflex Accent
        $trans_tbl[chr(137)] = '&permil;';    // Per Mille Sign
        $trans_tbl[chr(138)] = '&Scaron;';    // Latin Capital Letter S With Caron
        $trans_tbl[chr(139)] = '&lsaquo;';    // Single Left-Pointing Angle Quotation Mark
        $trans_tbl[chr(140)] = '&OElig;';    // Latin Capital Ligature OE
    
        // smart single/ double quotes (from MS)
        $trans_tbl[chr(145)] = '&lsquo;'; 
        $trans_tbl[chr(146)] = '&rsquo;'; 
        $trans_tbl[chr(147)] = '&ldquo;'; 
        $trans_tbl[chr(148)] = '&rdquo;'; 
    
        $trans_tbl[chr(149)] = '&bull;';    // Bullet
        $trans_tbl[chr(150)] = '&ndash;';    // En Dash
        $trans_tbl[chr(151)] = '&mdash;';    // Em Dash
        $trans_tbl[chr(152)] = '&tilde;';    // Small Tilde
        $trans_tbl[chr(153)] = '&trade;';    // Trade Mark Sign
        $trans_tbl[chr(154)] = '&scaron;';    // Latin Small Letter S With Caron
        $trans_tbl[chr(155)] = '&rsaquo;';    // Single Right-Pointing Angle Quotation Mark
        $trans_tbl[chr(156)] = '&oelig;';    // Latin Small Ligature OE
        $trans_tbl[chr(159)] = '&Yuml;';    // Latin Capital Letter Y With Diaeresis
    
        ksort($trans_tbl);
      }
    
      // escape HTML      
      return strtr($text, $trans_tbl); 
    }
    

    事实上,这个问题不是在PHP中发生的,而是在JavaScript中发生的,这是由于Word的复制/粘贴造成的,所以在将文本传递到PHP之前,您需要用JavaScript解决问题,请参见此答案。

    这似乎是一个完美的“快速修复”但遗憾的是,它通过添加更多无效字符使我的测试用例变得更糟。只有当您知道输入字符集是拉丁1时,从拉丁1转换为UTF-8才有意义。但是,如果输入已经是UTF-8,您只能通过“翻译”来进一步篡改它第二次将它从拉丁语1改为UTF-8。这对我来说也很有效,直接添加到运行
    INSERT
    /
    UPDATE
    的查询上方。其他所有内容都正确地设置为UTF8、表字符集、列排序规则和HTML输出页。很高兴这终于奏效了!请添加答案的相关部分。@Robert he说d“我有一个带有文本区域的表单。用户输入一个存储在数据库中的文本块。”因此我认为这意味着他使用JavaScript将数据从前端(即浏览器)传递到服务器端(即PHP)。他还说“从Word粘贴文本”,“我应该在中调用什么函数?”
    $trans_tbl = false;
    
    function htmlEncode($text) {
    
      global $trans_tbl;
    
      // create translation table once
      if(!$trans_tbl) {
        // start with the default set of conversions and add more.
    
        $trans_tbl = get_html_translation_table(HTML_ENTITIES); 
    
        $trans_tbl[chr(130)] = '&sbquo;';    // Single Low-9 Quotation Mark
        $trans_tbl[chr(131)] = '&fnof;';    // Latin Small Letter F With Hook
        $trans_tbl[chr(132)] = '&bdquo;';    // Double Low-9 Quotation Mark
        $trans_tbl[chr(133)] = '&hellip;';    // Horizontal Ellipsis
        $trans_tbl[chr(134)] = '&dagger;';    // Dagger
        $trans_tbl[chr(135)] = '&Dagger;';    // Double Dagger
        $trans_tbl[chr(136)] = '&circ;';    // Modifier Letter Circumflex Accent
        $trans_tbl[chr(137)] = '&permil;';    // Per Mille Sign
        $trans_tbl[chr(138)] = '&Scaron;';    // Latin Capital Letter S With Caron
        $trans_tbl[chr(139)] = '&lsaquo;';    // Single Left-Pointing Angle Quotation Mark
        $trans_tbl[chr(140)] = '&OElig;';    // Latin Capital Ligature OE
    
        // smart single/ double quotes (from MS)
        $trans_tbl[chr(145)] = '&lsquo;'; 
        $trans_tbl[chr(146)] = '&rsquo;'; 
        $trans_tbl[chr(147)] = '&ldquo;'; 
        $trans_tbl[chr(148)] = '&rdquo;'; 
    
        $trans_tbl[chr(149)] = '&bull;';    // Bullet
        $trans_tbl[chr(150)] = '&ndash;';    // En Dash
        $trans_tbl[chr(151)] = '&mdash;';    // Em Dash
        $trans_tbl[chr(152)] = '&tilde;';    // Small Tilde
        $trans_tbl[chr(153)] = '&trade;';    // Trade Mark Sign
        $trans_tbl[chr(154)] = '&scaron;';    // Latin Small Letter S With Caron
        $trans_tbl[chr(155)] = '&rsaquo;';    // Single Right-Pointing Angle Quotation Mark
        $trans_tbl[chr(156)] = '&oelig;';    // Latin Small Ligature OE
        $trans_tbl[chr(159)] = '&Yuml;';    // Latin Capital Letter Y With Diaeresis
    
        ksort($trans_tbl);
      }
    
      // escape HTML      
      return strtr($text, $trans_tbl); 
    }