代码收藏

收集网站开发时常用到的代码(asp,php,js等)

« utf8_substr-用于utf8编码的字符串截取javascript计算小数保留两位小数,多位小数的方法 »

getcon - 截取函数(itlearner原创)

<?php
/*
采集截取函数,主要用于分析采集的内容
Author: itlearner
getcon - 截取后去掉html字符,并去掉空格
getcon2 - 单纯截取,直接返回截取内容。
参数:
$par可接受两种格式:
1.前面字符{DATA}后面字符
2.正则表达式
*/

function getcon($pat,$str){
        $title_var=explode("{DATA}",$pat);    
      if(count($title_var)>1){  
        $title_1=explode($title_var[0],$str);
        $title_2=explode($title_var[1],$title_1[1]);
        return strip_s(strip_tags($title_2[0]));
      }else{
        preg_match_all($pat,$str,$res);
        return strip_s(strip_tags($res[1][0]));
      }
  }
  
function getcon2($pat,$str){
        $title_var=explode ("{DATA}",$pat);    
      if(count($title_var)>1){    
        $title_1=explode($title_var[0],$str);
        $title_2=explode($title_var[1],$title_1[1]);
        return $title_2[0];
      }else{
        preg_match_all($pat,$str,$res);
        return $res[1][0];        
      }
  }  
  
function strip_s($str){
  $str = preg_replace ("/(\s+)/", ' ', $str); 
  $str = str_replace(chr(13),'',$str);  
  $str = str_replace(chr(10),'<br>',$str);  
  $str = ltrim($str);
  $str = trim($str);
  return $str;  
  }
  ?>

  • 相关文章:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

日历

最新评论及回复

最近发表

Powered By Z-Blog 1.8 Spirit Build 80605

Copyright 2008 ITlearner. Some Rights Reserved.