首页 > 后端开发 > php教程 > php中的关于xml的使用(SEO)

php中的关于xml的使用(SEO)

WBOY
发布: 2016-08-08 09:19:02
原创
1077 人浏览过

关于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
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板