Link to home
Create AccountLog in
Avatar of dnotestine
dnotestine

asked on

PHP using preg_replace to remove form tags and everything in between

Using PHP I want to remove a form from a string that contains html - Here is the php code I have been trying to use but it doesn't work. Any ideas?
$html = "<form ... </form>"

$pattern = "/<form(.*?)<\/form>/i";
$html = preg_replace($pattern,"",$html);

Open in new window

Avatar of codeflux
codeflux
Flag of United States of America image

You're missing a semicolon.

<?php
$html = "before <form blah blah blah </form> after";
$pattern = "/<form(.*?)<\/form>/i";
$html = preg_replace($pattern,"",$html);
print "$html\n";
?>

Open in new window

(semicolon on the first line, btw - the one declaring $html)
Avatar of dnotestine
dnotestine

ASKER

Thanks - the semicolon I forgot but was just for the comment here. The actual code in $html is:

<form action="http://www.artechchurchinteriors.com/portfolio/portfolio_zip_email.php" method='POST' name='hidden_email_form'>
<input type='text' id='email' name='email' value='enter email address to email list' size='35' onClick="SelectAll('email');" />&nbsp;&nbsp;
<input type='hidden' name='hidden_html' id='hidden_html' value='' />
<input type='submit' name='submit' value=' Email Now ' />
</form>

-----------------

$html = preg_replace($pattern,"",$html);
print "$html\n";

Does not take out the Form :-(
$pattern = "/<form(.*?)<\/form>/i";
$html = preg_replace($pattern,"",$html);
ASKER CERTIFIED SOLUTION
Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
The "s" pattern modifier allows the . wildcard to match newlines
Note that the round brackets are unnecessary too...

$pattern = "/<form.*?<\/form>/is";