Use of xml in php (SEO)

Source: Internet
Author: User
: This article mainly introduces the use of xml (SEO) in php. if you are interested in the PHP Tutorial, please refer to it. For the use of xml, this is the two functions written for SEO. they are for reference only. You are welcome to discuss them. if you have any questions, please feel free to ask questions. for specific functions, please refer to the document.

Functionedit_xml_file ($ xml_file_path, $ datas, $ is_index = false, $ is_compress = true) {$ doc = new DomDocument ('1. 0', 'utf-8'); $ doc-> formatOutput = true; $ flag = false; if (! @ File_exists ($ xml_file_path) |! $ Doc-> load ($ xml_file_path) {$ flag = create_xml_file ($ xml_file_path, $ datas, $ is_index, $ is_compress); return $ flag ;} // read file data $ xmldata = simplexml_load_file ($ xml_file_path); $ newxmldata = array (); $ xmL_url = array (); foreach ($ xmldataas $ key => $ obj) {$ obj = (array) $ obj; if (isset ($ obj ['lastmod']) {$ obj ['lastmod'] = date ('C ', time () ;}$ xmL_url [] = $ obj ['loc ']; $ newxmldata [] [$ key] = $ obj;} // place the newly added data in Foreach ($ datasas $ data) {if (! In_array ($ data [$ key] ['loc '], $ xmL_url) {$ newxmldata [] = $ data ;}} // re-update the created file based on the new data if (! Empty ($ newxmldata) & unlink ($ xml_file_path) {$ flag = create_xml_file ($ xml_file_path, $ newxmldata, $ is_index, $ is_compress);} unset ($ datas ); return $ flag;}/** xml, append data * @ param xml_file_path file path, $ data, $ is_index to distinguish between map files and map indexes, the default is map file * @ author zdj * @ date 2015-01-23 */functionadd_data_xml_file ($ xml_file_path, $ datas, $ is_index = false, $ is_compress = true) {$ doc = new DomDocument ('1. 0', 'utf-8'); $ d Oc-> formatOutput = true; $ flag = false; if (! @ File_exists ($ xml_file_path) |! $ Doc-> load ($ xml_file_path) {$ flag = create_xml_file ($ xml_file_path, $ datas, $ is_index, $ is_compress); return $ flag ;} $ root = $ doc-> documentElement; foreach ($ datasas $ data) {foreach ($ dataas $ key => $ obj) {$ key_dom = $ doc-> createElement ($ key); foreach ($ objas $ attribute => $ attribute_value) {$ attribute_dom = $ doc-> createElement ($ attribute ); $ attribute_dom_value = $ doc-> createTextNode ($ attribute_value); $ attribute_dom-> appendChild ($ attribute_dom_value); $ key_dom-> appendChild ($ attribute_dom );} $ root-> appendChild ($ key_dom) ;}$ doc-> appendChild ($ root); $ doc-> save ($ xml_file_path ); // Create a compressed file $ xml_gz_file_path = export xml_file_path.'.gz '; if ($ is_compress) {$ fp = gzopen ($ xml_gz_file_path, 'w9'); gzwrite ($ fp, $ doc-> saveXML (); gzclose ($ fp);} // grant permissions to files/grant permissions to files if (file_exists ($ xml_file_path )) {chmod ($ xml_file_path, 0744);} if (file_exists ($ xml_gz_file_path) {chmod ($ xml_gz_file_path, 0744);} unset ($ datas );}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

The above introduces the use of xml in php (SEO), including some content, and hope to be helpful to friends who are interested in PHP tutorials.

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.