以PHP词条为例,PHP先读取:
http://zh.wikipedia.org/w/api.php?action=parse&format=xml&page=PHP
<text xml:space="preserve">下的内容,然后进行过滤,显示到网页上。
为了方便测试,原内容保存到一个html中“wiki.html”
作者共发了5篇帖子。
![]() |
以PHP词条为例,PHP先读取:
http://zh.wikipedia.org/w/api.php?action=parse&format=xml&page=PHP <text xml:space="preserve">下的内容,然后进行过滤,显示到网页上。 为了方便测试,原内容保存到一个html中“wiki.html” ![]() ![]() |
![]() |
<?php
mb_internal_encoding("UTF-8"); // encoding for mbstring include 'phpQuery-onefile.php'; ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>DOM</title> </head> <body> <?php $doc = phpQuery::newDocumentFileHTML("wiki.html"); $doc["table"]->remove(); // remove tables as well as their contents $doc[":header"]->remove(); // remove <h1>-<h6> $doc["#coordinates"]->remove(); // remove the float layer 'coordinates' $doc["span.editsection"]->remove(); // remove Links 'edit' $doc["small"]->remove(); // remove <span> but keep its content foreach ($doc["span"] as $span) { $span = pq($span); $span->after($span->text())->remove(); } $doc["div"]->remove(); $doc["script"]->remove(); // remove [number] foreach ($doc["a"] as $a) { $a = pq($a); if (preg_match("/^\\[\\d+\\]$/", trim($a->text()))) { $a->remove(); } } $doc->html($doc->text()); // remove all other html labels $html = $doc->html(); $html = preg_replace("/\\n{2,}/", "\n", $html); // \n\n\n\n\n => \n define("MAXLEN", 400); $shortened = false; if (mb_strlen($html) > MAXLEN) { $html = mb_substr($html, 0, MAXLEN); $shortened = true; } $html = preg_replace("/^[\\n\\s]+/", "", $html); // remove \n\s\n\s\n\s at the beginning $html = preg_replace("/[\\n\\s]+$/", "", $html); // remove \n\s\n\s\n\s at the end $html = preg_replace("/\\n/", "<br>", $html); // \n => <br> if ($shortened) { $html .= "..."; } $doc->html($html); echo $doc->html(); ?> </body> </html> ![]() ![]() |
![]() |
输出结果如下:
PHP(全称:PHP:Hypertext Preprocessor,即“PHP:超文本预处理器”)是一种开源的通用计算机脚本语言,尤其适用于网络开发并可嵌入HTML中使用。PHP的语法借 鉴吸收C语言、Java和Perl等流行计算机语言的特点,易于一般程序员学习。PHP的主要目标是允许网络开发人员快速编写动态页面,但PHP也被用于 其他很多领域。 PHP最初是由勒多夫在1995年开始开发的。而现在PHP的标准由PHP Group和开放源代码社群维护。PHP以PHP License作为许可协议,不过因为这个协议限制了PHP名称的使用,所以和开放源代码许可协议GPL不相容。 PHP的应用范围相当广泛,尤其是在网页程式的开发上。一般来说PHP大多执行在网页服务器上,透过执行PHP程式码来产生使用者浏览的网页。PHP可以在多数的服务器和操作系统上执行,而且使用PHP完全是免费的。根据2013年4月的... ![]() ![]() |
![]() |
PHP读取维基百科服务器上的xml文件,可以用curl库连接维基百科服务器并读取xml文件内容,保存到一个变量中,然后用simplexml_load_file()函数读取这个存有xml字符串的变量,读取<text xml:space="preserve"></text>之间的内容,然后就可以继续操作了。
![]() ![]() |
![]() |
PhpQuery貌似自带了从其他服务器上获取网页类容的功能,也还带有服务器端ajax功能,因此不需要自己再单独写curl网页获取代码。
![]() ![]() |