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>"; }
?>
<?
$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>"; }
?>
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;
}
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>";
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
第二个问题
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];
<?
$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>";
}
?>
但是,没有出现任何变化!后面得日期这些依旧存在!
<?
$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];
}
}
?> 显示正常,同样没有发现有任何变化!不知道为什么?
有劳你了,谢谢大哥!
<?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);?>
:)
<?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);
?>
<?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);
?>