PHP Regular Expression Analysis RSS

Source: Internet
Author: User

Copy codeThe Code is as follows:
$ Xmlfile = 'news/'. date ('ymmdh').'. xml ';
$ Sourcexml = 'HTTP: // www.chinanews.com.cn/rss/scroll-news.xml ';
If (! File_exists ($ xmlfile )){
$ Str = @ file ($ sourcexml) or die ('An error occurred while loading the file. ');
$ Fp = fopen ($ xmlfile, 'w') or die ('write cache failed! ');
Fputs ($ fp, $ str );
Fclose ($ fp );

Then read and output HTML with simplexml
Copy codeThe Code is as follows:
$ Xml = simplexml_load_file ($ xmlfile );
$ C = $ xml-> channel;
$ K = 0;
Foreach ($ c-> item as $ v ){
$ K ++;
Echo"
<Div class = \ "newsline \"> · <a title = \ "$ v-> title \" href = \ "$ v-> link \" target = \"_ blank \ "> $ v-> title </a> </div>
";

Later I encountered a problem, that is, RSS often contains GBK traditional Chinese characters. Google has published many articles on Character Set conversion. A solution is obtained:
Copy codeThe Code is as follows:
$ Str = mb_convert_encoding (join ('', $ str)," gb2312 "," GBK ");

This method cannot be simplified in Traditional Chinese, but it will be converted into a garbled code, but at least simplexml_load_file ($ xmlfile) can be successfully executed. Because these traditional Chinese characters usually appear in the description
.
Then I encountered another problem, as shown in the description: "xinnet, September December 30 (Wen Yu e ?" Simplexml_load_file failed.
So far, we have to use a regular expression to obtain the title and link. (In some articles, regular expressions may be more efficient and will be verified later)
The Code is as follows:
Copy codeThe Code is as follows:
$ Fp = fopen ($ xmlfile, 'R ');
If ($ fp)
{
$ Data = fread ($ fp, filesize ($ xmlfile ));
Fclose ($ fp );
Preg_match_all (
"/<Item> <title> (.*?) <\/Title> <link> (.*?) <\/Link>/I ", $ data, $ out, PREG_SET_ORDER );
Foreach ($ out as $ key => $ v)
{
$ V [1] = iconv ('gbk', 'utf-8', $ v [1]);
Echo"
<Div class = \ "newsline \"> · <a title = \ "$ v [1] \" href = \ "$ v [2] \" target = \"_ blank \ "> $ v [1] </a> </div>
";
}
}

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.