Home > Article > Backend Development > PHP uses regular expressions to remember data and read xml, regular expression xml_PHP tutorial
This article describes the example of php using regular expressions to remember data to read xml. . Share it with everyone for your reference. The specific analysis is as follows:
The xml source file is as follows:
<?xml version="1.0 encoding="UTF-8"?> <humans> <zhangying> <name>张映</name> <sex>男</sex> <old>28</old> </zhangying> <tank> <name>tank</name> <sex>男</sex> <old>28</old> </tank> </humans>
The php file is as follows:
<?php $xml = ""; $f = fopen('person.xml', 'r'); while($data = fread($f,4096)){ $xml .= $data; } fclose( $f ); // 上面读取数据 preg_match_all("/\<humans\>(.*?)\<\/humans\>/s",$xml,$humans); //匹配最外层标签里面的内容 foreach( $humans[1] as $k=>$human ) { preg_match_all("/\<name\>(.*?)\<\/name\>/",$human,$name); //匹配出名字 preg_match_all("/\<sex\>(.*?)\<\/sex\>/",$human,$sex); //匹配出性别 preg_match_all("/\<old\>(.*?)\<\/old\>/",$human,$old); //匹配出年龄 } foreach($name[1] as $key=>$val){ echo $val." - ".$sex[$key][1]." - ".$old[$key][1]."<br>" ; } ?>
I hope this article will be helpful to everyone’s PHP programming design.