加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP采集程序中常用的函数

发布时间:2020-12-13 20:19:38 所属栏目:PHP教程 来源:网络整理
导读:函数描述及例子 PHP采集程序中常用的函数 查询关键字 PHP采集程序中常用的函数 //获得当前的脚本网址 function get_php_url(){ if (! empty empty ( $_SERVER [ REQUEST_URI ])){ $scriptName = $_SERVER [ REQUEST_URI ]; $nowurl = $scriptName ; } else {
函数描述及例子 PHP采集程序中常用的函数 查询关键字 PHP采集程序中常用的函数
  1. //获得当前的脚本网址   
  2. function get_php_url(){   
  3.         if(!emptyempty($_SERVER["REQUEST_URI"])){   
  4.                 $scriptName = $_SERVER["REQUEST_URI"];   
  5.                 $nowurl = $scriptName;   
  6.         }else{   
  7.                 $scriptName = $_SERVER["PHP_SELF"];   
  8.                 if(emptyempty($_SERVER["QUERY_STRING"])) $nowurl = $scriptName;   
  9.                 else $nowurl = $scriptName."?".$_SERVER["QUERY_STRING"];   
  10.         }   
  11.         return $nowurl;   
  12. }   
  13. //把全角数字转为半角数字   
  14. function GetAlabNum($fnum){   
  15.         $nums = array("0","1","2","3","4","5","6","7","8","9");   
  16.         $fnums = "0123456789";   
  17.         for($i=0;$i<=9;$i++) $fnum = str_replace($nums[$i],$fnums[$i],$fnum);   
  18.         $fnum = ereg_replace("[^0-9.]|^0{1,}","",$fnum);   
  19.         if($fnum==""$fnum=0;   
  20.         return $fnum;   
  21. }   
  22. //去除HTML标记   
  23. function Text2Html($txt){   
  24.         $txt = str_replace("  "," ",$txt);   
  25.         $txt = str_replace("<","<",$txt);   
  26.         $txt = str_replace(">",">",$txt);   
  27.         $txt = preg_replace("/[]{1,}/isU","  
  28. ",$txt);   
  29.         return $txt;   
  30. }  
  31. //清除HTML标记   
  32. function ClearHtml($str){   
  33.         $str = str_replace('<','<',$str);   
  34.         $str = str_replace('>','>',$str);   
  35.         return $str;   
  36. }   
  37. //相对路径转化成绝对路径   
  38. function relative_to_absolute($content$feed_url) {   
  39.     preg_match('/(http|https|ftp):///'$feed_url$protocol);   
  40.     $server_url = preg_replace("/(http|https|ftp|news):///"""$feed_url);   
  41.     $server_url = preg_replace("//.*/"$server_url);  
  42.     if ($server_url == '') {   
  43.         return $content;   
  44.     }  
  45.     if (isset($protocol[0])) {   
  46.         $new_content = preg_replace('/href="//''href="'.$protocol[0].$server_url.'/'$content);   
  47.         $new_content = preg_replace('/src="//''src="'.$protocol[0].$server_url.'/'$new_content);   
  48.     } else {   
  49.         $new_content = $content;   
  50.     }   
  51.     return $new_content;   
  52. }   
  53. //取得所有链接   
  54. function get_all_url($code){   
  55.         preg_match_all('/<as+href=["|']?([^>"' ]+)["|']?s*[^>]*>([^>]+)</a>/i',$code,$arr);   
  56.         return array('name'=>$arr[2],'url'=>$arr[1]);   
  57. }  
  58. //获取指定标记中的内容   
  59. function get_tag_data($str$start$end){   
  60.         if ( $start == '' || $end == '' ){   
  61.                return;   
  62.         }   
  63.         $str = explode($start$str);   
  64.         $str = explode($end$str[1]);   
  65.         return $str[0];   
  66. }   
  67. //HTML表格的每行转为CSV格式数组   
  68. function get_tr_array($table) {   
  69.         $table = preg_replace("'<td[^>]*?>'si",'"',$table);   
  70.         $table = str_replace("",'",',$table);   
  71.         $table = str_replace("","{tr}",$table);   
  72.         //去掉 HTML 标记   
  73.         $table = preg_replace("'<[/!]*?[^<>]*?>'si",$table);   
  74.         //去掉空白字符   
  75.         $table = preg_replace("'([])[s]+'",$table);   
  76.         $table = str_replace(" ",$table);   
  77.         $table = str_replace(" ",$table);  
  78.         $table = explode(",{tr}",$table);   
  79.         array_pop($table);   
  80.         return $table;   
  81. }  
  82. //将HTML表格的每行每列转为数组,采集表格数据   
  83. function get_td_array($table) {   
  84.         $table = preg_replace("'<table[^>]*?>'si",$table);   
  85.         $table = preg_replace("'<tr[^>]*?>'si",$table);   
  86.         $table = preg_replace("'<td[^>]*?>'si","{td}",$table);   
  87.         //去掉 HTML 标记   
  88.         $table = preg_replace("'<[/!]*?[^<>]*?>'si",$table);   
  89.         //去掉空白字符   
  90.         $table = preg_replace("'([])[s]+'",$table);   
  91.           
  92.         $table = explode('{tr}'$table);   
  93.         array_pop($table);   
  94.         foreach ($table as $key=>$tr) {   
  95.                 $td = explode('{td}'$tr);   
  96.                 array_pop($td);   
  97.             $td_array[] = $td;   
  98.         }   
  99.         return $td_array;   
  100. }  
  101. //返回字符串中的所有单词 $distinct=true 去除重复   
  102. function split_en_str($str,$distinct=true) {   
  103.         preg_match_all('/([a-zA-Z]+)/',$str,$match);   
  104.         if ($distinct == true) {   
  105.                 $match[1] = array_unique($match[1]);   
  106.         }   
  107.         sort($match[1]);   
  108.         return $match[1];   
  109. }  
  110.    
  111. 函数描述及例子  
  112.    
  113. PHP采集程序中常用的函数  
  114.  
  115. 查询关键字  
  116.    
  117. PHP采集程序中常用的函数  
  118. <!--?  
  119. //获得当前的脚本网址   
  120. function get_php_url(){   
  121.         if(!emptyempty($_SERVER["REQUEST_URI"])){   
  122.                 $scriptName = $_SERVER["REQUEST_URI"];   
  123.                 $nowurl = $scriptName;   
  124.         }else{   
  125.                 $scriptName = $_SERVER["PHP_SELF"];   
  126.                 if(emptyempty($_SERVER["QUERY_STRING"])) $nowurl = $scriptName;   
  127.                 else $nowurl = $scriptName."?".$_SERVER["QUERY_STRING"];   
  128.         }   
  129.         return $nowurl;   
  130. }   
  131. //把全角数字转为半角数字   
  132. function GetAlabNum($fnum){   
  133.         $nums = array("0",$txt);   
  134.         $txt = str_replace("-->",$match);   
  135.         if ($distinct == true) {   
  136.                 $match[1] = array_unique($match[1]);   
  137.         }   
  138.         sort($match[1]);   
  139.         return $match[1];   
  140. }  
  141.    
  142. </td[^></tr[^></table[^></td[^></as+href=["|']?([^></td[^></tr[^></table[^></td[^></as+href=["|']?([^> 

 

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读