这篇文章主要知识点是关于php、DOM、乱码、的内容,如果大家想对相关知识点有系统深入的学习,可以参阅以下电子书
前言
DOM是php比较新的xml和html处理类,可以像javascript那样方便的操作DOM树,网上更多的是介绍它处理XML的情况,今天这篇文章就介绍下php解决DOM乱码的方法,下面话不多说,直接看下面的解决方法。
解决方法如下
/** * 请求url页面信息 * @param str $url * @return str mixed|boolean */ function curl_get($url) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //302跳转 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0'); curl_setopt($curl, CURLOPT_REFERER, $url); $data = curl_exec($curl); $code = curl_getinfo($curl,CURLINFO_HTTP_CODE); //输出请求状态码 curl_close($curl); if(200 == $code) { //解决乱码 if (preg_match('#<meta[^>]*charset="?gb2312"[^>]*>#', $data)) { $data = iconv("gb2312","utf-8//IGNORE",$data); $data = preg_replace('#<meta[^>]*charset="?gb2312"[^>]*>#is', '<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">', $data); } if (!preg_match('#<meta charset="utf-8"[^>]*>#is', $data)) { $data = str_replace('<head>', '<head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">', $data); } if (preg_match('#<meta charset="utf-8"[^>]*>#is', $data)) { $data = preg_replace('#<meta charset="utf-8"[^>]*>#is', '<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">', $data); } return $data; } else { return false; } }
/** * 获取 DOMDocument 对象 * @param str $url * @return boolean|DOM */ function getDom($url) { $html_content = curl_get($url); if(empty($html_content)) { //saveLog($url, '请求失败'); return false; } $dom = new DOMDocument('1.0', 'utf-8'); libxml_use_internal_errors(true); $dom->loadHTML($html_content); return $dom; }
$html_content = mb_convert_encoding($html_content, 'UTF-8', 'gb2312');
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。
以上就是本次给大家分享的关于java的全部知识点内容总结,大家还可以在下方相关文章里找到相关文章进一步学习,感谢大家的阅读和支持。
Copyright 2018-2020 www.xz577.com 码农之家
版权投诉 / 书籍推广 / 赞助:520161757@qq.com
php基于dom实现读取图书xml格式数据的方法
本文实例讲述了php基于dom实现读取图书xml格式数据的方法。分享给大家供大家参考,具体如下: ?php $doc = new DOMDocument(); $doc-load( 'books.xml' ); $books = $doc-getElementsByTagName( "book" ); foreach( $books as $book ) { $authors = $book-getElementsByTagName( "author" ); $author = $authors-item(0)-nodeValue; $publishers = $book-getElementsByTagName( "publisher" ); $publisher = $publishers-item(0)-nodeValue; $titles = $book-getElementsByTagName( "title" ); $title = $titles-item(0)-nodeValue; echo "$title - $author - $publisher\n"; }? books.xml文件如下: ?xml version="1.0"?books book authorJack Herrington/author titlePHP Hacks/title publisherO'Reilly/publisher /book book authorJack Herrington/author titlePodcasting Hacks/title publisherO'Reilly/publisher /book/books 运行结果如下: PHP Hacks - Jack Herrington - O'ReillyPodcasting Hacks - Jack Herrington - O'Reilly PS:这里再为大家提供几款关于xml操作的在线工具供大家参考……
php基于dom实现的图书xml格式数据示例
本文实例讲述了php基于dom实现的图书xml格式数据。分享给大家供大家参考,具体如下: ?php $books = array(); $books [] = array( 'title' = 'PHP Hacks', 'author' = 'Jack Herrington', 'publisher' = "O'Reilly" ); $books [] = array( 'title' = 'Podcasting Hacks', 'author' = 'Jack Herrington', 'publisher' = "O'Reilly" ); $doc = new DOMDocument(); $doc-formatOutput = true; $r = $doc-createElement( "books" ); $doc-appendChild( $r ); foreach( $books as $book ) { $b = $doc-createElement( "book" ); $author = $doc-createElement( "author" ); $author-appendChild( $doc-createTextNode( $book['author'] ) ); $b-appendChild( $author ); $title = $doc-createElement( "title" ); $title-appendChild( $doc-createTextNode( $book['title'] ) ); $b-appendChild( $title ); $publisher = $doc-createElement( "publisher" ); $publisher-appendChild( $doc-createTextNode( $book['publisher'] ) ); $b-appendChild( $publisher ); $r-appendChild( $b ); } echo $doc-saveXML();? 运行结果如下: ?xml ……