2012-06-08 83 views
0

我想在添加到xml文件之前用“欧元”替换$ XML_COMMENT中的欧元符号。php preg_replace欧元符号

的€符号不是一个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)数据库为 'â,¬'

但是在网页的textarea上正确显示。

我试图用这些不同的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) 

没有成功

FYI:我使用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?

+3

'我在使用utf-8无处不在.'不,你不是。很可能您的编辑器配置错误,并使用其他编码。 – phihag

+0

我用utf-8检查过它yes dosent使用utf-8编码,当我删除指定我的字符集的元标记时它正常工作!!!!! ----无法理解:(这是什么类型的字符? –

+0

@phphag是什么让你这么说?我认为编辑器与我的问题没有任何关系,因为$ XML_COMMENT来自utf-8由utf-8网页提供的MySQL表 – baptme

回答

3

您可以尝试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

+0

you're a +1 – baptme

+0

很高兴能提供帮助:D –

+0

你可以用htmlentities更新你的答案($ XML_COMMENT,ENT_QUOTES | ENT_IGNORE,“UTF-8”,true) – baptme

4

我有同样的问题,我下面的作品,其中原来的HTML页面是UTF-8和使用&欧元,其卷曲荷兰国际集团与PHP吐出为“â,¬后“

$nodeValue = str_replace(chr(0xE2).chr(0x82).chr(0xAC), "", $nodeValue)