In php array to xml, we learned to write this in php.
The code is as follows: |
Copy code |
Function array2xml ($ array, $ xml = false ){ If ($ xml = false ){ $ Xml = new SimpleXMLElement ('<root/> '); } Foreach ($ array as $ key => $ value ){ If (is_array ($ value )){ Array2xml ($ value, $ xml-> addChild ($ key )); } Else { $ Xml-> addChild ($ key, $ value ); } } Return $ xml-> asXML (); } Header ('content-type: text/XML '); Print array2xml ($ array );
|
If the content contains Chinese characters, it is null.
Solution: Transcoding
The code is as follows: |
Copy code |
Function array2xml ($ array, $ xml = false ){ If ($ xml = false ){ $ Xml = new SimpleXMLElement ('<root/> '); } Foreach ($ array as $ key => $ value ){ If (is_array ($ value )){ Array2xml ($ value, $ xml-> addChild ($ key )); } Else { // $ Value = utf8_encode ($ value ); If (preg_match ("/([x81-xfe] [x40-xfe])/", $ value, $ match )){ $ Value = iconv ('gbk', 'utf-8', $ value ); // Determine whether there are any Chinese characters } $ Xml-> addChild ($ key, $ value ); } } Return $ xml-> asXML (); } |
Here are some examples of Chinese character regular expressions.
1. Determine whether the string is full of Chinese characters
The code is as follows: |
Copy code |
<? Php $ Str = 'All are Chinese character test '; If (Preg_match_all ("/^ ([x81-xfe] [x40-xfe]) + $/", $ str, $ match)){ Echo 'all Chinese characters '; } Else { Echo 'all Chinese characters '; } ?> |
When $ str = 'All are Chinese character test', the output "all are Chinese characters ";
When $ str = 'All is a Chinese character test', the output is "not all Chinese characters ";