今天做sitemap.xml.找了個國外的網站,http://www.freesitemapgenerator.com/這個可以生成5000條數據,以前找那個只能生成500條。但是,生成的xml標籤中有些是沒有用的,如圖:
於是想到了php處理文件,一行一行的讀取,並且把沒用的行刪除。
代碼如下:
<?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
貼代碼吧:
<?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");
最終搞定了,心理挺開心的,分享給大家。