Home > Backend Development > PHP Tutorial > How to read xml using regular expressions in php_PHP Tutorial

How to read xml using regular expressions in php_PHP Tutorial

WBOY
Release: 2016-07-13 10:04:30
Original
848 people have browsed it

How PHP reads xml by remembering data through regular expressions

This article mainly introduces the method of PHP reading xml by remembering data through regular expressions, example analysis Learned the skills of PHP regular expressions and the method of reading XML files, friends in need can refer to it

The example in this article describes how PHP reads xml by remembering data through regular expressions. Share it with everyone for your reference. The specific analysis is as follows:

The xml source file is as follows:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

张映

28

tank

28

1 2

3

4

5

6

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("/(.*?)/s",$xml,$humans);

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

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

{

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

//匹配出名字

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

//匹配出性别

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

//匹配出年龄

}

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

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

}

?>

7 8

9 10

11

13
Zhang Ying Male
28
tank Male 28
The php file is as follows: ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 (.*?)/s",$xml,$humans); // Match the content inside the outermost tag foreach( $humans[1] as $k=>$human ) { preg_match_all("/(.*?)/",$human,$name); //Match the name preg_match_all("/(.*?)/",$human,$sex); //Match gender preg_match_all("/(.*?)/",$human,$old); //Match age } foreach($name[1] as $key=>$val){ echo $val." - ".$sex[$key][1]." - ".$old[$key][1]."
" ; } ?>
I hope this article will be helpful to everyone’s PHP programming design. http://www.bkjia.com/PHPjc/965527.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/965527.htmlTechArticlephp uses regular expressions to remember data and read xml. This article mainly introduces php to use regular expressions. The method of reading xml by remembering data, analyzing the techniques of php regular expressions with examples...
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