Jump to content


Photo

Regular Expressions and preg_replace


  • Please log in to reply
4 replies to this topic

#1 grazzman

grazzman
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 26 July 2006 - 01:44 AM

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....

$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);


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...

#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 26 July 2006 - 02:04 AM

The dot metacharacter matches "everything but a new line." As a result, your pattern will only work when the form tags are on the same line. To make the dot really match everything, use the 's' modifier: /<form.+form>/s
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#3 grazzman

grazzman
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 26 July 2006 - 02:21 AM

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>

#4 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 26 July 2006 - 02:45 AM

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: /<form.+?form>/s
2. Use the 'U' switch to make all quantifiers ungreedy by default: /<form.+form>/Us
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#5 grazzman

grazzman
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 26 July 2006 - 02:53 AM

Thanks again.... You guys are the best.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users