Home >php教程 >php手册 >php通过正则表达式记取数据来读取xml的方法,正则表达式xml

php通过正则表达式记取数据来读取xml的方法,正则表达式xml

WBOY
WBOYOriginal
2016-06-13 09:12:541019browse

php通过正则表达式记取数据来读取xml的方法,正则表达式xml

本文实例讲述了php通过正则表达式记取数据来读取xml的方法。分享给大家供大家参考。具体分析如下:

xml源文件如下:

<&#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>

php文件如下:

<&#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;>

希望本文所述对大家的php程序设计有所帮助。

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