묻고답하기
page_full_width" class="col-xs-12" |cond="$__Context->page_full_width">
[php]신문기사 긁어오기에서 날짜와 시간도 긁어오고 싶은데요.
2004.09.17 06:39
php팁에서 신문기사긁어오기 소스를 찾았는데...
날짜와 시간은 없더라구요...
날짜와 시간을 긁어오려면 어떻게 구현해야 되는지좀.....
<?
function get($url){//문서가져옮
$fp = fopen($url, "r");
while(!feof($fp)){
$tmp = fgets($fp, 900);
$temp .= $tmp;
}
fclose($fp);
return $temp;
}//function get
function Strip($all){// 링크랑 제목만 빼오기
$link_pm=strpos($all,">");
$link=substr($all,0,$link_pm);
if(!$link) return;
if(ereg(""",$link)){
$link_p=strpos($link,""");
$link=substr($link,$link_p+1);
$link_p=strpos($link,""");
$link=substr($link,0,$link_p);
}
if(ereg("'",$link)){
$link_p=strpos($link,"'");
$link=substr($link,$link_p+1);
$link_p=strpos($link,"'");
$link=substr($link,0,$link_p);
}
$link=trim($link);
$substr=substr($all,$link_pm+1);
$substr_p=strpos($substr,"</a>");
$substr=substr($substr,0,$substr_p);
$strip=strip_tags($substr);
$strip=trim($strip);
return array(link => $link, sub => $strip);
}//function strip($all)
function makeUrl($Url, $Link){
if(!strpos($Link,"http://") && substr_count("$Link","http://")) return $Link;
$eURL = pathinfo("$Url");
$host = parse_url($Url);
$host = $host[scheme]."://".$host[host];
$Link = "/".$Link; //링크가 루트인지 확인위해 임의로 붙인다.
if(substr_count("$Link","../")){ //한칸앞으로
$Link = str_replace("./../", "", $Link);
$Link = str_replace("../", "", $Link);
$pos = strrpos($eURL["dirname"], "/");
$makeUrl = substr($eURL["dirname"],0,$pos);
$makeUrl = "$makeUrl"."$Link";
}else if(substr_count("$Link","//") && !strpos($Link, "//")){ //루트로 가라
$makeUrl = $host."/".str_replace("//", "", $Link);
}else{ //제자리
$Link = str_replace("./", "", $Link);
$makeUrl = $eURL["dirname"]."$Link";
}
return $makeUrl;
}
////////////////////////////////////// 펑션끝
$date_Y = date("Y");
$date_M = date("m");
$date_D = date("d");
$mdate = "$date_Y.$date_M.$date_D"; //확장성 위해
////////////////////////////////////////////////////////////<== 신문사별설정 추가가능
$newspaper[] = "동아일보";
$URL[] = "http://www.donga.com/fbin/output?f=total";
$like[] = "$date_Y$date_M$date_D";
$newspaper[] = "연합신문";
$URL[] = "http://www.yonhapnews.co.kr/services/0400000000.html";
$like[] = "$date_Y$date_M$date_D";
////////////////////////////////////////////////////////////<== 여기까지 신문사별설정
for($x=0; $x < sizeof($newspaper); $x++){
$temp = get($URL[$x]);
$all = split("href =|HREF =|href=|HREF=",$temp);//--> url 추출
for($i=0; $i < sizeof($all); $i++){
$kisa = Strip($all[$i]);
if(ereg($like[$x], $kisa[link]) & 20 < strlen($kisa[sub])){
$LinkURL = makeUrl($URL[$x], $kisa[link]);
echo "<a href='$LinkURL'>[$newspaper[$x]]$kisa[sub]</a><br>";
}//if(ereg($like[$x], $kisa[link]) & 20 < strlen($kisa[sub]))
}//for($i=0; $i < sizeof($all); $i++)
}//for($x=0; $x < sizeof($newspaper); $x++)
?>
날짜와 시간은 없더라구요...
날짜와 시간을 긁어오려면 어떻게 구현해야 되는지좀.....
<?
function get($url){//문서가져옮
$fp = fopen($url, "r");
while(!feof($fp)){
$tmp = fgets($fp, 900);
$temp .= $tmp;
}
fclose($fp);
return $temp;
}//function get
function Strip($all){// 링크랑 제목만 빼오기
$link_pm=strpos($all,">");
$link=substr($all,0,$link_pm);
if(!$link) return;
if(ereg(""",$link)){
$link_p=strpos($link,""");
$link=substr($link,$link_p+1);
$link_p=strpos($link,""");
$link=substr($link,0,$link_p);
}
if(ereg("'",$link)){
$link_p=strpos($link,"'");
$link=substr($link,$link_p+1);
$link_p=strpos($link,"'");
$link=substr($link,0,$link_p);
}
$link=trim($link);
$substr=substr($all,$link_pm+1);
$substr_p=strpos($substr,"</a>");
$substr=substr($substr,0,$substr_p);
$strip=strip_tags($substr);
$strip=trim($strip);
return array(link => $link, sub => $strip);
}//function strip($all)
function makeUrl($Url, $Link){
if(!strpos($Link,"http://") && substr_count("$Link","http://")) return $Link;
$eURL = pathinfo("$Url");
$host = parse_url($Url);
$host = $host[scheme]."://".$host[host];
$Link = "/".$Link; //링크가 루트인지 확인위해 임의로 붙인다.
if(substr_count("$Link","../")){ //한칸앞으로
$Link = str_replace("./../", "", $Link);
$Link = str_replace("../", "", $Link);
$pos = strrpos($eURL["dirname"], "/");
$makeUrl = substr($eURL["dirname"],0,$pos);
$makeUrl = "$makeUrl"."$Link";
}else if(substr_count("$Link","//") && !strpos($Link, "//")){ //루트로 가라
$makeUrl = $host."/".str_replace("//", "", $Link);
}else{ //제자리
$Link = str_replace("./", "", $Link);
$makeUrl = $eURL["dirname"]."$Link";
}
return $makeUrl;
}
////////////////////////////////////// 펑션끝
$date_Y = date("Y");
$date_M = date("m");
$date_D = date("d");
$mdate = "$date_Y.$date_M.$date_D"; //확장성 위해
////////////////////////////////////////////////////////////<== 신문사별설정 추가가능
$newspaper[] = "동아일보";
$URL[] = "http://www.donga.com/fbin/output?f=total";
$like[] = "$date_Y$date_M$date_D";
$newspaper[] = "연합신문";
$URL[] = "http://www.yonhapnews.co.kr/services/0400000000.html";
$like[] = "$date_Y$date_M$date_D";
////////////////////////////////////////////////////////////<== 여기까지 신문사별설정
for($x=0; $x < sizeof($newspaper); $x++){
$temp = get($URL[$x]);
$all = split("href =|HREF =|href=|HREF=",$temp);//--> url 추출
for($i=0; $i < sizeof($all); $i++){
$kisa = Strip($all[$i]);
if(ereg($like[$x], $kisa[link]) & 20 < strlen($kisa[sub])){
$LinkURL = makeUrl($URL[$x], $kisa[link]);
echo "<a href='$LinkURL'>[$newspaper[$x]]$kisa[sub]</a><br>";
}//if(ereg($like[$x], $kisa[link]) & 20 < strlen($kisa[sub]))
}//for($i=0; $i < sizeof($all); $i++)
}//for($x=0; $x < sizeof($newspaper); $x++)
?>