php专区

 首页 > php专区 > PHP应用 > php函数大全 > PHP采集程序常用的采集函数收藏 - php函数

PHP采集程序常用的采集函数收藏 - php函数

分享到:
【字体:大 中 小】
导读:
          这几天关注了一下PHP的采集程序,才发现用PHP采集内容是这么方便,把经常用到的采集函数在这里总结一下,方便以后使用 在php采集页面中最常用...

PHP采集程序常用的采集函数收藏

这几天关注了一下PHP的采集程序,才发现用PHP采集内容是这么方便,把经常用到的采集函数在这里总结一下,方便以后使用.

在php采集页面中最常用的就是过滤一些特殊字符或把内容中的图片也采集保存下来,下面我来给大家介绍我在写php采集程序时一些常用的函数.

  1. 获取所有链接内容和地址 
  2. function getAllURL($code){ 
  3. preg_match_all('/]?([^>"' ]+)["|']?s*[^>]*>([^>]+)/i',$code,$arr); 
  4. return array('name'=>$arr[2],'url'=>$arr[1]); 
  5. } 
  6. 获取所有的图片地址 
  7. function getImgSrc($code){ 
  8. $reg = "/]*src="(http://(.+)/(.+).(jpg|gif|bmp|bnp|png))"/isU"; 
  9. preg_match_all($reg, $code, $img_array, PREG_PATTERN_ORDER); 
  10. return $img_array[1]; 
  11. } 
  12. 当前的脚本网址 
  13. function getSelfURL(){ 
  14. if(!emptyempty($_SERVER["REQUEST_URI"])){ 
  15. $scriptName = $_SERVER["REQUEST_URI"]; 
  16. $nowurl = $scriptName; 
  17. }else{ 
  18. $scriptName = $_SERVER["PHP_SELF"]; 
  19. if(emptyempty($_SERVER["QUERY_STRING"])) $nowurl = $scriptName; 
  20. else $nowurl = $scriptName."?".$_SERVER["QUERY_STRING"]; 
  21. } 
  22. return $nowurl; 
  23. } 
  24. 把全角数字转为半角数字 
  25. function getAlabNum($fnum){ 
  26. $nums = array("0","1","2","3","4","5","6","7","8","9"); 
  27. $fnums = "0123456789"; 
  28. for($i=0;$i<=9;$i++) $fnum = str_replace($nums[$i],$fnums[$i],$fnum); 
  29. $fnum = ereg_replace("[^0-9.]|^0{1,}","",$fnum); 
  30. if($fnum=="") $fnum=0; 
  31. return $fnum; 
  32. } 
  33. 去除HTML标记 
  34. function text2Html($txt){ 
  35. $txt = str_replace(" "," ",$txt); 
  36. $txt = str_replace("<","<",$txt); 
  37. $txt = str_replace(">",">",$txt); 
  38. $txt = preg_replace("/[rn]{1,}/isU","
    rn"
    ,$txt); 
  39. return $txt; 
  40. } 
  41. 清除HTML标记 
  42. function clearHtml($str){ 
  43. $str = str_replace('<','<',$str); 
  44. $str = str_replace('>','>',$str); 
  45. return $str; 
  46. } 
  47. 相对路径转化成绝对路径 
  48. function relative2Absolute($content, $feed_url) { 
  49. preg_match('/(http|https|ftp):///', $feed_url, $protocol); 
  50. $server_url = preg_replace("/(http|https|ftp|news):///", "", $feed_url); 
  51. $server_url = preg_replace("//.*/", "", $server_url); 
  52. if ($server_url == '') { 
  53. return $content; 
  54. } 
  55. if (isset($protocol[0])) { 
  56. $new_content = preg_replace('/href="//', 'href="'.$protocol[0].$server_url.'/', $content); 
  57. $new_content = preg_replace('/src="//', 'src="'.$protocol[0].$server_url.'/', $new_content); 
  58. } else { 
  59. $new_content = $content; 
  60. } 
  61. return $new_content; 
  62. } 
  63. 获取指定标记中的内容 
  64. function getTagData($str, $start, $end){ 
  65. if ( $start == '' || $end == '' ){ 
  66. return; 
  67. } 
  68. $str = explode($start, $str); 
  69. $str = explode($end, $str[1]); 
  70. return $str[0]; 
  71. } 
  72. HTML表格的每行转为CSV格式数组 
  73. function getTrArray($table) { 
  74. $table = preg_replace("']*?>'si",'"',$table); 
  75. $table = str_replace("",'",',$table); 
  76. $table = str_replace("","{tr}",$table); 
  77. //去掉 HTML 标记 
  78. $table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table); 
  79. //去掉空白字符 
  80. $table = preg_replace("'([rn])[s]+'","",$table); 
  81. $table = str_replace(" ","",$table); 
  82. $table = str_replace(" ","",$table); 
  83. $table = explode(",{tr}",$table); 
  84. array_pop($table); 
  85. return $table; 
  86. } 
  87. 将HTML表格的每行每列转为数组,采集表格数据 
  88. function getTdArray($table) { 
  89. $table = preg_replace("']*?>'si","",$table); 
  90. $table = preg_replace("']*?>'si","",$table); 
  91. $table = preg_replace("']*?>'si","",$table); 
  92. $table = str_replace("","{tr}",$table); 
  93. $table = str_replace("","{td}",$table); 
  94. //去掉 HTML 标记 
  95. $table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table); 
  96. //去掉空白字符 
  97. $table = preg_replace("'([rn])[s]+'","",$table); 
  98. $table = str_replace(" ","",$table); 
  99. $table = str_replace(" ","",$table); 
  100. $table = explode('{tr}', $table); 
  101. array_pop($table); 
  102. foreach ($table as $key=>$tr) { 
  103. $td = explode('{td}', $tr); 
  104. array_pop($td); 
  105. $td_array[] = $td; 
  106. } 
  107. return $td_array; 
  108. } 
  109. 返回字符串中的所有单词 $distinct=true 去除重复 
  110. function splitEnStr($str,$distinct=true) { 
  111. preg_match_all('/([a-zA-Z]+)/',$str,$match); 
  112. if ($distinct == true) { 
  113. $match[1] = array_unique($match[1]); 
  114. } 
  115. sort($match[1]); 
  116. return $match[1]; 
  117. } 
分享到:
php生成随机颜色代码实例 - php函数
php生成随机颜色代码实例 function randrgb()    {      $str=&#039;0123456789ABCDEF&#039;;        $estr=&#039;#&#039;;        $len=strlen($str);        for($i=1;$i
substr(),mb_substr()及mb_strcut函数用...
substr(),mb_substr()及mb_strcut函数用法与区别 在php中substr(),mb_substr()及mb_strcut三个函数都是字符截取函数,但是substr截取中文时会有乱码,而后两者支持中文截取,下面我来介绍介绍。 substr()函数 substr(string,start,length) string表示要截取的对象,start表示从哪个位置开始截...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……