Code example for reading xml data using PHP regular expression

WBOY
Release: 2016-07-25 08:59:20
Original
811 people have browsed it
  1. Zhang Ying
  2. 28
  3. ;old>28
Copy code
2. PHP processing file, rexml.php

    /***
  1. @ Read xml file using regular expression
  2. @ Recoded By Androidyue
  3. @ site bbs.it-home.org
  4. ***/
  5. $xml = "";
  6. //Open the read file in read-only mode
  7. $f = fopen('person.xml', 'r');
  8. //Format to get the read data
  9. while( $data = fread( $f, 4096 ) ) {
  10. $xml .= $data;
  11. }
  12. //Close an open file pointer
  13. fclose( $f );
  14. // Read the data above
  15. preg_match_all( "//(.*?)//s", $xml, $humans ) ; //Match the content in the outermost tag
  16. //Format data
  17. foreach( $humans[1] as $k=>$human ){
  18. preg_match_all( "//(.* ?)//", $human, $name ); //Match the name
  19. preg_match_all( "//(.*?)//", $human, $sex ); //Match gender
  20. preg_match_all( "//(.*?)//", $human, $ old ); //Match the age
  21. }
  22. //Process the result data
  23. foreach($name[1] as $key=>$val){
  24. echo $val." - ".$sex[$key][ 1]." - ".$old[$key][1]."
    " ;
  25. }
  26. ?>
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.

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