您的位置主页 > 编程专区 > Php > php采集的函数

php采集的函数

2009-10-08    文章来源:互联网    浏览次数:254
 
  1. <?php
  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("<","&lt;",$txt); 
  26.     $txt = str_replace(">","&gt;",$txt); 
  27.     $txt = preg_replace("/[\r\n]{1,}/isU","<br/>\r\n",$txt);
  28.     return $txt
  29.  
  30. //清除HTML标记 
  31. function ClearHtml($str){ 
  32.     $str = str_replace('<','&lt;',$str); 
  33.     $str = str_replace('>','&gt;',$str); 
  34.     return $str
  35.  
  36. function relative_to_absolute($content$feed_url) { 
  37.     preg_match('/(http|https|ftp):\/\//'$feed_url$protocol); 
  38.     $server_url = preg_replace("/(http|https|ftp|news):\/\//"""$feed_url); 
  39.     $server_url = preg_replace("/\/.*/"""$server_url); 
  40.  
  41.     if ($server_url == '') { 
  42.         return $content
  43.     } 
  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('/<a\s+href=["|\']?([^>"\' ]+)["|\']?\s*[^>]*>([^>]+)<\/a>/i',$code,$arr); 
  56.     return array('name'=>$arr[2],'url'=>$arr[1]); 
  57. }
  58.  
  59. //获取指定标记中的内容 
  60. function get_tag_data($str$start$end){ 
  61.     if ( $start == '' || $end == '' ){ 
  62.         return
  63.     } 
  64.     $str = explode($start$str); 
  65.     $str = explode($end$str[1]); 
  66.     return $str[0]; 
  67. }
  68.  
  69. //HTML表格的每行转为CSV格式数组 
  70. function get_tr_array($table) { 
  71.     $table = preg_replace("'<td[^>]*?>'si",'"',$table);
  72.     $table = str_replace("</td>",'",',$table); 
  73.     $table = str_replace("</tr>","{tr}",$table); 
  74.     //去掉 HTML 标记 
  75.     $table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table); 
  76.     //去掉空白字符 
  77.     $table = preg_replace("'([ ])[s]+'","",$table); 
  78.     $table = str_replace(" ","",$table); 
  79.     $table = str_replace(" ","",$table); 
  80.     $table = explode(",{tr}",$table); 
  81.     array_pop($table); 
  82.     return $table
  83. }
  84.  
  85. //将HTML表格的每行每列转为数组,采集表格数据 
  86. function get_td_array($table) { 
  87.     $table = preg_replace("'<table[^>]*?>'si","",$table); 
  88.     $table = preg_replace("'<tr[^>]*?>'si","",$table); 
  89.     $table = preg_replace("'<td[^>]*?>'si","",$table); 
  90.     $table = str_replace("</tr>","{tr}",$table); 
  91.     $table = str_replace("</td>","{td}",$table); 
  92.     //去掉 HTML 标记 
  93.     $table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table); 
  94.     //去掉空白字符 
  95.     $table = preg_replace("'([ ])[s]+'","",$table); 
  96.     $table = str_replace(" ","",$table); 
  97.     $table = str_replace(" ","",$table); 
  98.     $table = explode('{tr}'$table); 
  99.     array_pop($table); 
  100.     foreach ($table as $key=>$tr) { 
  101.         $td = explode('{td}'$tr); 
  102.         array_pop($td); 
  103.         $td_array[] = $td
  104.     } 
  105.     return $td_array
  106. }
  107.  
  108. //返回字符串中的所有单词 $distinct=true 去除重复 
  109. function split_en_str($str,$distinct=true) { 
  110.     preg_match_all('/([a-zA-Z]+)/',$str,$match); 
  111.     if ($distinct == true) { 
  112.         $match[1] = array_unique($match[1]); 
  113.     } 
  114.     sort($match[1]); 
  115.     return $match[1]; 
  116. }
  117. ?>

 原文地址:http://www.itlearner.com/article/2007/3726.shtml

  • 上一篇:li中的图片和文字不对齐div+css,解决方法
  • 下一篇:没有了
  • 文章评论(查看全部)

    验证码: 中评 好评 差评