: 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.