This article mainly introduces php's method of reading xml by using regular expressions to record data. The example analyzes php's regular expression skills and the method of reading XML files, for more information about how to read xml, see the example in this article. Share it with you for your reference. The specific analysis is as follows:
The xml source file is as follows:
<? Xml version = "1.0 encoding =" UTF-8 "?>
Zhang Ying
Male
28
Tank
Male
28
The PHP file is as follows:
<? Php $ xml = ""; $ f = fopen ('person. XML', 'r'); while ($ data = fread ($ f, 4096) {$ xml. = $ data;} fclose ($ f); // read data preg_match_all ("/\
(.*?) \ <\/Humans \>/s ", $ xml, $ humans ); // match the content of foreach ($ humans [1] as $ k => $ human) {preg_match_all ("/\
(.*?) \ <\/Name \>/", $ human, $ name); // match the name preg_match_all ("/\
(.*?) \ <\/Sex \>/", $ human, $ sex); // match the gender preg_match_all ("/\
(.*?) \ <\/Old \>/", $ human, $ old); // match the age} foreach ($ name [1] as $ key => $ val) {echo $ val. "-". $ sex [$ key] [1]. "-". $ old [$ key] [1]."
";}?>
I hope this article will help you with php programming.