用php正则表达式来记取数据
- xml源文件
-
-
-
- 张映
- 男
- 28
-
-
- tank
- 男
- 28
-
-
-
-
$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]."
" ;
- }
- ?>
复制代码
|