1

I am trying to find a way to match something inside a match. I am still reading oreilly's book on regular expressions, but this is something I have to do now:

I need to get all the values from the inputs inside a form. The page contains multiple forms, so normally I would do something similar to this:

type="checkbox" value="([^"])"

But now I need to target all the checkbox values inside the specific form. Using preg_match by the way.

p.s. I know I could get the form with preg_match first, and the match inside that result, but I'm wondering how to do this in regex.

Mrchief
  • 75,126
  • 20
  • 142
  • 189
pyronaur
  • 3,515
  • 6
  • 35
  • 52
  • 1
    To get the value of `input`s from a `form`, you don't need regex. Is there something else you want to try it for? – Mrchief Jul 21 '11 at 17:55
  • I am using CURL to get the HTML. It is not my form that I am scraping data from :) – pyronaur Jul 21 '11 at 18:09

2 Answers2

0
preg_match("|type=\"checkbox\" value=\"(.*?)\"|");
genesis
  • 50,477
  • 20
  • 96
  • 125
0

You really shouldn't be using regular expressions to parse HTML. Look into the HTML DOM Parser. Using this your code becomes very simple.

$html = file_get_html('http://www.website.com/');

$form = $html->find('#formId')

foreach($form->find('input[type=checkbox]') as $element) {
       //Do what you need to do
}
Ryan Gross
  • 6,423
  • 2
  • 32
  • 44
  • Okay, now that's useful. Thank you. Still - the mystery remains, how do I match inside a match ? (I'll be using DOM Parser probably, but I just have to know...) – pyronaur Jul 21 '11 at 18:12
  • Disclaimer: `SimpleHTMLDOM` is _extremely_ slow compared to native `DOM` implementations, see this [excellent answer](http://stackoverflow.com/questions/3577641/best-methods-to-parse-html-with-php/3577662#3577662) on SO to compare alternatives. – Wrikken Jul 21 '11 at 18:26
  • Okay, so then it's going to be easier for me to get the results with Regex after all, is it ? – pyronaur Jul 21 '11 at 18:37