我想得到页面上的文字,和连接下的内容得到其余的图片,网站的头部和尾部都不要了,并且我想把如何得到的信息写入数据库中,小弟只明白一点
还指望各位指教:
<?php
$url="http://www.jjxww.com/html/list.aspx?cid=207";
$content=file_get_contents($url);
?>

解决方案 »

  1.   

    <?php
    $sql=mysql_query("insert into 表名(字段1)values('$content')")
        if(mysql_affected_rows($sql)>0)
    {
    echo "<script>alert('恭喜您,添加成功!');location.href='#';</script>";
    }
    else
    {
    echo "<script>alert('不恭喜您,添加失败!');location.href='message.php';</script>";
    }
    ?>不知道这样可行否
      

  2.   


    error_reporting(E_ALL ^ E_NOTICE);set_time_limit(0);
    $htmldir = "./zhtml";
    @mkdir($htmldir);
    @chmod($htmldir,777);
    $httpdir = "http://www.jjxww.com/html/";
    $url = "{$httpdir}list.aspx?cid=207";
    $html = file_get_contents($url);
    $html = formathtml($html);
    preg_match_all('~<a href="(show\.aspx\?.*?)"[^>]*?>(.*?)</a>~is',$html,$data);
    $result=array();
    foreach($data[1] as $key=>$item){
    $result[$key]["href"]=$item;
    }
    foreach($data[2] as $key=>$item){
    $result[$key]["title"]=$item;
    }
    //获取完整标题,本想在上一个preg_match_all里把标题给提取出来,却怎么提都是空。
    foreach($data[0] as $key=>$rt){
    preg_match_all('~title="(.*?)"~is',$rt,$tdata);
    if($tdata[1][0]!="")$result[$key]["title"]=$tdata[1][0];
    }
    foreach($result as $key=>$query){
    $nurl=$httpdir.$query['href'];
    $html = file_get_contents($nurl);
    $html = formathtml($html);
    preg_match_all('~<hr/><div id="BodyLabel" class="content" style="display:block;padding:0px 10px">(.*?)</div><br/><br/><table width="97%" cellpadding="0" cellspacing="0" style="clear:both"><tr><td align="right"><table><tr><td>责任编辑~is',$html,$data);
    $result[$key]["content"]=$data[1][0];
    //写入数据库
    $title = $result[$key]["title"];
    $content = $result[$key]["content"];
    /*
    写入数据库
    */
    //这里用写入文件测试
    writeover($htmldir.'/'.$title.".txt",$content);
    //break;
    }function formathtml($html){
    $html = preg_replace('/>(\s+)/','>',$html);
    $html = preg_replace('/(\s+)</','<',$html);
    $html = preg_replace('/(\s+)/',' ',$html);
    $html = str_replace("\r\n",'',$html);
    $html = str_replace("\t",'',$html);
    return $html;
    }function writeover($filename,$data,$method="rb+",$iflock=1,$check=1,$chmod=1){
    touch($filename);
    $handle = fopen($filename,$method);
    $iflock && flock($handle,LOCK_EX);
    fwrite($handle,$data);
    $method=="rb+" && ftruncate($handle,strlen($data));
    fclose($handle);
    $chmod && @chmod($filename,0777);
    }