问题描述
在添加到 xml 文件之前,我想用欧元"替换 $XML_COMMENT 中的 € 符号.
I would like to replace the € sign in $XML_COMMENT with "euros" before adding to a xml file.
€ 符号不是我从 simplexml 得到的 utf-8 字符和错误消息
The € sign not being a utf-8 character I get and error message from 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) 数据库中显示为……"
The euro sign appears in the MySQL (utf-8) database as '€'
但在网页的 textarea 中正确显示.
But appears correctly in the textarea on the webpage.
我尝试使用这些不同的 str_replace
I tried to use these different 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)
FYI: I'm using utf-8 everywhere (MySQL, Web Page and 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 的替代方法?
Is there any alternative using regex / preg_replace ?
推荐答案
您可以尝试 htmlentities()
转换包括欧元符号在内的所有实体,使它们看起来像 €.
You can try
htmlentities()
to convert all entities including the euro sign, so they appear like €
.
我会以下列方式使用它:
htmlentities($str, ENT_QUOTES|"ENT_HTML401", "UTF-8", true)
I would use it in the following manner:
htmlentities($str, ENT_QUOTES|"ENT_HTML401", "UTF-8", true)
您可以选择使用:
htmlentities($XML_COMMENT, ENT_QUOTES | ENT_IGNORE, "UTF-8", true)
.有关标志更改内容的完整说明,请访问以下链接.根据 OP @baptme 的要求(见评论).
You may choose to use:
htmlentities($XML_COMMENT, ENT_QUOTES | ENT_IGNORE, "UTF-8", true)
. For a full explanation of what the flags change, visit the link below.As requested by OP @baptme (see comments).
来源:php.net 参考
这篇关于php preg_replace € 符号为“欧元";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!