如何使用php删除所有表单元素及其内容。我应该在preg_replace中插入什么模式;
HTML字符串:
<p>Hey I am a boy</p>
<form id='id1' class='class1'>Content</form>
<p>Hey I am a girl</p>
<form id='id1' class='class1'>content</form>
Preg_replace应返回字符串:
<p>I am a boy</p>
<p>Hey I am a girl</p>
我希望从返回字符串中删除所有表单元素
答案 0 :(得分:3)
使用下面的正则表达式,然后用空字符串替换匹配。
(?s)(^|\n)?<form\b.*?<\/form>
<强>解释强>
(?s) set flags for this block (with . matching
\n) (case-sensitive) (with ^ and $
matching normally) (matching whitespace
and # normally)
( group and capture to \1 (optional):
^ the beginning of the string
| OR
\n '\n' (newline)
)? end of \1 (NOTE: because you are using a
quantifier on this capture, only the LAST
repetition of the captured pattern will be
stored in \1)
<form '<form'
\b the boundary between a word char (\w) and
something that is not a word char
.*? any character (0 or more times)
< '<'
\/ '/'
form> 'form>'
答案 1 :(得分:1)
答案 2 :(得分:0)
您可以使用:
preg_replace('#<form*>*</form>#isU','',$str);
答案 3 :(得分:0)
您可以使用strip_tag删除所有代码,但将<p>
作为第二个参数传递,仅返回该代码。
echo strip_tags($text, '<p>');
如果不起作用,请使用:
echo preg_replace('/<form[^>]*>([\s\S]*?)<\/form[^>]*>/', '', '$text);