如何使用 php 在 html 页面中编辑 div 和元标记

how to edit a div and meta tags in a html page using php

我这里有个情况。我只需要使用 php 在远程 html 页面中编辑元标记和 <div id="container"></div> 的内容。

过程是,当用户从edit.php编辑远程html页面时,加载远程页面的div#container(编辑由html5的内容完成可编辑属性)并点击保存按钮,编辑页面发送 html 到服务器页面,比如 update_html.php.

update_html.php这里,我怎样才能打开pages/remote_page.html和write/update只有div#container和meta标签中的内容?

首先,在您的 html 文件中添加一些标记,例如 <!-- container begin --><!-- container end -->

其次,使用 preg_replace 将它们更改为您的内容。

例如

$info = file_get_contents('pages/remote_page.html');
preg_replace('|(<!\-\- container begin \-\->.*<!\-\- container end \-\->)|isU', $_POST['content'], $info);
file_put_contents('pages/remote_page.html', $info);