如何在字符串中转换 Word 智能引号和破折号?

2021-12-26 00:00:00 unicode php smart-quotes

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

有时,用户会粘贴 Word 中包含智能引号或短划线的文本.这些字符在数据库中显示为:–、–、–、–

我应该在输入字符串上调用什么函数来将智能引号转换为常规引号并将短划线转换为常规短划线?

我在 PHP 工作.

更新:感谢您到目前为止的所有精彩回复.Joel 网站上关于编码的页面非常有用:http://www.joelonsoftware.com/articles/Unicode.html

关于我的环境的一些说明:

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

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

解决方案:

再次感谢您的回复.解决方案是双重的:

  1. 确保数据库和 HTML文件被明确设置为使用UTF-8 编码.
  2. 使用 htmlspecialchars() 而不是htmlentities().

解决方案

这听起来像是 Unicode 问题.Joel Spolsky 在这个主题上有一个很好的起点:http://www.joelonsoftware.com/articles/Unicode.html

I have a form with a textarea. Users enter a block of text which is stored in a database.

Occasionally a user will paste text from Word containing smart quotes or emdashes. Those characters appear in the database as: â€", ’, “ ,â€

What function should I call on the input string to convert smart quotes to regular quotes and emdashes to regular dashes?

I am working in PHP.

Update: Thanks for all of the great responses so far. The page on Joel's site about encodings is very informative: http://www.joelonsoftware.com/articles/Unicode.html

Some notes on my environment:

The MySQL database is using UTF-8 encoding. Likewise, the HTML pages that display the content are using UTF-8 (Update:) by explicitly setting the meta content-type.

On those pages the smart quotes and emdashes appear as a diamond with question mark.

Solution:

Thanks again for the responses. The solution was twofold:

  1. Make sure the database and HTML files were explicitly set to use UTF-8 encoding.
  2. Use htmlspecialchars() instead of htmlentities().

解决方案

This sounds like a Unicode issue. Joel Spolsky has a good jumping off point on the topic: http://www.joelonsoftware.com/articles/Unicode.html

相关文章