在发送回客户端之前,使用regex处理整个页面


Process entire page with regex before sending back to client

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

ob_start()开始输出缓冲,最后用$content = ob_get_contents(); ob_end_clean();获取内容。然后,您可以使用preg_replace在内容上运行regex,然后仅使用echo $content;

示例

ob_start();
echo "Hello World!";
$content = ob_get_contents();
ob_end_clean();
// Outputs "Hello StackOverflow!"
echo preg_replace("/World/", "StackOverflow", $content);