Home  >  Article  >  Backend Development  >  PHP uses regular expressions to remember data and read xml, regular expression xml_PHP tutorial

PHP uses regular expressions to remember data and read xml, regular expression xml_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:04:27843browse

PHP uses regular expressions to remember data to read xml. Regular expression xml

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:

<&#63;xml version="1.0 encoding="UTF-8"&#63;>
<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:

<&#63;php
 $xml = "";
 $f = fopen('person.xml', 'r');
 while($data = fread($f,4096)){
  $xml .= $data;
 }
 fclose( $f );
// 上面读取数据
 preg_match_all("/\<humans\>(.*&#63;)\<\/humans\>/s",$xml,$humans); 
//匹配最外层标签里面的内容
 foreach( $humans[1] as $k=>$human )
 {
  preg_match_all("/\<name\>(.*&#63;)\<\/name\>/",$human,$name);
//匹配出名字
  preg_match_all("/\<sex\>(.*&#63;)\<\/sex\>/",$human,$sex);
//匹配出性别
  preg_match_all("/\<old\>(.*&#63;)\<\/old\>/",$human,$old);
//匹配出年龄
 }
foreach($name[1] as $key=>$val){
 echo $val." - ".$sex[$key][1]." - ".$old[$key][1]."<br>" ;
}
&#63;>

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/965355.htmlTechArticlephp uses regular expressions to remember data to read xml, regular expression xml This article describes how php passes Regular expression method to remember data to read xml. Share it with everyone for everyone...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn