Jump to content

Regular Expressions and preg_replace


grazzman

Recommended Posts

Need some help.... All the code below is working fine but the first 2 lines. I am trying to remove everthing between the FORM tags but its not working....

[code]
$patterns[0] = '/<form.+form>/';
$replacements[0] = ' ';
$patterns[1] = '/<tr.+?>/';
$replacements[1] = '<br>';
$patterns[2] = '/<td.+?>/';
$replacements[2] = ' ';
$patterns[3] = '/:/';
$replacements[3] = '=';

$test1 = preg_replace($patterns, $replacements, $result);

[/code]

results is a html page that I'm getting using a CURL. I need the preg_replace to go from form to form then its being replaced with " ".  The other 2 replace lines work great.

Thanks guys...
Link to comment
https://forums.phpfreaks.com/topic/15644-regular-expressions-and-preg_replace/
Share on other sites

well, that worked, a little to well. Their is more then one form on the page, one near the top and one near the bottom. With the /s its removing everything from the first <form> tag to the last </form> tag on the page where I need it to remove each <form> and leave everthing else... if that makes sense..... I hope...

<form> NEED this removed </form>

NEED this to stay, with the /s this is removed...

<form> NEED this removed</form>
All quantifiers are greedy by default. This means they match as much as possible, thus your first and last form tag. In order to match as little as possible, quantifiers must be ungreedy. This is done in two ways:

1. Place a question mark after a quantifier in order to affect only that quantifier: [tt]/<form.+?form>/s[/tt]
2. Use the 'U' switch to make all quantifiers ungreedy by default: [tt]/<form.+form>/Us[/tt]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.