-
-
-
-
- Zhang Ying
-
-
- 28
-
- ;old>28
-
-
-
-
Copy code
2. PHP processing file, rexml.php
/*** - @ Read xml file using regular expression
- @ Recoded By Androidyue
- @ site bbs.it-home.org
- ***/
- $xml = "";
- //Open the read file in read-only mode
- $f = fopen('person.xml', 'r');
- //Format to get the read data
- while( $data = fread( $f, 4096 ) ) {
- $xml .= $data;
- }
- //Close an open file pointer
- fclose( $f );
- // Read the data above
- preg_match_all( "//(.*?)//humans/>/s", $xml, $humans ) ; //Match the content in the outermost tag
- //Format data
- foreach( $humans[1] as $k=>$human ){
- preg_match_all( "//(.* ?)//name/>/", $human, $name ); //Match the name
- preg_match_all( "//(.*?)//sex/ >/", $human, $sex ); //Match gender
- preg_match_all( "//(.*?)//old/>/", $human, $ old ); //Match the age
- }
- //Process the result data
- foreach($name[1] as $key=>$val){
- echo $val." - ".$sex[$key][ 1]." - ".$old[$key][1]."
" ;
- }
- ?>
-
-
-
Copy code
Note:
Please keep the encoding format of the two files consistent. It is best to set both to the UTF8 character set to avoid inexplicable errors.
|