PHP实现通过正则表达式替换回调的内容标签

yipeiwu_com5年前PHP代码库

本文实例讲述了PHP实现通过正则表达式替换回调的内容标签。分享给大家供大家参考。具体实现方法如下:

function my_wp_plugin_tag_action($content,$tag,$function,$args = FALSE) {
 // match all regular expressions
 preg_match_all($tag,$content,$matches);
 if (count($matches)>0) {
  // filter duplicates
  $matches = array_unique($matches);
  // loop through
  $tag_results = array();
  $found_tags = array();
  foreach ($matches as $idx => $match) {
   //build arg array
   $full_tag = array_shift($match);
   //call function, adding function output and full tag text to replacement array
   $tag_results[] = my_wp_plugin_buffer_func($function,$match);
   $found_tags[] = $full_tag;
  }
  // replace all tags with corresponding text
  $content = str_replace($found_tags,$tag_results,$content);
 }
 return $content;
}

希望本文所述对大家的php程序设计有所帮助。

相关文章

php引用和拷贝的区别知识点总结

对于值传递和引用传递,书本上的解释比较繁琐,而php面试中总会出现,下面我会通过一个生活的例子带大家理解它们之间区别。 第一步 假设我们去酒店订房间,我们把酒店的门牌号比作变量名,我们把...

功能强大的php分页函数

分页是每一个程序需要去理解的东西,学习过的几门语言中我发现分页原理都是一样的,下面为php初学者分析一下php分页实现与最后面补充了一个超级强大的分页函数。 文章内容分页主要有两个办法:...

PHP实现的简易版图片相似度比较

由于相似图片搜索的php实现的 API 不怎么符合我的用途,所以我重新定义 API 的架构,改写成比较简单的函数方式,虽然还是用对象的方式包装。 复制代码 代码如下: <?...

PHP下用rmdir实现删除目录的三种方法小结

1、递规法:利用递归一层一层的删。 复制代码 代码如下:deleteDir($dir)   {   if (rmdir($di...

php计算函数执行时间的方法

本文实例讲述了php计算函数执行时间的方法。分享给大家供大家参考。具体如下: 我们可以通过在程序的前后分别记录开始和结束时间,两个时间差就是程序的执行时间。 <?ph...