在添加到$XML_COMMENT文件之前,我想将欧元符号替换为"euros“。
我得到的欧元符号不是utf-8字符,以及simplexml的错误消息
Warning: SimpleXMLElement::addAttribute(): string is not in UTF-8 in ...
Warning: SimpleXMLElement::asXML(): output conversion failed due to conv error, bytes 0x82 0x26 0x61 0x6D in ....欧元符号在MySQL (utf-8)数据库中显示为‘’,‘’
而是正确地出现在网页上的文本区域中。
我尝试使用这些不同的str_replace
$XML_COMMENT=str_replace('€','euros',$XML_COMMENT)
$XML_COMMENT=str_replace('€','euros',$XML_COMMENT)
$XML_COMMENT=str_replace(chr(128),'euros',$XML_COMMENT)
$XML_COMMENT=str_replace(chr(0xE2).chr(0×82).chr(0xAC),'euros',$XML_COMMENT)
$XML_COMMENT=str_replace(chr(0x82).chr(0x26).chr(0x61).chr(0x6D),'euros',$XML_COMMENT)如果没有成功
仅供参考:我到处都在使用utf-8 (MySQL、网页和XML)。
这是我的代码
// ? : $XML_COMMENT= "bla bla bla € bla bla bla";
// ? : $XML_COMMENT= "bla bla bla € bla bla bla";
// expected : $XML_COMMENT= "bla bla bla euros bla bla bla";
$ProductLog_XML = simplexml_load_file($file);
$ProductUpdate = $ProductLog_XML->order->product->addChild('update');
$ProductUpdate->addAttribute('comment',$XML_COMMENT);
$fp=fopen(file, "w");
fwrite($fp, $ProductLog_XML->asXML());
fclose($fp);有没有使用正则表达式/ preg_replace的替代方案?
发布于 2012-06-08 19:51:48
您可以尝试使用htmlentities()转换包括欧元符号在内的所有实体,使它们看起来像€。
我将以以下方式使用它:htmlentities($str, ENT_QUOTES|"ENT_HTML401", "UTF-8", true)
您可以选择使用:htmlentities($XML_COMMENT, ENT_QUOTES | ENT_IGNORE, "UTF-8", true)。有关这些标志更改的完整说明,请访问下面的链接。按照OP @baptme的要求(参见注释)。
来源:php.net reference
发布于 2013-01-13 23:55:06
我也遇到了同样的问题,下面的代码适用于我,其中原始HTML页面是UTF-8格式,并使用&euro,它在使用PHP卷曲后显示为“-,”
$nodeValue = str_replace(chr(0xE2).chr(0x82).chr(0xAC), "", $nodeValue)https://stackoverflow.com/questions/10948176
复制相似问题