从 HTML 文档中删除表单标签及其内容

问题描述 投票:0回答:5
php html replace html-parsing sanitization
5个回答
2
投票

试试这个

ob_start(); // start output buffer
include $file;
$template = ob_get_contents(); // get contents of buffer
$template = preg_replace('/<form.*?class="FormTest".*?>.*?<\/form>/',
'', $template );
ob_end_clean();

只是

ob_end_clean();
应该在最后

您也可以这样做

ob_get_clean()
本质上同时执行
ob_get_contents()
ob_end_clean()

ob_start(); // start output buffer
include $file;
$template = ob_get_clean();
$template = preg_replace('/<form.*?class="FormTest".*?>.*?<\/form>/','', $template );
echo $template ; 

0
投票

您可能需要在正则表达式表达式中使用带有 /m 的多行修饰符:

$template = preg_replace('/<form.*?class="FormTest".*?>.*?<\/form>/m', '', $template );

0
投票

尝试将 preg_replace 代码更改为以下内容:

$template = preg_replace('/<form.*?class="FormTest.*?form>/s','', $template );

将 HTML 作为字符串放入 $template 中时,它对我有用。


0
投票

你的点还需要匹配换行符,所以添加 s-修饰符

$template = preg_replace('/<form.*?class="FormTest".*?>.*?<\/form>/s', '', $template );

-1
投票

以下代码应该可以工作

$template = 'this is just a test<form class="FormTest">form will be removed</form>...';
$template = preg_replace('/<form.*?class="FormTest".*?>.*?<\/form>/', '', $template);

echo $template;
© www.soinside.com 2019 - 2024. All rights reserved.