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

yipeiwu_com6年前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 来访国内外IP判断代码并实现页面跳转

我大概构思了一下,有两个方案: 1. Javascript判断来访者的浏览器语言,如果是中文系统,自然使用者都是中国人,跳中文网站; 如果是非中文系统,默认使用者非中国人,跳英文网站。...

深入解析PHP的Yii框架中的缓存功能

数据缓存是指将一些 PHP 变量存储到缓存中,使用时再从缓存中取回。它也是更高级缓存特性的基础,例如查询缓存和内容缓存。 如下代码是一个典型的数据缓存使用模式。其中 $cache 指向缓...

PHP实现过滤掉非汉字字符只保留中文字符

<?php $str = "a 1b 2b<中文>。xxyy字符"; //转换 GB2312 -> UTF-8 $str = mb_convert_encod...

php实现两个数组相加的方法

本文实例讲述了php实现两个数组相加的方法。分享给大家供大家参考。具体如下: 实例1: <?php $arr1 = array("a"=>"朝阳区","b"=&g...

PHP三元运算符的结合性介绍

先看一个三元运算式子: 复制代码 代码如下: <?php $a=1;$b=2;$c=3;$d=4; echo $a<$b?'xx':$a<$c?'yy':$a<$...