duannai1883 2014-04-16 22:02
浏览 182
已采纳

PHP正则表达式替换wordpress短代码标签

I have a shortcode which I want to be able to strip away depending on the context of the post. Eg.

[tooltip slug="test"]Test Text[/tooltip]

I would like the output to be:

<span class="dummy">Test Text</span>

I have experimented (a lot!) with preg_replace and I can't seem to get it to recognize that the replacement string is between the ']' and then delimited by '[/tooltip]' without doing multiple passes.

Ideas?

Update: As so often happens, about 10 seconds after I wrote this one of my attempts seemed to work. I don't think it's as good as the solution below but FWIW...

$my_var .= preg_replace('/(?:\[tooltip slug=\"([^\"]*)"[^\>]*\]([^\<]*)\[\/tooltip\])/', '<span class="dummy">\\2</span>', $my_post->post_content);
  • 写回答

2条回答 默认 最新

  • dtdr57046 2014-04-16 22:58
    关注

    Here is the simple regex you are looking for.

    $result = preg_replace('%\[tooltip slug="[^"]*"]([^[]*)\[/tooltip]%',
              '<span class="dummy">\1</span>', $subject);
    

    What we do here is capture the text between the tooltip tags, and insert it in the replacement. Let me know if you need any details.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程