PHP中是否有用于将Accented字符(例如法语中存在的字符)转换为HTML编码字符的函数?
答案 0 :(得分:4)
您唯一需要做的就是确保它们是有效的UTF8并设置适当的content-tyoe标头(text/html; charset=utf-8
)。
现在没有理由为这些角色使用HTML实体。
答案 1 :(得分:2)
您编写HTML编码字符,因此我假设您要将字符转换为HTML EntititesRef。这些是在HTML 2(ISO Latin 1 Character Entity Set)以及HTML 3.2(Character Entities for ISO Latin-1)中引入的,最后是HTML 4中的一些(Character entity references in HTML 4)。
您尚未分享您正在使用的哪些HTML版本,因此我建议您查看List of XML and HTML character entity referencesWikidpedia以找到您要替换的版本。
与这些相关的PHP函数称为:htmlentities
Docs。
感谢the content-type header in HTTP和it's equivaltent in HTML,没有必要将这些字符编码为实体,因为您可以告诉浏览器您正在使用哪个字符集。只有在字符不是用于输出/响应的编码的一部分时,才需要使用实体。
对于这些情况,可以使用htmlentities
Docs或strtr
Docs函数。由于您没有指定数据和目标涉及哪种编码,因此不能给出具体的代码示例,只能是一般代码:
echo htmlentities ($string, ENT_HTML401, $encoding = 'YOUR STRING ENCODING');
ENT_HTML401
translation tableDocs的转换次数将超过您可能要求的字符数。
您也可以创建自己的转换表,并使用strtr
Docs函数进行转换,而不是使用内置转换表。如果htmlentities
不支持您的数据编码,也需要这样做,例如Adobe符号字体(请参阅:How to convert Symbol font to standard utf8 HTML entity)。或者因为您只想运行自己的转换(请参阅How to substitute non SGML characters in String using PHP?)。
/*
* mappings of Windows-1252 (cp1252) 128 (0x80) - 159 (0x9F) characters:
* @link http://en.wikipedia.org/wiki/Windows-1252
* @link http://www.w3.org/TR/html4/sgml/entities.html
*/
$cp1252HTML401Entities = array(
"\x80" => '€', # 128 -> euro sign, U+20AC NEW
"\x82" => '‚', # 130 -> single low-9 quotation mark, U+201A NEW
"\x83" => 'ƒ', # 131 -> latin small f with hook = function = florin, U+0192 ISOtech
"\x84" => '„', # 132 -> double low-9 quotation mark, U+201E NEW
"\x85" => '…', # 133 -> horizontal ellipsis = three dot leader, U+2026 ISOpub
"\x86" => '†', # 134 -> dagger, U+2020 ISOpub
"\x87" => '‡', # 135 -> double dagger, U+2021 ISOpub
"\x88" => 'ˆ', # 136 -> modifier letter circumflex accent, U+02C6 ISOpub
"\x89" => '‰', # 137 -> per mille sign, U+2030 ISOtech
"\x8A" => 'Š', # 138 -> latin capital letter S with caron, U+0160 ISOlat2
"\x8B" => '‹', # 139 -> single left-pointing angle quotation mark, U+2039 ISO proposed
"\x8C" => 'Œ', # 140 -> latin capital ligature OE, U+0152 ISOlat2
"\x8E" => 'Ž', # 142 -> U+017D
"\x91" => '‘', # 145 -> left single quotation mark, U+2018 ISOnum
"\x92" => '’', # 146 -> right single quotation mark, U+2019 ISOnum
"\x93" => '“', # 147 -> left double quotation mark, U+201C ISOnum
"\x94" => '”', # 148 -> right double quotation mark, U+201D ISOnum
"\x95" => '•', # 149 -> bullet = black small circle, U+2022 ISOpub
"\x96" => '–', # 150 -> en dash, U+2013 ISOpub
"\x97" => '—', # 151 -> em dash, U+2014 ISOpub
"\x98" => '˜', # 152 -> small tilde, U+02DC ISOdia
"\x99" => '™', # 153 -> trade mark sign, U+2122 ISOnum
"\x9A" => 'š', # 154 -> latin small letter s with caron, U+0161 ISOlat2
"\x9B" => '›', # 155 -> single right-pointing angle quotation mark, U+203A ISO proposed
"\x9C" => 'œ', # 156 -> latin small ligature oe, U+0153 ISOlat2
"\x9E" => 'ž', # 158 -> U+017E
"\x9F" => 'Ÿ', # 159 -> latin capital letter Y with diaeresis, U+0178 ISOlat2
);
$outputWithEntities = strtr($output, $cp1252HTML401Entities);
答案 2 :(得分:1)
来源:http://coding.smashingmagazine.com/2011/11/02/introduction-to-url-rewriting/
尝试此功能:
function GenerateUrl ($s) {
//Convert accented characters, and remove parentheses and apostrophes
$from = explode (',', "ç,æ,œ,á,é,í,ó,ú,à,è,ì,ò,ù,ä,ë,ï,ö,ü,ÿ,â,ê,î,ô,û,å,e,i,ø,u,(,),[,],'");
$to = explode (',', 'c,ae,oe,a,e,i,o,u,a,e,i,o,u,a,e,i,o,u,y,a,e,i,o,u,a,e,i,o,u,,,,,,');
//Do the replacements, and convert all other non-alphanumeric characters to spaces
$s = preg_replace ('~[^\w\d]+~', '-', str_replace ($from, $to, trim ($s)));
//Remove a - at the beginning or end and make lowercase
return strtolower (preg_replace ('/^-/', '', preg_replace ('/-$/', '', $s)));
}