PHP 采集程序 常用函數(shù)
更新時(shí)間:2008年12月18日 14:21:29 作者:
php中喜歡他的采集功能的朋友,就不的不參考下面的函數(shù)了,他們就是php采集程序中,常用的一些函數(shù)收集
當(dāng)前的腳本網(wǎng)址
function get_php_url(){
if(!empty($_SERVER["REQUEST_URI"])){
$scriptName = $_SERVER["REQUEST_URI"];
$nowurl = $scriptName;
}else{
$scriptName = $_SERVER["PHP_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];
}
function get_php_url(){
if(!empty($_SERVER["REQUEST_URI"])){
$scriptName = $_SERVER["REQUEST_URI"];
$nowurl = $scriptName;
}else{
$scriptName = $_SERVER["PHP_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];
}
您可能感興趣的文章:
- 開啟CURL擴(kuò)展,讓服務(wù)器支持PHP curl函數(shù)(遠(yuǎn)程采集)
- 基于PHP的cURL快速入門教程 (小偷采集程序)
- PHP采集利器 Snoopy 試用心得
- php file_get_contents函數(shù)輕松采集html數(shù)據(jù)
- 基于PHP的簡(jiǎn)單采集數(shù)據(jù)入庫(kù)程序
- PHP實(shí)現(xiàn)采集抓取淘寶網(wǎng)單個(gè)商品信息
- PHP 采集獲取指定網(wǎng)址的內(nèi)容
- snoopy 強(qiáng)大的PHP采集類使用實(shí)例代碼
- PHP采集類snoopy詳細(xì)介紹(snoopy使用教程)
- 利用PHP命令行模式采集股票趨勢(shì)信息
相關(guān)文章
數(shù)據(jù)庫(kù)查詢記錄php 多行多列顯示
數(shù)據(jù)庫(kù)查詢記錄多行多列顯示,其實(shí)是用php生成符合table標(biāo)準(zhǔn)格式的代碼,大家只要對(duì)table熟悉下,或?qū)崿F(xiàn)畫好,再生成出代碼,即可。2009-08-08PHP zlib擴(kuò)展實(shí)現(xiàn)頁(yè)面GZIP壓縮輸出
GZIP(GNU-ZIP)是一種壓縮技術(shù)。經(jīng)過GZIP壓縮后頁(yè)面大小可以變?yōu)樵瓉?lái)的30%甚至更小。這樣用戶瀏覽的時(shí)候就會(huì)感覺很爽很愉快!2010-06-06PHP擴(kuò)展mcrypt實(shí)現(xiàn)的AES加密功能示例
這篇文章主要介紹了PHP擴(kuò)展mcrypt實(shí)現(xiàn)的AES加密功能,結(jié)合實(shí)例形式分析了php基于mcrypt實(shí)現(xiàn)AES加密的相關(guān)操作技巧,需要的朋友可以參考下2019-01-01用PHP中的 == 運(yùn)算符進(jìn)行字符串比較
用PHP中的 == 運(yùn)算符進(jìn)行字符串比較...2006-11-11WordPress中Gravatar頭像緩存到本地及相關(guān)優(yōu)化的技巧
這篇文章主要介紹了WordPress中Gravatar頭像緩存到本地及優(yōu)化的技巧,需要的朋友可以參考下2015-12-12