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];
}
您可能感興趣的文章:
- 開(kāi)啟CURL擴(kuò)展,讓服務(wù)器支持PHP curl函數(shù)(遠(yuǎn)程采集)
- 基于PHP的cURL快速入門(mén)教程 (小偷采集程序)
- 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采集類(lèi)使用實(shí)例代碼
- PHP采集類(lèi)snoopy詳細(xì)介紹(snoopy使用教程)
- 利用PHP命令行模式采集股票趨勢(shì)信息
相關(guān)文章
php實(shí)現(xiàn)的XML操作(讀取)封裝類(lèi)完整實(shí)例
這篇文章主要介紹了php實(shí)現(xiàn)的XML操作(讀取)封裝類(lèi),給出了xml格式文件示例,并結(jié)合完整實(shí)例形式分析了php遍歷讀取xml格式數(shù)據(jù)節(jié)點(diǎn)的相關(guān)操作技巧,需要的朋友可以參考下2017-02-02
PHP添加PNG圖片背景透明水印操作類(lèi)定義與用法示例
這篇文章主要介紹了PHP添加PNG圖片背景透明水印操作類(lèi)定義與用法,涉及php操作圖片的顯示、保存、壓縮、水印添加等相關(guān)操作技巧,需要的朋友可以參考下2019-03-03
PHP封裝的遠(yuǎn)程抓取網(wǎng)站圖片并保存功能類(lèi)
這篇文章主要介紹了PHP封裝的遠(yuǎn)程抓取網(wǎng)站圖片并保存功能類(lèi),結(jié)合實(shí)例形式分析了php抓取遠(yuǎn)程圖片封裝類(lèi)的定義與簡(jiǎn)單使用方法,涉及php正則匹配與文件讀寫(xiě)相關(guān)操作技巧,需要的朋友可以參考下2018-06-06
mac下Apache + MySql + PHP搭建網(wǎng)站開(kāi)發(fā)環(huán)境
最近接了個(gè)小活,做一個(gè)使用PHP語(yǔ)言和MySql數(shù)據(jù)庫(kù)的動(dòng)態(tài)網(wǎng)站。之前做過(guò)類(lèi)型的網(wǎng)站,是在windows系統(tǒng)下做的,開(kāi)發(fā)環(huán)境使用的是 AppServ 的PHP開(kāi)發(fā)套件?,F(xiàn)在有了我的大MAC,所以找了MAC系統(tǒng)下PHP環(huán)境的開(kāi)發(fā)套件。2014-06-06
thinkphp5.0自定義驗(yàn)證規(guī)則使用方法
本文主要講了thinkphp5.0版本中自定義驗(yàn)證規(guī)則的使用方法和一些注意事項(xiàng)。2017-11-11
PHP 配置open_basedir 讓各虛擬站點(diǎn)獨(dú)立運(yùn)行
好幾年前,我在抱怨Apache運(yùn)行PHP的安全性不行,只要一個(gè)站點(diǎn)被人拿下,服務(wù)器上的其他站點(diǎn)就會(huì)跟著遭殃。2009-11-11
一個(gè)簡(jiǎn)單php擴(kuò)展介紹與開(kāi)發(fā)教程
這個(gè)擴(kuò)展早就寫(xiě)好了,只是一直沒(méi)有時(shí)間寫(xiě)在blog上面,今天抽點(diǎn)時(shí)間,將它記錄下來(lái),以后備用。2010-08-08
解決PHP 7編譯安裝錯(cuò)誤:cannot stat ‘phar.phar’: No such file or direc
這篇文章主要給大家介紹了關(guān)于解決在PHP 7編譯安裝遇到的錯(cuò)誤錯(cuò)誤:cannot stat ‘phar.phar’: No such file or directory問(wèn)題的相關(guān)資料,文中給出詳細(xì)的解決方法,需要的朋友可以參考借鑒。2017-02-02

