首頁 > 後端開發 > php教程 > php中的關於xml的使用(SEO)

php中的關於xml的使用(SEO)

WBOY
發布: 2016-08-08 09:19:02
原創
1093 人瀏覽過

關於xml使用,這個地方是為了SEO收錄寫的兩個函數,僅供參考,歡迎交流,有問題歡迎提問,具體的函數可以查文檔。

<code><span><span>function</span><span>edit_xml_file</span><span>(<span>$xml_file_path</span>,<span>$datas</span>,<span>$is_index</span> = false,<span>$is_compress</span> = true)</span>{</span><span>$doc</span> = <span>new</span> DomDocument(<span>'1.0'</span>, <span>'utf-8'</span>);
     <span>$doc</span>->formatOutput = <span>true</span>;
     <span>$flag</span> = <span>false</span>;
     <span>if</span>( !@file_exists(<span>$xml_file_path</span>) || !<span>$doc</span>->load(<span>$xml_file_path</span>)) {    
        <span>$flag</span> = create_xml_file(<span>$xml_file_path</span>,<span>$datas</span>,<span>$is_index</span>,<span>$is_compress</span>);
         <span>return</span><span>$flag</span>;
     } 
    <span>//读取文件数据</span><span>$xmldata</span> = simplexml_load_file(<span>$xml_file_path</span>);
    <span>$newxmldata</span> = <span>array</span>();
    <span>$xmL_url</span> = <span>array</span>();
    <span>foreach</span> (<span>$xmldata</span><span>as</span><span>$key</span>=><span>$obj</span>){
         <span>$obj</span> = (<span>array</span>) <span>$obj</span>;
         <span>if</span>(<span>isset</span>(<span>$obj</span>[<span>'lastmod'</span>])){
             <span>$obj</span>[<span>'lastmod'</span>] = date(<span>'c'</span>,time());
         }
         <span>$xmL_url</span>[] = <span>$obj</span>[<span>'loc'</span>];
         <span>$newxmldata</span>[][<span>$key</span>] = <span>$obj</span>;
    }
    <span>//将新添加的数据放在一起</span><span>foreach</span> (<span>$datas</span><span>as</span><span>$data</span>){
        <span>if</span>(!in_array(<span>$data</span>[<span>$key</span>][<span>'loc'</span>], <span>$xmL_url</span>)){
            <span>$newxmldata</span>[] = <span>$data</span>;
        }
    }

    <span>//根据新数据重新更新创建文件</span><span>if</span>(!<span>empty</span>(<span>$newxmldata</span>) && unlink(<span>$xml_file_path</span>)){
        <span>$flag</span> = create_xml_file(<span>$xml_file_path</span>,<span>$newxmldata</span>,<span>$is_index</span>,<span>$is_compress</span>);
    }
    <span>unset</span>(<span>$datas</span>);
    <span>return</span><span>$flag</span>;
}

<span>/** xml,追加数据
 *<span> @param</span> xml_file_path 文件路径,$data 数据,$is_index 为了区分是地图文件还是地图索引,默认是地图文件
 *<span> @author</span> zdj
 *<span> @date</span> 2015-01-23
 */</span><span><span>function</span><span>add_data_xml_file</span><span>(<span>$xml_file_path</span>,<span>$datas</span>,<span>$is_index</span> = false,<span>$is_compress</span> = true)</span>{</span><span>$doc</span> = <span>new</span> DomDocument(<span>'1.0'</span>, <span>'utf-8'</span>);
    <span>$doc</span>->formatOutput = <span>true</span>;
    <span>$flag</span> = <span>false</span>;
    <span>if</span>( !@file_exists(<span>$xml_file_path</span>) || !<span>$doc</span>->load(<span>$xml_file_path</span>)) {
        <span>$flag</span> = create_xml_file(<span>$xml_file_path</span>,<span>$datas</span>,<span>$is_index</span>,<span>$is_compress</span>);
        <span>return</span><span>$flag</span>;
    }
    <span>$root</span> = <span>$doc</span>->documentElement;
    <span>foreach</span> (<span>$datas</span><span>as</span><span>$data</span>){
        <span>foreach</span> (<span>$data</span><span>as</span><span>$key</span>=><span>$obj</span>){
            <span>$key_dom</span> = <span>$doc</span>->createElement(<span>$key</span>);
            <span>foreach</span> (<span>$obj</span><span>as</span><span>$attribute</span> =><span>$attribute_value</span>){
                <span>$attribute_dom</span> = <span>$doc</span>->createElement(<span>$attribute</span>);
                <span>$attribute_dom_value</span> = <span>$doc</span>->createTextNode(<span>$attribute_value</span>);
                <span>$attribute_dom</span>->appendChild(<span>$attribute_dom_value</span>);
                <span>$key_dom</span>->appendChild(<span>$attribute_dom</span>);
            }
            <span>$root</span>->appendChild(<span>$key_dom</span>);
        }
    }
    <span>$doc</span>->appendChild(<span>$root</span>);
    <span>$doc</span>->save(<span>$xml_file_path</span>);
    <span>//创建压缩的文件</span><span>$xml_gz_file_path</span> = <span>$xml_file_path</span>.<span>'.gz'</span>;
    <span>if</span>(<span>$is_compress</span>){
        <span>$fp</span> = gzopen (<span>$xml_gz_file_path</span>, <span>'w9'</span>);
        gzwrite (<span>$fp</span>, <span>$doc</span>->saveXML());
        gzclose(<span>$fp</span>);
    }
    <span>//给文件赋予权限//给文件赋予权限</span><span>if</span>(file_exists(<span>$xml_file_path</span>)){
        chmod(<span>$xml_file_path</span>, <span>0744</span>);
    }
    <span>if</span>(file_exists(<span>$xml_gz_file_path</span>)){
        chmod(<span>$xml_gz_file_path</span>, <span>0744</span>);
    }
    <span>unset</span>(<span>$datas</span>);
}</code>
登入後複製

版權聲明:本文為部落客原創文章,未經部落客允許不得轉載。

以上就介紹了php中的關於xml的使用(SEO),包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板