php处理文件,一行一行的读取,并且把没用的行删除。

简介:

今天做sitemap.xml.找了个国外的网站,http://www.freesitemapgenerator.com/这个可以生成5000条数据,以前找那个只能生成500条。但是,生成的xml标签中有些是没有用的,如图:wKioL1T1rSfzLEAgAAYyM92B1fs402.jpg

于是想到了php处理文件,一行一行的读取,并且把没用的行删除。

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?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 );

但是登录一会公司的又让改,说是loc中不能还有?号。我又想到直接写个循环不就行了,但是苦逼的时间开始了。见http://ningyuqiao.blog.51cto.com/5581274/1616984

贴代码吧:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?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" );

最终搞定了,心理挺开心的,分享给大家。





      本文转自ning1022 51CTO博客,原文链接:http://blog.51cto.com/ning1022/1616986,如需转载请自行联系原作者




相关文章
|
1月前
thinkphp5.1隐藏index.php入口文件
thinkphp5.1隐藏index.php入口文件
30 0
thinkphp5.1隐藏index.php入口文件
|
3月前
|
PHP 数据安全/隐私保护
php案例:判断这个文件是什么编程语言代码的文件(判断java或者php)
php案例:判断这个文件是什么编程语言代码的文件(判断java或者php)
php案例:判断这个文件是什么编程语言代码的文件(判断java或者php)
php案例:用代码的方式创建目录+文件+写入数据(都由你定)
php案例:用代码的方式创建目录+文件+写入数据(都由你定)
php案例:用代码的方式创建目录+文件+写入数据(都由你定)