Php's solution to reading text files

Source: Internet
Author: User
After php reads a text file and opens it with fopen, it will read it in one row, and filter it out starting with # and @. If & lt; node & gt; or & lt; leaf & gt; (start to read when the tag starts) the three attributes in the read, and the next tag & lt; leaf & gt; or & lt;/leaf & gt; or & lt; /node & gt php reads text files
When fopen is enabled, one row is read, and the values starting with # and @ are filtered out. Or (Start to read when the tag starts) read the three attributes, and then the next tag Or Or Then, take out the first three elements as an array, that is, read the array of three elements in the middle of each tag for processing.
How to write this method?
# Vision1.1
@ Fileid = 0010.

Name = xx
Id = 0
Orth = zzz

Name = xx
Id = 801
Orth =/btf/sub.txt


Name = xx
Id = 101
Orth =/bbs/test.txt



------ Solution --------------------
Filter the # and @ of the first two rows for processing, from Start:
When $ FirstNode =" "; Make $ firstNode =" ";
Then one row is processed to determine if the current row is =$ firstNode,
So far...
This is To retrieve all the contents.

As for <>... regular expression processing ..
------ Solution --------------------
PHP code
$ Fn = 'data2.txt '; $ fp = fopen ($ fn, 'r'); $ s = ''; while ($ buf = fgets ($ fp )) {if (in_array ($ buf {0}, array ('#', '@') continue; if ($ buf {0} = '<') {if ($ s) {parse_str ($ s, $ t); $ r [] = $ t; $ s = '';} continue;} $ s. = ($ s? '&': ''). $ Buf;} fclose ($ fp); print_r ($ r );
------ Solution --------------------
You can solve the problem by nagging the boss. you should.

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.