【问题标题】:Process entire page with regex before sending back to client在发送回客户端之前使用正则表达式处理整个页面
【发布时间】:2012-02-07 22:16:41
【问题描述】:

是否可以在发送到客户端之前使用正则表达式处理整个 wordpress 页面(我的页面是指对站点中任何请求的 url 的响应)html?这是否最好通过服务器而不是 php 来实现,以及如何在 apache 中完成?

【问题讨论】:

  • 你想改变/替换一些东西吗?
  • 我想用 span 和 class 包装 html 中的所有 & 符号,以避免在客户端对 javascript 做同样的事情......不会认为这值得一票否决跨度>

标签: php regex apache wordpress


【解决方案1】:

ob_start()开头开始输出缓冲,最后以$content = ob_get_contents(); ob_end_clean();获取内容。然后,您可以使用 preg_replace 对您的内容运行正则表达式,然后只需 echo $content;

示例

ob_start();

echo "Hello World!";

$content = ob_get_contents();
ob_end_clean();

// Outputs "Hello StackOverflow!"
echo preg_replace("/World/", "StackOverflow", $content);

【讨论】:

    猜你喜欢
    • 2010-09-21
    • 2011-08-13
    • 2011-04-17
    • 2011-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-26
    • 1970-01-01
    相关资源
    最近更新 更多