PHP 采集程序中常用的函數(shù)
更新時(shí)間:2009年12月09日 19:05:21 作者:
現(xiàn)在不少的php采集程序,用了下面的函數(shù),需要的朋友可以參考下。
復(fù)制代碼 代碼如下:
//獲得當(dāng)前的腳本網(wǎng)址
function get_php_url()
{
if(!empty($_SERVER[”REQUEST_URI”]))
{
$scriptName = $_SERVER[”REQUEST_URI”];
$nowurl = $scriptName;
}
else
{
$scriptName = $_SERVER[”P(pán)HP_SELF”];
if(empty($_SERVER[”QUERY_STRING”]))
$nowurl = $scriptName;
else
$nowurl = $scriptName.”?”.$_SERVER[”QUERY_STRING”];
}
return $nowurl;
}
//把全角數(shù)字轉(zhuǎn)為半角數(shù)字
function GetAlabNum($fnum)
{
$nums = array(”0”,”1”,”2”,”3”,”4”,”5”,”6”,”7”,”8”,”9”);
$fnums = “0123456789″;
for($i=0;$i<=9;$i++) $fnum = str_replace($nums[$i],$fnums[$i],$fnum);
$fnum = ereg_replace(”[^0-9\.]|^0{1,}”,””,$fnum);
if($fnum==””) $fnum=0;
return $fnum;
}
//去除HTML標(biāo)記
function Text2Html($txt)
{
$txt = str_replace(” “,” ”,$txt);
$txt = str_replace(”<”,”<”,$txt);
$txt = str_replace(”>”,”>”,$txt);
$txt = preg_replace(”/[\r\n]{1,}/isU”,”<br/>\r\n”,$txt);
return $txt;
}
//清除HTML標(biāo)記
function ClearHtml($str)
{
$str = str_replace('<','<',$str);
$str = str_replace('>','>',$str);
return $str;
}
//相對(duì)路徑轉(zhuǎn)化成絕對(duì)路徑
function relative_to_absolute($content, $feed_url)
{
preg_match('/(http|https|ftp):\/\//', $feed_url, $protocol);
$server_url = preg_replace(”/(http|https|ftp|news):\/\//”, “”, $feed_url);
$server_url = preg_replace(”/\/.*/”, “”, $server_url);
if ($server_url == ”)
{
return $content;
}
if (isset($protocol[0]))
{
$new_content = preg_replace('/href=”\//', ‘href=”‘.$protocol[0].$server_url.'/', $content);
$new_content = preg_replace('/src=”\//', 'src=”‘.$protocol[0].$server_url.'/', $new_content);
}
else
{
$new_content = $content;
}
return $new_content;
}
//取得所有鏈接
function get_all_url($code){
preg_match_all('/<a\s+href=[”|\']?([^>”\' ]+)[”|\']?\s*[^>]*>([^>]+)<\/a>/i',$code,$arr);
return array('name'=>$arr[2],'url'=>$arr[1]);
}
//獲取指定標(biāo)記中的內(nèi)容
function get_tag_data($str, $start, $end)
{
if ( $start == ” || $end == ” )
{
return;
}
$str = explode($start, $str);
$str = explode($end, $str[1]);
return $str[0];
}
//HTML表格的每行轉(zhuǎn)為CSV格式數(shù)組
function get_tr_array($table)
{
$table = preg_replace(”‘<td[^>]*?>'si”,'”‘,$table);
$table = str_replace(”</td>”,'”,',$table);
$table = str_replace(”</tr>”,”{tr}”,$table);
//去掉 HTML 標(biāo)記
$table = preg_replace(”‘<[\/\!]*?[^<>]*?>'si”,””,$table);
//去掉空白字符
$table = preg_replace(”‘([\r\n])[\s]+'”,””,$table);
$table = str_replace(” “,””,$table);
$table = str_replace(” “,””,$table);
$table = explode(”,{tr}”,$table);
array_pop($table);
return $table;
}
//將HTML表格的每行每列轉(zhuǎn)為數(shù)組,采集表格數(shù)據(jù)
function get_td_array($table)
{
$table = preg_replace(”‘<table[^>]*?>'si”,””,$table);
$table = preg_replace(”‘<tr[^>]*?>'si”,””,$table);
$table = preg_replace(”‘<td[^>]*?>'si”,””,$table);
$table = str_replace(”</tr>”,”{tr}”,$table);
$table = str_replace(”</td>”,”{td}”,$table);
//去掉 HTML 標(biāo)記
$table = preg_replace(”‘<[\/\!]*?[^<>]*?>'si”,””,$table);
//去掉空白字符
$table = preg_replace(”‘([\r\n])[\s]+'”,””,$table);
$table = str_replace(” “,””,$table);
$table = str_replace(” “,””,$table);
$table = explode('{tr}', $table);
array_pop($table);
foreach ($table as $key=>$tr)
{
$td = explode('{td}', $tr);
array_pop($td);
$td_array[] = $td;
}
return $td_array;
}
//返回字符串中的所有單詞 $distinct=true 去除重復(fù)
function split_en_str($str,$distinct=true)
{
preg_match_all('/([a-zA-Z]+)/',$str,$match);
if ($distinct == true)
{
$match[1] = array_unique($match[1]);
}
sort($match[1]);
return $match[1];
}
相關(guān)文章
PHP中配置IIS7實(shí)現(xiàn)基本身份驗(yàn)證的方法
本文介紹的是在PHP中IIS7實(shí)現(xiàn)基本身份驗(yàn)證的方法,希望對(duì)你有幫助,一起來(lái)看。2015-09-09PHP對(duì)接抖音開(kāi)發(fā)平臺(tái)接口的詳細(xì)教程
本文主要介紹了PHP對(duì)接抖音開(kāi)發(fā)平臺(tái)接口的詳細(xì)教程,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11php md5下16位和32位的實(shí)現(xiàn)代碼
PHP里MD5加密的16位和32位實(shí)現(xiàn)代碼,在網(wǎng)上一搜也有不少人有這方面的困惑,后來(lái)找到一個(gè)解決辦法,是正確的,就記錄下來(lái)2008-04-04PHP+Ajax實(shí)現(xiàn)的無(wú)刷新分頁(yè)功能詳解【附demo源碼下載】
這篇文章主要介紹了PHP+Ajax實(shí)現(xiàn)的無(wú)刷新分頁(yè)功能,結(jié)合具體實(shí)例形式分析了php+ajax通過(guò)pdo操作類讀取數(shù)據(jù)庫(kù)并分頁(yè)顯示的相關(guān)實(shí)現(xiàn)技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-07-07在smarty中調(diào)用php內(nèi)置函數(shù)的方法
在smarty中調(diào)用php的內(nèi)置函數(shù),相信很多人都不是很很了解smarty的一個(gè)重要功能,就是能在smarty模板里面調(diào)用php的內(nèi)置函數(shù)2013-02-02PHP 函數(shù)call_user_func和call_user_func_array用法詳解
下面來(lái)和大家分享一下這個(gè)call_user_func_array和call_user_func函數(shù)的用法,另外附贈(zèng)func_get_args()函數(shù)和func_num_args()函數(shù),嘿嘿!!2014-03-03php事務(wù)回滾簡(jiǎn)單實(shí)現(xiàn)方法示例
這篇文章主要介紹了php事務(wù)回滾簡(jiǎn)單實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了php事務(wù)的定義、提交及回滾事務(wù)的具體操作技巧,需要的朋友可以參考下2017-03-03