如何使用 PHP 编辑 HTML 页面中的 div 和元标记



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

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

update_html.php这里,我如何打开pages/remote_page.html并仅写入/更新div#container和元标记中的内容?

首先,在 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);

最新更新