Code代码片断(5do8)

PHPMysqlApacheLinux

新主题
php escape URL 编码
php中文字符串截取函数
file_get_contents post数...
获取文本内容
php文件写入
检查url网址是否存在
php输出系统状态函数
获取网页中标签之间的数据...
php中的模板替换常用方法
说话自由

首页 » LAMP » PHP »

获取网页中标签之间的数据

标签: html php find remote URL search tag spider grab grabber

<?php

$config['url']       = "http://www.5do8.com/"; // url of html to grab
$config['start_tag'] = "<b>"; // where you want to start grabbing
$config['end_tag']   = "</b>"; // where you want to stop grabbing
$config['show_tags'] = 0; // do you want the tags to be shown when you show the html? 1 = yes, 0 = no

class grabber
{
    var $error = '';
    var $html  = '';
    
    function grabhtml( $url, $start, $end )
    {
        $file = file_get_contents( $url );
        
        if( $file )
        {
            if( preg_match_all( "#$start(.*?)$end#s", $file, $match ) )
            {                
                $this->html = $match;
            }
            else
            {
                $this->error = "Tags cannot be found.";
            }
        }
        else
        {
            $this->error = "Site cannot be found!";
        }
    }
    
    function strip( $html, $show, $start, $end )
    {
        if( !$show )
        {
            $html = str_replace( $start, "", $html );
            $html = str_replace( $end, "", $html );
            
            return $html;
        }
        else
        {
            return $html;
        }
    }
}

$grab = new grabber;
$grab->grabhtml( $config['url'], $config['start_tag'], $config['end_tag'] );

echo $grab->error;

foreach( $grab->html[0] as $html )
{
    echo htmlspecialchars( $grab->strip( $html, $config['show_tags'], $config['start_tag'], $config['end_tag'] ) ) . "<br>";
}

?>


获取一个网址的源码,然后获取指定标签之间的内容。也可以使用正则。

ccdot写于2008-7-3 16:01:20

如果愿意,请留下你观点或者感受...
称呼*
内容*
验证码*