問題描述
我有一個帶有 textarea 的表單.用戶輸入存儲在數據庫中的文本塊.
有時,用戶會粘貼 Word 中包含智能引號或短劃線的文本.這些字符在數據庫中顯示為:–、–、–、–
我應該在輸入字符串上調用什么函數來將智能引號轉換為常規引號并將短劃線轉換為常規短劃線?
我在 PHP 工作.
更新:感謝您到目前為止的所有精彩回復.Joel 網站上關于編碼的頁面非常有用:http://www.joelonsoftware.com/articles/Unicode.html
關于我的環境的一些說明:
MySQL 數據庫使用 UTF-8 編碼.同樣,顯示內容的 HTML 頁面通過顯式設置元內容類型使用 UTF-8(更新:).
在這些頁面上,智能引號和短劃線顯示為帶問號的菱形.
解決方案:
再次感謝您的回復.解決方案是雙重的:
- 確保數據庫和 HTML文件被明確設置為使用UTF-8 編碼.
- 使用
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: a€", a€?, a€? ,a€
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:
- Make sure the database and HTML files were explicitly set to use UTF-8 encoding.
- Use
htmlspecialchars()
instead ofhtmlentities()
.
This sounds like a Unicode issue. Joel Spolsky has a good jumping off point on the topic: http://www.joelonsoftware.com/articles/Unicode.html
這篇關于如何在字符串中轉換 Word 智能引號和破折號?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!