通过regex用回调替换内容标记

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过regex用回调替换内容标记相关的知识,希望对你有一定的参考价值。

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

以上是关于通过regex用回调替换内容标记的主要内容,如果未能解决你的问题,请参考以下文章

正则表达式替换为c ++ 11中的回调?

我可以用啥来替换嵌套的异步回调?

C# Regex 用 <p> 替换一些 HTML 序列,用 </p> 替换一些

如何自定义设置 angularjs jsonp 回调名称?

当片段被替换并且再次可见时回调

标记完成加载后是不是有回调?