<?php
//RSS源地址列表數組
$rssfeed = array("http://www.blog-dragon.com/feed",
"http://rss.sina.com.cn/news/allnews/sports.xml",
"http://ent.163.com/special/00031K7Q/rss_toutiao.xml",
"http://tech.163.com/special/00091JPQ/techimportant.xml");
//設置編碼為UTF-8
header('Content-Type:text/html;charset= UTF-8');
for($i=0;$i<sizeof($rssfeed);$i++){//分解開始
$buff = "";
$rss_str="";
//打開rss地址,並讀取,讀取失敗則中止
$fp = fopen($rssfeed[$i],"r") or die("can not open $rssfeed");
while ( !feof($fp) ) {
$buff .= fgets($fp,4096);
}
//關閉文件打開
fclose($fp);
//建立一個 XML 解析器
$parser = xml_parser_create();
//xml_parser_set_option -- 為指定 XML 解析進行選項設置
xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,1);
//xml_parse_into_struct -- 將 XML 數據解析到數組$values中
xml_parse_into_struct($parser,$buff,$values,$idx);
//xml_parser_free -- 釋放指定的 XML 解析器
xml_parser_free($parser);
foreach ($values as $val) {
$tag = $val["tag"];
$type = $val["type"];
$value = $val["value"];
//標籤統一轉為小寫
$tag = strtolower($tag);
if ($tag == "item" && $type == "open"){
$is_item = 1;
}else if ($tag == "item" && $type == "close") {
//構造輸出字符串
$rss_str .= "<a href='".$link."' target=_blank>".$title."</a><br />";
$is_item = 0;
}
//僅讀取item標籤中的內容
if($is_item==1){
if ($tag == "title") {$title = $value;}
if ($tag == "link") {$link = $value;}
}
}
//輸出結果
echo $rss_str."<br />";
}
?>
2011年1月25日 星期二
[PHP]讀取多筆 RSS feed
來源網站
2011年1月4日 星期二
[PHP] 上傳檔案過濾檔名特殊字元
安全性考量 :
function clean_file_name($filename)
{
$bad = array(
"<!--",
"-->",
"'",
"<",
">",
'"',
'&',
'$',
'=',
';',
'?',
'/',
"%20",
"%22",
"%3c", // <
"%253c", // <
"%3e", // >
"%0e", // >
"%28", // (
"%29", // )
"%2528", // (
"%26", // &
"%24", // $
"%3f", // ?
"%3b", // ;
"%3d" // =
);
$filename = str_replace($bad, '', $filename);
return stripslashes($filename);
}
2010年11月17日 星期三
[php] get_week 得到每週的日期.
簡單的函數, 用來指定一個日期, 直到停止日期的所有每週的日期:
result :
print_r (get_all_week('2010-06-06','2010-11-05'));
Array
(
[0] => 2010-11-05
[1] => 2010-10-29
[2] => 2010-10-22
[3] => 2010-10-15
[4] => 2010-10-08
[5] => 2010-10-01
[6] => 2010-09-24
[7] => 2010-09-17
[8] => 2010-09-10
[9] => 2010-09-03
[10] => 2010-08-27
[11] => 2010-08-20
[12] => 2010-08-13
[13] => 2010-08-06
[14] => 2010-07-30
[15] => 2010-07-23
[16] => 2010-07-16
[17] => 2010-07-09
[18] => 2010-07-02
[19] => 2010-06-25
[20] => 2010-06-18
[21] => 2010-06-11
[22] => 2010-06-04
function get_all_week ($stopDate , $startDate ){
$allWeek[] = $startDate;
if ($startDate < $stopDate ){
return false;
}
while (1){
$prevWeek = strtotime($startDate)- (7 * 24 * 60 * 60);
$get = date('Y-m-d', $prevWeek);
if ($get <= $stopDate ) {
$allWeek[] = $get;
break;
}
$allWeek[] = $startDate = $get;
}
return $allWeek;
}
result :
print_r (get_all_week('2010-06-06','2010-11-05'));
Array
(
[0] => 2010-11-05
[1] => 2010-10-29
[2] => 2010-10-22
[3] => 2010-10-15
[4] => 2010-10-08
[5] => 2010-10-01
[6] => 2010-09-24
[7] => 2010-09-17
[8] => 2010-09-10
[9] => 2010-09-03
[10] => 2010-08-27
[11] => 2010-08-20
[12] => 2010-08-13
[13] => 2010-08-06
[14] => 2010-07-30
[15] => 2010-07-23
[16] => 2010-07-16
[17] => 2010-07-09
[18] => 2010-07-02
[19] => 2010-06-25
[20] => 2010-06-18
[21] => 2010-06-11
[22] => 2010-06-04
2009年11月30日 星期一
[php]解決 big 輸出 許功蓋 跑出很多\\\\\\\\ 問題.
資料原本輸出為 '許功\\\\蓋 '
使用此function 後 變成 '許功蓋';
出處:竹貓星球
使用此function 後 變成 '許功蓋';
出處:竹貓星球
function Fix_Backslash($org_str) {
//if ( mysql_client_encoding() != "big5" ) return mysql_client_encoding();
$tmp_length = strlen($org_str);
for ( $tmp_i=0; $tmp_i<$tmp_length; $tmp_i++ ) {
$ascii_str_a = substr($org_str, $tmp_i , 1);
$ascii_str_b = substr($org_str, $tmp_i+1, 1);
$ascii_value_a = ord($ascii_str_a);
$ascii_value_b = ord($ascii_str_b);
if ( $ascii_value_a > 128 ) {
if ( $ascii_value_b == 92 ) {
$org_str = substr($org_str, 0, $tmp_i+2) . substr($org_str,$tmp_i+3);
$tmp_length = strlen($org_str);
}
$tmp_i++;
}
}
$tmp_length = strlen($org_str);
if ( substr($org_str, ($tmp_length-1), 1) == "\\" ) $org_str .= chr(32);
$org_str = str_replace("\\\0", "\ 0", $org_str);
return $org_str;
}
訂閱:
文章 (Atom)