1.php如下:
<? 
$link="/c/2003-07-09/1457349093s.shtml";
if (($result=@file("http://news.sina.com.cn/china/index.shtml","r"))==null) 

echo "新闻读取失败!"; 

else 

$result_string=@join(" ", $result); 
//截取新闻正文
$tq=stristr($result_string,"<li>"); 
$tq1=strpos($tq,"<br><br>"); 
$tq2=substr($tq,0,$tq1); 
$tq2=str_replace("</a>","</a><br>",$tq2); 
$tq2=str_replace("<li>"," ",$tq2); 
$tq2=str_replace("http://news.sina.com.cn"," ",$tq2);
$tq2=str_replace("/c/","2.php?link=/c/",$tq2);
echo "<p align=\"left\">$tq2</p> "; 
 } 
?> 
2.php如下:
<?php
if (($result=@file("http://news.sina.com.cn".$link."","r"))==null) 

echo "新闻读取失败!"; 

else 

$result_string=@join(" ", $result); 
//截取新闻正文
$tq=stristr($result_string,"<table width=560 border=0"); 
$tq1=strpos($tq,"<font class=f14>"); 
$tq2=substr($tq,0,$tq1); 
//$tq2=str_replace("<br>"," ",$tq2); 
$tq2=str_replace("http://news.sina.com.cn","2.php?link=",$tq2); 
$tq2=str_replace("<!--zlx/chinaunicom/7.27/B-->"," ",$tq2);
$tq2=str_replace("画中画广告开始-->"," ",$tq2); 
$tq2=str_replace("<!--画中画广告结束"," ",$tq2); 
$tq2=str_replace("<center><b><a href=2.php?link=/photo/>点击此处查看其它图片</a></b></center>"," ",$tq2); 
$tq2=str_replace("<!--zlx/chinaunicom/7.27/B-->"," ",$tq2);
$tq2=str_replace("http://www.sina.com.cn","<font color=#ff0000><a href=http://www.443100.com>宜昌信息港</a></font>",$tq2); 
echo "<center>$tq2</center>"; 
echo  "相关链接:<br>";
//截取相关链接
$xglink=stristr($result_string,"<td class=title14  bgcolor=#EDF0F5>"); 
$xglink1=strpos($xglink,"</td></tr>"); 
$xglink2=substr($xglink,0,$xglink1); 
$xglink2=str_replace("<td class=title14  bgcolor=#EDF0F5>"," ",$xglink2); 
$xglink2=str_replace("http://news.sina.com.cn","2.php?link=",$xglink2); 
//$xglink2=str_replace(".shtml","",$xglink2); 
echo "<p align=\"left\">$xglink2</p>"; } 
?>

