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.
preg_match("|type=\"checkbox\" value=\"(.*?)\"|");
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
}
精彩评论