dtxs9017 2014-09-28 15:21
浏览 80
已采纳

如何使用preg_replace反向引用而不是preg_match和preg_replace?

I am trying to use shortcode function on content after the body tag.

preg_match("/<body[^>]*>(.*?)<\/body>/is", $html, $matches);
$html= preg_replace("/<body[^>]*>(.*?)<\/body>/is",run_shortcodes($matches[1]), $body);
echo $html;

And this works ok but I would like to do this with preg_replace backreference if possible.

I have tried something like this but obviously it is wrong

$html= preg_replace("/<body[^>]*>(.*?)<\/body>/is",run_shortcodes('$1'), $body);

Can someone please show a possible example of using backreference.

Any help is appreciated .

  • 写回答

2条回答 默认 最新

  • dongshuming7131 2014-09-29 17:19
    关注

    Here's an example:

    preg_match("/<body[^>]*>(.*?)<\/body>/is", $html, $matches);
    $newBody = run_shortcodes($matches[1]);
    echo preg_replace('/(.*<body[^>]*>)(.*)(<\/body>.*)/is', '${1}'.$newBody.'${3}', $html);
    

    You can also try:

    $html = preg_replace_callback("/<body[^>]*>(.*?)<\/body>/is", 'run_shortcodes', $subject);
    echo $html;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题
  • ¥15 python点云生成mesh精度不够怎么办
  • ¥15 QT C++ 鼠标键盘通信
  • ¥15 改进Yolov8时添加的注意力模块在task.py里检测不到
  • ¥50 高维数据处理方法求指导
  • ¥100 数字取证课程 关于FAT文件系统的操作
  • ¥15 如何使用js实现打印时每页设置统一的标题
  • ¥15 安装TIA PortalV15.1报错