解决方案 »

  1.   

    你看看吧,中英文字符串截取
    function subcnstr($str, $len, $padstr="...")
    {
    $ret="";
    if ( strlen($str)<=$len )
    { return $str;
    }
    for($i=0; $i<$len; $i++)
    { $ret.=$str[$i];
    if (ord($str[$i])>128)
    { $i++;
    $ret.=$str[$i];
    }
    }
    $ret.=$padstr; return $ret;
    }
      

  2.   

    另可用iconv()函数将gb2312->utf8取偶数个字符
      

  3.   

    1.php
    echo "<p align=\"left\">$tq2</p> "; 
    改为
    $ar = split("<a",$tq2);
    echo $ar[0];
    for($i=1;$i<count($ar);$i++)
      echo "<a".$ar[$i];
    // count($ar) 是连接部分的数量。根据需要控制输出3.php
    echo "<p align=\"left\">$xglink2</p>"; 
    改为
    echo "<p align=\"left\">preg_replace("/\(\d日\d:\d\)/U","",$xglink2)</p>"; 
      

  4.   

    xuzuning(唠叨) ,谢谢你,第一个问题解决了,同时又提出一个新问题,那如何控制输出得字符字数呢?后面用...替代?第二个问题还是没有解决.错误提示如下:
    Warning: Unexpected character in input: '\' (ASCII=92) state=1 in c:\inetpub\wwwroot\3.php on line 48Warning: Unexpected character in input: '\' (ASCII=92) state=1 in c:\inetpub\wwwroot\3.php on line 48Warning: Unexpected character in input: '\' (ASCII=92) state=1 in c:\inetpub\wwwroot\3.php on line 48Parse error: parse error, unexpected T_STRING in c:\inetpub\wwwroot\3.php on line 48
      

  5.   

    哦,写错了。不好意思
    第二个问题
    echo "<p align=\"left\">". preg_replace("/\(\d日\d:\d\)/U","",$xglink2) ."</p>"; 那如何控制输出得字符字数呢?后面用...替代?
    一般的写法是:
    $len = 20; // 假定待输出的字符数
    echo substr($str,0,$len).chr(0)."...");
    不过对你的具体问题有点麻烦
    先取出文字
    $s = preg_replace("/.*>([^<]*)<.*/","\\1",$ar[$i]);
    再截断,替换回去
    if(strlen($s) > $len) {
      $t = substr($s,0,$len).chr(0)."...";
      $ar[$i] = str_replace($s,$t,$ar[$i]);
    }
    然后输出
    echo $ar[$i];
      

  6.   

    第2个问题我修改成如下代码了:
    <? 
    $link="/c/2003-07-09/1457349093s.shtml";
    if (($result=@file("http://www.yc.chinanews.com.cn/index.html","r"))==null) 

    echo "新闻读取失败!"; 

    else 

    $result_string=@join(" ", $result); 
    //截取新闻正文
    $tq=stristr($result_string,"□"); 
    $tq1=strpos($tq,"·</font>"); 
    $tq2=substr($tq,0,$tq1); 
    $tq3=stristr($result_string,"("); 
    $tq4=strpos($tq3,")"); 
    $tq5=substr($tq3,0,$tq4); 
    $tq2=str_replace("$tq5"," ",$tq2); 
    $tq2=str_replace("□"," ",$tq2); 
    $tq2=str_replace("href=\"","href=\"4.php?link=",$tq2);
    echo "<p align=\"left\">". preg_replace("/\(\d日\d:\d\)/U","",$tq2) ."</p>"; 

    ?> 
    但是,没有出现任何变化!后面得日期这些依旧存在!
      

  7.   

    字数控制这个我修改成如下:
    <? 
    $link="/c/2003-07-09/1457349093s.shtml";
    if (($result=@file("http://news.sina.com.cn/china/index.shtml","r"))==null) 

    echo "新闻读取失败!"; 

    else 

    $result_string=@join(" ", $result); 
    //截取新闻正文
    $tq=stristr($result_string,"<li>"); 
    $tq1=strpos($tq,"<br><br>"); 
    $tq2=substr($tq,0,$tq1); 
    $tq2=str_replace("</a>","</a><br>",$tq2); 
    $tq2=str_replace("<li>"," ",$tq2); 
    $tq2=str_replace("http://news.sina.com.cn"," ",$tq2);
    $tq2=str_replace("/c/","2.php?link=/c/",$tq2);
    $ar = split("<a",$tq2);
    for($i=1;$i<11;$i++)
    {
    $len = 8; // 假定待输出的字符数
    $s = preg_replace("/.*>([^<]*)<.*/","\\1",$ar[$i]);
    if(strlen($s) > $len) {
      $t = substr($s,0,$len).chr(0)."...";
      $ar[$i] = str_replace($s,$t,$ar[$i]);
    }
    echo "<a".$ar[$i];
    }
     } 
    ?> 显示正常,同样没有发现有任何变化!不知道为什么?
    有劳你了,谢谢大哥!
      

  8.   

    本人发明的最简代码
    <?php
    $handle = fopen("http://news.sina.com.cn/news1000/","r");
    $sign = 0;
    while(!feof($handle))
    {
    $message = fgets($handle,40960);
    if(ereg("<li>",$message))
    {
    echo $message;
    }
    }
    fclose($handle);?>
    :)
      

  9.   

    杀猪的,你的代码不错,按照你的我修改了一下,可以控制新闻条数了,但是还是不能控制字数,不能隐藏后面的日期,代码如下!
    <?php
    $handle = fopen("http://www.yc.chinanews.com.cn/","r");
    $sign = 0;
    while(!feof($handle))
    {
    $message = fgets($handle,40960); if(ereg("□",$message))
    {
    $tq=stristr($message,"<font color=\"#003399\">"); 
    $tq1=strpos($tq,"</td></tr>"); 
    $tq2=substr($tq,0,$tq1); 
    $tq2=str_replace("□"," ",$tq2);
    $ar = split("<a",$tq2);
    for($i=1;$i<11;$i++)
    {
    echo "<a".$ar[$i];
    }
    }
    }
    fclose($handle);
    ?>
      

  10.   

    这个问题应该很简单啊,你问小乐zerg不就完了吗???呵呵
      

  11.   

    在大家的帮助下问题终于解决了,不过很繁琐,还是要谢谢大家的,我是个php的初学者,望大家以后多多帮助!
      

  12.   

    我贴出我的代码,看看那个达人能够帮忙改进指点指点.
    <?php
    $handle = fopen("http://www.yc.chinanews.com.cn/","r");
    $sign = 0;
    while(!feof($handle))
    {
    $message = fgets($handle,40960); if(ereg("□",$message))
    {
    $tq=stristr($message,"<font color=\"#003399\">"); 
    $tq1=strpos($tq,"</td></tr>"); 
    $tq2=substr($tq,0,$tq1); 
    $tq2=str_replace("□"," ",$tq2);
    $ar = split("<a",$tq2);
    for($i=1;$i<11;$i++)
    {
    //echo "<br>$i<a".$ar[$i];
    $ar[$i]=str_replace("</a>","</a><br>",$ar[$i]);
    $leyan=stristr($ar[$i],"href"); 
    $leyan1=strpos($ar[$i],"<br>"); 
    $leyan2=substr($leyan,0,$leyan1);
    echo "<a ".$leyan2;
    echo "br>";
    }
    }
    }
    fclose($handle);
    ?>