使用PHP

时间:2016-05-11 11:15:30

标签: php xml

我正在尝试生成一个XML文件,其中包含一些包含特殊字符的值,例如μmol/ l,x10³cell/μl等等。还需要输入上标的功能。

我使用来自php.net的ordutf8函数将文本μmol/ l编码为类似的东西

  

&安培;#956&安培;#109&安培;#111&安培;#108&安培;#47&安培;#108

function ords_to_unistr($ords, $encoding = 'UTF-8'){
    // Turns an array of ordinal values into a string of unicode characters
    $str = '';
    for($i = 0; $i < sizeof($ords); $i++){
        // Pack this number into a 4-byte string
        // (Or multiple one-byte strings, depending on context.)               
        $v = $ords[$i];
        $str .= pack("N",$v);
    }
    $str = mb_convert_encoding($str,$encoding,"UCS-4BE");
    return($str);           
}

function unistr_to_ords($str, $encoding = 'UTF-8'){       
    // Turns a string of unicode characters into an array of ordinal values,
    // Even if some of those characters are multibyte.
    $str = mb_convert_encoding($str,"UCS-4BE",$encoding);
    $ords = array();

    // Visit each unicode character
    for($i = 0; $i < mb_strlen($str,"UCS-4BE"); $i++){       
        // Now we have 4 bytes. Find their total
        // numeric value.
        $s2 = mb_substr($str,$i,1,"UCS-4BE");                   
        $val = unpack("N",$s2);           
        $ords[] = $val[1];               
    }       
    return($ords);
}

我已成功将此代码转换回“richtext”,使用PHPExcel生成Excel文档和PDF,但我现在需要将其放入XML中。

如果我按原样使用&amp;#字符,我会收到一条错误消息

  

SimpleXMLElement :: addChild():无效的十进制字符值

以下是我在数据库中需要使用“XML”友好

的更多值
  

&安培;#120&安培;#49&安培;#48&安培;#60&安培;#115&安培;#117&安培;#112&安培;#62&安培;#54&安培;#60&安培;#47&安培;#115&安培;#117&安培;#112&安培;#62&安培; #32&安培;#99&安培;#101&安培;#108&安培;#108&安培;#115&安培;#47&安培;#181&安培;#108

从x10 3 细胞转换/μl

1 个答案:

答案 0 :(得分:3)

这里不需要编码这些字符。 XML字符串可以使用UTF-8或其他编码。根据编码,序列化器将根据需要进行编码。

$foo = new SimpleXmlElement('<?xml version="1.0" encoding="UTF-8"?><foo/>');
$foo->addChild('bar', 'μmol/l, x10³ cells/µl'); 
echo $foo->asXml();

输出(未编码的特殊字符):

<?xml version="1.0" encoding="UTF-8"?>
<foo><bar>μmol/l, x10³ cells/µl</bar></foo>

要强制实体使用特殊字符,您需要更改编码:

$foo = new SimpleXmlElement('<?xml version="1.0" encoding="ASCII"?><foo/>');
$foo->addChild('bar', 'μmol/l, x10³ cells/µl');
echo $foo->asXml();

输出(特殊字符编码):

<?xml version="1.0" encoding="ASCII"?>
<foo><bar>&#956;mol/l, x10&#179; cells/&#181;l</bar></foo>

我建议您将自定义编码转换回UTF-8。这样,XML Api可以处理它。如果您希望使用自定义编码存储字符串,则需要解决a bug

&#120&#49&#48&#60&#115&#117这样的字符串会触发SimpleXML / DOM中的错误。 SimpleXMLElement::addChild()DOMDocument::createElement()的第二个参数有一个断开的转义。您需要将内容创建为文本节点并附加它。

这是一个扩展SimpleXMLElement的小类,并添加了一个解决方法:

class MySimpleXMLElement extends SimpleXMLElement {

  public function addChild($nodeName, $content = NULL) {
    $child = parent::addChild($nodeName);
    if (isset($content)) {
      $node = dom_import_simplexml($child);
      $node->appendChild($node->ownerDocument->createTextNode($content));
    }
    return $child;
  }
}

$foo = new MySimpleXmlElement('<?xml version="1.0" encoding="UTF-8"?><foo/>');
$foo->addChild('bar', '&#120&#49&#48&#60&#115&#117'); 
echo $foo->asXml();

输出:

<?xml version="1.0" encoding="UTF-8"?>
<foo><bar>&amp;#120&amp;#49&amp;#48&amp;#60&amp;#115&amp;#117</bar></foo>

自定义编码中的&作为实体&amp;进行转义 - 因为它是XML中的特殊字符。 XML解析器将对其进行解码。

$xml = <<<'XML'
<?xml version="1.0" encoding="UTF-8"?>
<foo><bar>&amp;#120&amp;#49&amp;#48&amp;#60&amp;#115&amp;#117</bar></foo>
XML;

$foo = new SimpleXMLElement($xml);
var_dump((string)$foo->bar);

输出:

string(27) "&#120&#49&#48&#60&#115&#117"