`
phantom
  • 浏览: 164788 次
社区版块
存档分类
最新评论

php 正则表达式的是一个使用范例 统计链接生成与替换

阅读更多
<?php

class acmeCache{

 function fetch($name, $refreshSeconds = 0){

  if(!$GLOBALS['cache_active']) return false;
  if(!$refreshSeconds) $refreshSeconds = 60;
  $cacheFile = acmeCache::cachePath($name);
  if(file_exists($cacheFile) and
   ((time()-filemtime($cacheFile))< $refreshSeconds))
   $cacheContent = file_get_contents($cacheFile);
  return $cacheContent;
 }

 function save($name, $cacheContent){
  if(!$GLOBALS['cache_active']) return;
  $cacheFile = acmeCache::cachePath($name);
  acmeCache::savetofile($cacheFile, $cacheContent);
 }

 // for internal use
 // ====================
 function cachePath($name){
  $cacheFolder = $GLOBALS['cache_folder'];
  if(!$cacheFolder) $cacheFolder = trim($_SERVER['DOCUMENT_ROOT'],'/').'/cache/';
  return $cacheFolder . md5(strtolower(trim($name))) . '.cache';
 }

 function savetofile($filename, $data){
  $dir = trim(dirname($filename),'/').'/';
  acmeCache::forceDirectory($dir);
  $file = fopen($filename, 'w');
  fwrite($file, $data); fclose($file);
 }

 function forceDirectory($dir){ // force directory structure
  return is_dir($dir) or (acmeCache::forceDirectory(dirname($dir)) and mkdir($dir, 0777));
 }

}
function repHtml($data){
//$pattern = "|·<a href=(.*) target=_blank>(.*)</a>|Us"; //表格内的行
  $pattern = "|<a href=\"http://(.*)\"|Us"; //表格内的行
//  preg_match_all($pattern,$data,$matches);
//
  $data=preg_replace($pattern, '<a href="ad.php?p=$1"',$data);

//  foreach($matches[1] as $key =>$value)
//{
////echo $value."\n";
//}
  return $data;
}
$cache_active = true;
$cache_folder = 'cache/';
$key="zhuanti/share/intr.shtml";
 $result = acmeCache::fetch($key, 300+rand()%1); // 2-4分钟
  {
  	$data = file_get_contents ("http://www.jinghua.cn/zhuanti/share/intr.shtml");
     $result =  repHtml($data);
     acmeCache::save($key, $result);
  }

  echo $result;



?>

 

分享到:
评论

相关推荐

    精通正则表达式~~~

    精通正则表达式第三版 搜集于网络 前言..........I 第1章:正则表达式入门.... 1 解决实际问题... 2 作为编程语言的正则表达式... 4 以文件名做类比... 4 以语言做类比... 5 正则表达式的知识框架... 6 对于...

    PHP5范例代码查询辞典(英文)

    - **正则表达式**:PHP支持使用正则表达式进行字符串匹配和搜索替换。 #### 文件与目录处理 - **文件操作**:包括文件的打开(`fopen()`)、读取(`fread()`)、写入(`fwrite()`)、关闭(`fclose()`)等操作。 - **目录...

    php范例代码查询辞典

    2. **字符串处理**:如何进行字符串的拼接、查找替换、分割合并等操作,以及使用正则表达式进行复杂匹配。 3. **数组操作**:数组的创建、遍历、排序、合并、键值操作等,以及关联数组和多维数组的应用。 4. **...

    PHP基础教程 是一个比较有价值的PHP新手教程!

    与Java和Perl不同,你不必把头埋进100多页的文档中努力学习才可以写出一个象样的程序。只要了解一些基本的语法和语言特色,你就可以开始你的PHP编码之旅了。之后你在编码过程中如果遇到了什么麻烦,还可以再去翻阅...

    万用小偷 v2.0.rar

    3、新增正则表达式替换功能,并提供相关范例,方便新手快速入门【此为高级功能,仅对授权用户提供技术咨询】; 4、内核程序及其架构优化,减少服务器资源占用,减少了程序执行失败的几率; 5、优化升级流程,方便...

    magento入门文档

    这里创建了一个名为`hello.world`的Block对象,使用的是`helloworld/index`类型的Block,并指定了模板文件`helloworld.phtml`。 **3.6 block和reference的区别** `block`标签用于实例化一个新的Block对象,而`...

    vscode 教程

    - 搜索和替换:强大的搜索功能,支持正则表达式; - 版本控制:与 Git 等版本控制系统集成。 #### 基础 VSCode 的基础知识包括: - 安装过程:下载安装包后,按照向导提示进行安装; - 扩展市场:访问扩展市场可以...

Global site tag (gtag.js) - Google Analytics