Regexp catching the HTML whithin a form

Hello,

Here’s a form I have. I’d like to grab what’s inside it. So far my attempts are pathetic:


preg_match_all('#<form(.*)name="post"(.*)action="post.php"(.*)</form>#',$html,$matches);

Here’s the HTML:


<form name="post" action="post.php" method="post" id="post">
...
</form>

Could anyone help me improve this code snippet, and not laugh at me?

:slight_smile:

What would you like to obtain from that?

All the pairs of name and value for each <input> field found within the form.


<input name="foo" value="">
<input name="var" value="bar" id="blah">
...

I am trying to automate some repetitive tasks I am tired of doing…

:slight_smile:

Here’s something to play with. :slight_smile:


<?php
$html = '
<form action="post.php" method="post">
  <input name="foo" value="bar" />
  <input name="bar" value="foo">
</form>
';

$doc = new DOMDocument;
$doc->loadHTML($html);

foreach($doc->getElementsByTagName('input') as $input){
  var_dump(array(
    'name'  => $input->getAttribute('name'),
    'value' => $input->getAttribute('value')
  ));
}

/*
  array(2) {
    ["name"]=>
    string(3) "foo"
    ["value"]=>
    string(3) "bar"
  }
  array(2) {
    ["name"]=>
    string(3) "bar"
    ["value"]=>
    string(3) "foo"
  }
*/

Woah. Great approach. Much, much better than my one. :slight_smile:

I just have a little issue. There is more than one form on the page I’m dealing with. How would I only get the one I posted above?

I tried this without any success:


$form = $doc->getElementById('post');

And then… how would I loop through the fields of that specific form? Yeah, I’m that a beginner…

:wink:


<?php
$html = '
<html>
  <head>
    <title>Demo</title>
  </head>
  <body>
  
    <form action="post.php" method="post">
      <input name="foo" value="bar" />
      <input name="bar" value="foo">
    </form>
    
    <form action="post.php" method="post">
      <input name="ying" value="yang" />
      <input name="yang" value="ying">
    </form>
    
  </body>
</html>
';

$doc = new DOMDocument;
$doc->loadHTML($html);

$form = $doc->getElementsByTagName('form')->item(1);

foreach($form->getElementsByTagName('input') as $input){
  var_dump(array(
    'name'  => $input->getAttribute('name'),
    'value' => $input->getAttribute('value')
  ));
}

/*
  array(2) {
    ["name"]=>
    string(4) "ying"
    ["value"]=>
    string(4) "yang"
  }
  array(2) {
    ["name"]=>
    string(4) "yang"
    ["value"]=>
    string(4) "ying"
  }
*/

Yeah Anthony. Works perfect.

Not only helpful, you thaught me something. :slight_smile:

If someone has the “Warning” issue because of ill-formatted documents, here’s how to fix it:
PHP: DOMDocument::loadHTML - Manual

:slight_smile: