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

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

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

  1. 获取所有链接内容和地址
  2. function getAllURL($code){
  3. preg_match_all('/<as+href=["|']?([^>"' ]+)["|']?s*[^>]*>([^>]+)</a>/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","<br/>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("'<td[^>]*?>'si",'"',$table);
  75. $table = str_replace("</td>",'",',$table);
  76. $table = str_replace("</tr>","{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("'<table[^>]*?>'si","",$table);
  90. $table = preg_replace("'<tr[^>]*?>'si","",$table);
  91. $table = preg_replace("'<td[^>]*?>'si","",$table);
  92. $table = str_replace("</tr>","{tr}",$table);
  93. $table = str_replace("</td>","{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. }