php web robot php 提取超链接

The docs for the this robot class are here.
<?php
/*
Copyright (C) 2005 Sam J. Clarke
All rights reserved.

+-------------------------------------------+
|                                           |
|              PHP Robot Class              |
|                                           |
+-------------------------------------------+
|                                           |
| Author Name: Sam J. Clarke                |
| Author Email: admin@free-php.org.uk       |
| Author URI: http://www.free-php.org.uk/   |
| Description: This script is a robot class |
| to help you build web robots.             |
|                                            |
+-------------------------------------------+
|                                           |
| If you like this, Please link back to us. |
|                                           |
+-------------------------------------------+


LICENSE

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License (GPL)
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

To read the license please visit http://www.gnu.org/copyleft/gpl.html
*/


class Robot {

   var
$Agent = '-'; // user agent
   
var $temp_everything = false; // stores what's sent back
   
   
   // gets the status code returned
   // returns false on fail and status code on sucsess
   // you must call the get everything function first
   
function GetStatus()
   {
     
$html = $this->temp_everything; // gets what was sent back
     
if (!$html) // check it's not false
     
{
       return
false; // if it is return false
     
}
     
     
$pieces = preg_split("/(\r\n\r\n|\r\r|\n\n)/", $html, 2); // split the HTML from the headers
     
$headers = preg_split("/(\r\n|\n|\r)/", $pieces [0]); // save the headers
     
unset($pieces); // unset everything else
     
     
for($i=0 ;isset($headers[$i]);$i++)
     {
       
// search for the status code header
       
if (preg_match("/HTTP\/[0-9A-Za-z +]/i", $headers[$i]))
       {
         
// replace everything but the status code
         
$status = preg_replace("/http\/[0-9]\.[0-9] /i", '', $headers[$i]);
       }
     }
     
     return
$status; // return the status code
   
}
   
   
   
// gets everything from the url and stores it in a string
   // returns false on fail true on sucsess
   
function GetEverything($url)
   {
     
$info = @parse_url ($url); // parse the url
     
$fp = @fsockopen($info["host"], 80, $errno, $errstr, 10); // open a socket
     
     
if (!$fp) // check it worked
     
{
        return
false; // if it didn't return false
     
}
     else
     {

       if (empty(
$info['path'])) // if the path is empty
       
{
         
$info['path'] = '/'; // then set the path to /
       
}
       
       if (isset(
$info["query"])) // check if there is a query string
       
{
         
$query = '?' .$info["query"]; // if there is get it ready to use
       
}
       else
       {
         
$query = ''; // if not make an empty string
       
}
        
       
// HTTP headers to send
       
$out  = "GET ".$info[ "path"]."".$query." HTTP/1.0\r\n" ;
       
$out .= "Host: ".$info[ "host"]."\r\n";
       
$out .= "Connection: close \r\n";
       
$out .= "User-Agent: ". $this->Agent."\r\n\r\n";
        
fwrite ( $fp, $out ); // write the HTTP headers to the socket
       
       
$html = ''; // make an empty string to store them in
       
       
while (!feof($fp)) // while not end of socket
       
{
         
$html .= fread($fp, 8192); // read from the socket and add it to the string
       
}
       
       
fclose($fp); // close the socket
       
       
$this->temp_everything = $html; // save the string
       
       
return true; // return true
     
}
   }
   
   
   
// returns what was read from the socket
   // returns everything that was read from the socket or false
   // you must call the get everything function first
   
function ReturnEverything()
   {
     
$html = $this->temp_everything; // gets what was read from the socket
     
return $html;
   }
   

    
// gets an array of urls from the web page
   // returns an array of urls or false on fail
   // you must call the get everything function first
   
function GetUrls($url)
   {
     
$info = @parse_url($url); // parse the url
     
     
$html = $this->temp_everything; // gets what was sent back
     
if (!$html) // check it's not false
     
{
       return
false; // if it is return false
     
}
     
     
$pieces = preg_split ("/(\r\n\r\n|\r\r|\n\n)/", $html, 2); // split the HTML from the headers
     
$html = $pieces[1]; // save the HTML
     
unset($pieces); // unset everything else
     
     // find all the urls
     
preg_match_all("|href\=\"?'?`?([[:alnum:]:?=&@/;._-]+)\"?'?`?|i", $html, &$matches);
     
     
$links = array(); // make an array to store them in
     
$ret = $matches[1];
     for(
$i=0;isset($ret[$i]);$i++)
     {
       
// if it starts with http:// save it without editing
       
if(preg_match("|^http://(.*)|i",$ret[$i]))
       {
         
$links[] = $ret[$i];
       }
       
       
// if it matches /place.html
       
elseif(preg_match("|^/(.*)|i",$ret[ $i]))
       {
         
// add it to the host name and save it
         
$links[] = 'http://'.$info["host"].''.$ret[$i];
       }
       
       
// if it maches mailto:
       
elseif(preg_match("/^mailto:(.*)/i",$ret[$i]))
       {
         
// could save email addresses here
       
}
     }
     
     return
$links ; // return the array of links
   
}
   
   
   
// gets the headers returned
   // returns false on fail headers on sucsess
   // you must call the get everything function first
   
function GetHeaders()
   {
     
$html = $this->temp_everything; // gets what was sent back
     
if (!$html) // check it's not false
     
{
       return
false; // if it is return false
     
}

     
$pieces = preg_split("/(\r\n\r\n|\r\r|\n\n)/", $html, 2); // split the HTML from the headers
     
return $pieces[0]; // return the headers
   
}
   
   
   
// gets the html of a page
   // returns false on fail HTML on sucsess
   // you must call the get everything function first
   
function GetHTML()
   {
     
$html = $this->temp_everything; // gets what was sent back
     
if (!$html) // check it's not false
     
{
       return
false; // if it is return false
     
}
     
      
$pieces = preg_split("/(\r\n\r\n|\r\r|\n\n)/", $html, 2); // split the HTML from the headers
     
     
return $pieces[1]; // return the HTML
   
}
   
   
   
// Gets the text of a web page
   // returns false on fail text on sucsess
   // you must call the get everything function first
   
function GetTEXT()
   {
     
$html = $this->temp_everything; // gets what was sent back
     
if (!$html) // check it's not false
      
{
       return
false; // if it is return false
     
}

     
$pieces = preg_split("/(\r\n\r\n|\r\r|\n\n)/", $html, 2); // split the HTML from the headers
     
     // strip the HTML off and just leave text
     
$html = preg_replace('@<script[^>]*
?>
.*?@si', ' ', $pieces[1]); $html = preg_replace('@]*?>.*?@si', ' ', $html); $html = strip_tags($html); $html = preg_replace('@&#(\d+);@e', ' ', $html); $html = str_replace('&', ' ', $html); $html = str_replace('<', ' ', $html); $html = str_replace('>' , ' ', $html); $html = str_replace(' ', ' ', $html); $html = str_replace('¡', ' ', $html); $html = str_replace('¢', ' ', $html); $html = str_replace('£', ' ', $html ); $html = str_replace('©', ' ', $html); $html = preg_replace('@(\r+|\n+| +)@s', ' ', $html); return $html; // return the text } } // below is some example code // make the robot $robot = new robot; // must be called before any other function $robot->GetEverything('http://www.free-php.org.uk/'); // echo out the text from free-php.org.uk echo 'TEXT from free-php.org.uk:' . $robot->GetTEXT() . '

'."\n"; // echo out the HTML from free-php.org.uk echo 'HTML from free-php.org.uk:' . $robot->GetHTML() . '

'."\n"; // echo out the Headers from free-php.org.uk echo 'Headers from free-php.org.uk:' . $robot->GetHeaders() . '

'."\n"; // echo out the urls from free-php.org.uk echo 'Array of urls from free-php.org.uk:' . print_r($robot->GetUrls('http://www.free-php.org.uk/')) . '

'."\n"; // echo out everything from free-php.org.uk echo 'Everything from free-php.org.uk:' . $robot->ReturnEverything() . '

'."\n"; // echo out the status code from free-php.org.uk echo 'The status code from free-php.org.uk:' . $robot->GetStatus() . '

'."\n"; ?>

为什么不应该写char *p="hello"[zz]

发信人: ilovecpp (cpp), 信区: CPlusPlus
标 题: [FAQ]为什么不应该写char *p="hello"
发信站: BBS 水木清华站 (Tue Jan 14 16:53:26 2003), 转信

char *p="hello";不应该存在于今天的C++程序中了。
这种写法完全是为了保持对C中过去通行的(错误的)
写法的兼容性而对C++类型系统不得已的破坏。

不仅从原理上毫无道理,正如RoachCock所言,由p改写
"hello"会直接引发CPU异常。

此写法已被声明为deprecated,这意味着在未来的某一天
你的程序将不能通过编译。

in iso 2.13.4 string literal

如何读书--做一个真正有知识的人

余英时

  中国传统的读书法,讲得最亲切有昧的无过于朱熹。《朱子语类》中有《总论为学之方》一卷和《读书法》两卷,我希望读者肯花点时间去读一读,对于怎样进入中国旧学间的世界一定有很大的帮助。朱子不但现身说法,而且也总结了荀子以来的读书经验,最能为我们指点门迳。
      
  我们不要以为这是中国的旧方法,和今天西方的新方法相比早已落伍了。我曾经比较过朱子读书法和今天西方所谓“诠释学”的异同,发现彼此相通之处甚多。“诠释学”所分析的各种层次,大致都可以在朱子的《语类》和《文集》中找得到。

gb2312.txt

http://www.fayland.org/journal/050115.html
gb2312.enc
Category: Miscellaneous Keywords: gb2312.enc

我用XML::RSS创建rss文件是没问题,更新的时候却总是乱码。
原因可能是创建rss时我是提取mysql数据库里的资料(utf8编码的), 而更新的时候是直接使用表单里的数据,不是utf8编码的。
我试了use Encode qw/encode decode/;可没结果,将表单数据转换后,在网页编码为utf8时正确显示,写入的时候却成了乱码。
实在想不到啥好办法,只好想指定XML::RSS的encoding为gb2312试试,读取时却发现少了gb2312.enc

利用 PHP 将 HTML 转化为 WML2005

利用 PHP 将 HTML 转化为 WML2005-07-05 @ 12:04:32 · 作者 andot · 归类于 PHP, WML

原创作品,转载请注明出处。

最近在做学校的 WAP 网站,其中通知公告、新闻等信息来源于原来的学校网站上的信息,因此就涉及到一个 HTML 转化为 WML 的问题。尽管现在的 WAP 2.0 支持 XHTML 的内容显示,但是 XHTML 语法要求比 HTML 严格,我们来源信息的 HTML 内容并不能保证符合 XHTML 的要求,而且 XHTML 相对于 WML 来说,内容的流量还是相对比较多一些的,另外仅支持 WAP 1.x 的手机设备也不支持 XHTML,因此为了支持更多的手机设备用户能够正常的浏览我们 WAP 网站,我们采用了 WML,而不是 XHTML。

对于新闻、通知、公告之类的信息,对手机设备用户来说,最关心的是文字内容,因此我们的主要工作就是按照一定的格式(比如该换行分段的地方要换行分段)来从原来的 HTML 页面里提取出纯文本信息,并将其转化为 WML。下面的代码演示了如何来实现这个步骤:

下载: html2wml.php
<?php

/**
* @author 马秉尧
* @copyright 2005 CoolCode.CN
*/

function text2wml($content) {
// 将 WML 变量前缀"$"转义
$content = str_replace('$', '$$', $content);
// 转换特殊字符,并将 Windows/DOS 换行符(\r\n)转化为 Unix 换行符(\n)
$content = str_replace("\r\n", "\n", htmlspecialchars($content));
// 通过换行符来将各行分开进行处理(过滤空行)
$content = explode("\n", $content);
for ($i = 0; $i < count($content); $i++) {
// 过滤首尾空格
$content[$i] = trim($content[$i]);
// 如果去掉全角空格为空行,则设为空行,否则不对全角空格过滤。
if (str_replace(" ", "", $content[$i]) == "") $content[$i] = "";
}
//合并各行,转化为 WML,并过滤掉空行
$content = str_replace("<p><br /></p>

meta search上加个性化定制服务

发信人: heffalumps (幻象·Stay Hungry. Stay Foolish), 信区: ITExpress
标 题: Re: 求助软件设计,征寻牛人
发信站: 水木社区 (Thu Apr 13 20:04:04 2006), 站内

这个点子很老很老了。你找Information Retieval, web search engine
领域的论文,10年前就有人提出meta search engine的概念,
即利用已有的search engine结果,对之重新整合排序后提供给客户,

而且还有比你说的更进一步的主意,
在meta search上加个性化定制服务,因为假设每个人的偏好不同,
就可以在排序时为每个人生成不同的排序结果。个性化搜索是为了弥补meta search

Tripwire 应用 系统完整性检查的软件工具

Tripwire 应用

  对unix管理员来说,主机系统的安全一直是个课题,一方面管理员通过更新patch,安装软硬件防火墙等手段努力使自己的系统可靠性增强,而另一方面unix操作系统的漏洞总是不断被发现并被公布出来,如BUGTRAQ这样的安全列表,从这个角度上可以很绝对的说,互联网上没有安全的主机。任何一台放在Internet上的主机被入侵的潜在可能性是不可逃脱的,而且,对入侵者而言,利用漏洞进入系统往往只是第一步,如果想得到更多的,如超级用户的密码,数据库的口令等,往往需要下点功夫,最便捷也是最有效的就是改动或特洛伊化受侵害的主机上的文件,如放置自己的监听程序,替代某些关键文件,修改编辑可信文件,设置suid文件等。

有感于“离不开MS Word”

发信人: brep (哪个虫儿敢做声), 信区: LinuxApp
标 题: 有感于“离不开MS Word”
发信站: 水木社区 (Mon May 16 13:45:25 2005), 站内

msword vs. tex, GNU vs. microsoft, 这一类的争论一向很吸引眼球,而
且还将继续下去。不过,我认为,这些争论的核心在于--版权法,大多数国
人对于这个东西的重要性并没有足够的认识。

我认为,西方发达国家,如果有所谓传统的话,崇尚科学就是其中之一,而
且已经成了西方社会赖以生存的基石。崇尚科学、尊重知识不仅仅是两个简

标 题: “2>&1"的意思

标 题: “2>&1"的意思
发信站: 水木社区 (Thu Jun 30 15:19:42 2005), 站内
发信人: innobase (身心俱焚), 信区: LinuxApp

我在很多shell例子里看到,有"2>&1 >/dev/null"的写法,例如“/usr/lib/acct/ckpacct > /dev/null 2>&1“有谁知道这"2>&1“的意思吗?

关于, & 1 更准确的说应该是文件描述符 1,而1 一般代表的就是STDOUT_FILENO,实际上这个操作就是一个dup2(2)调用.他标准输出到all_result ,然后复制标准输出到文件描述符2(STDERR_FILENO),其后果就是文件描述符1和2指向同一个文件表项,也可以说错误的输出被合并了

双网卡做负载均衡(zz)

  Linux配置双网卡相信很多人已经很熟悉了,其实在高版本的Linux中已经需要复杂的配置,Linux能够自动识别两块网卡,分别命名为eth0 和eth1,只需要为之指定相应的ip地址就可以了。我们今天要说的双网卡或者说多网卡是他的另外一种应用——将两块网卡配置成为一块使用。
  随着计算机的处理能够力越来越强大,网络的I/O性能越来越成为系统的一个瓶颈,解决的措施是:
  采用更加快速的网络如千兆网络,这需要很大的投资,而且对于改造旧系统的难度很大。