当前位置:Gxlcms > PHP教程 > php通过正则表达式记取数据来读取xml的方法_PHP教程

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

时间:2021-07-01 10:21:17 帮助过:5人阅读

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


这篇文章主要介绍了php通过正则表达式记取数据来读取xml的方法,实例分析了php正则表达式的技巧及读取XML文件的方法,需要的朋友可以参考下

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

xml源文件如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

张映

28

tank

28

php文件如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

$xml = "";

$f = fopen('person.xml', 'r');

while($data = fread($f,4096)){

$xml .= $data;

}

fclose( $f );

// 上面读取数据

preg_match_all("/\(.*?)\<\/humans\>/s",$xml,$humans);

//匹配最外层标签里面的内容

foreach( $humans[1] as $k=>$human )

{

preg_match_all("/\(.*?)\<\/name\>/",$human,$name);

//匹配出名字

preg_match_all("/\(.*?)\<\/sex\>/",$human,$sex);

//匹配出性别

preg_match_all("/\(.*?)\<\/old\>/",$human,$old);

//匹配出年龄

}

foreach($name[1] as $key=>$val){

echo $val." - ".$sex[$key][1]." - ".$old[$key][1]."
" ;

}

?>

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

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/965527.htmlTechArticlephp通过正则表达式记取数据来读取xml的方法 这篇文章主要介绍了php通过正则表达式记取数据来读取xml的方法,实例分析了php正则表达式的技...

人气教程排行