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

WBOY
Release: 2016-07-13 10:04:27
Original
825 people have browsed it

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>
Copy after login

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;>
Copy after login

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...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!