Example code for reading xml data using PHP regular expressions-php Tutorial

Source: Internet
Author: User
Example code for reading xml data using PHP regular expressions

  1. Zhang Ying
  2. Male
  3. 28
  4. Tank
  5. Male
  6. 28

2. php file processing, rexml. php

  1. /***
  2. @ Read xml files using regular expressions
  3. @ Recoded By Androidyue
  4. @ Site bbs.it-home.org
  5. ***/
  6. $ Xml = "";
  7. // Open the read object in read-only mode
  8. $ F = fopen ('person. XML', 'r ');
  9. // Format and obtain the read data
  10. While ($ data = fread ($ f, 4096 )){
  11. $ Xml. = $ data;
  12. }
  13. // Close an opened file pointer
  14. Fclose ($ f );
  15. // Read data above
  16. Preg_match_all ("// (.*?) / /S ", $ xml, $ humans); // match the content in the outermost tag
  17. // Format the data
  18. Foreach ($ humans [1] as $ k => $ human ){
  19. Preg_match_all ("// (.*?) / /", $ Human, $ name); // match the name
  20. Preg_match_all ("// (.*?) / /", $ Human, $ sex); // match the gender
  21. Preg_match_all ("// (.*?) / /", $ Human, $ old); // match the age
  22. }
  23. // Process result data
  24. Foreach ($ name [1] as $ key => $ val ){
  25. Echo $ val. "-". $ sex [$ key] [1]. "-". $ old [$ key] [1]."
    ";
  26. }
  27. ?>

Note: the encoding formats of the two files must be consistent. it is best to set them to the UTF8 character set to avoid inexplicable errors.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.