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

这篇文章主要介绍了PHP实现通过正则表达式替换回调的内容标签的方法,涉及php正则匹配与替换的相关技巧,需要的朋友可以参考下

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

  1. function my_wp_plugin_tag_action($content,$tag,$function,$args = FALSE) {
  2. // match all regular expressions
  3. preg_match_all($tag,$content,$matches);
  4. if (count($matches)>0) {
  5. // filter duplicates
  6. $matches = array_unique($matches);
  7. // loop through
  8. $tag_results = array();
  9. $found_tags = array();
  10. foreach ($matches as $idx => $match) {
  11. //build arg array
  12. $full_tag = array_shift($match);
  13. //call function, adding function output and full tag text to replacement array
  14. $tag_results[] = my_wp_plugin_buffer_func($function,$match);
  15. $found_tags[] = $full_tag;
  16. }
  17. // replace all tags with corresponding text
  18. $content = str_replace($found_tags,$tag_results,$content);
  19. }
  20. return $content;
  21. }