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
12
13
|
Zhang Ying
Male
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...