欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2024-01-11 10:30:28
...
  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. }
复制代码