PHP handles files, reads one line at a line, and deletes useless rows.

Source: Internet
Author: User

Do sitemap.xml today. Find a foreign site, http://www.freesitemapgenerator.com/This can generate 5,000 data, previously found that can only generate 500. However, some of the generated XML tags are useless, 650) this.width=650; "Src=" http://s3.51cto.com/wyfs02/M01/5A/12/ Wkiol1t1rsfzleagaayym92b1fs402.jpg "title=" Loc.png "alt=" Wkiol1t1rsfzleagaayym92b1fs402.jpg "/>

So I think of the PHP processing file, a line of reading, and the useless row deleted.

The code is as follows:

<?php set_time_limit (0), $file =fopen (' Sitemap.xml ', ' R '), while (!feof ($file)) {$line = Fgets ($file);  $arr []= $line; $reg = "/<priority>|<lastmod>|<changefreq>/";    if (!preg_match ($reg, $line)) {$arr []= $line;} if ()//echo $line;} Fclose ($file);//var_dump ($arr), $str = Implode ("\ r \ n", $arr); File_put_contents ("1.xml", $str);

But login a company's again let Change, say is Loc in Can't still have? No. I thought of writing a loop directly, but the time for bitterness began. See http://ningyuqiao.blog.51cto.com/5581274/1616984

Paste the Code bar:

<?php  set_time_limit (0); $xml _str = file_get_ Contents ("1.xml");  $doc  = domdocument::loadxml ($xml _str);    $root  = $ doc->documentelement; $items = $root->getelementsbytagname (' url ');// echo  $length;// die (); for  ($i  = 0;  $i  <  $items->length;  $i + +)  {     // echo  $items->item ($i)->nodevalue .  "\ n";     $url  =   $items->item ($i)->nodevalue;     $reg = "/\?/";     if (Preg_ Match ($reg, $url)) {    echo  $url;     $root->removechild ($items- >item ($i));     }    // sleep (3);} $doc->formatoutput = true; $doc->savexml (), $doc->save ("1.xml"); 

Finally done, the psychology is very happy, share to everyone.

This article is from my blog blog, so be sure to keep this source http://ningyuqiao.blog.51cto.com/5581274/1616986

PHP handles files, reads one line at a line, and deletes useless rows.

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.