5

I know regex isn't popular here, what is the best way to extract a value of an input tag within an HTML form using a php script?

for example:

some divs/tables etc..

<form action="blabla.php" method=post>

<input type="text" name="campaign">  
<input type="text" name="id" value="this-is-what-i-am-trying-to-extract">

</form>

some divs/tables etc..

Thanks

Jim
  • 375
  • 2
  • 4
  • 11
  • Any chance you can rephrase that? Are you wanting to get the value of the input control in your php script when it is posted? If so that is pretty basic. Also not sure what the comment about regex has in this context? – spinon Jul 14 '10 at 19:17
  • Is this while generating the page (with php) or after the form's submitted? – David Thomas Jul 14 '10 at 19:17
  • @spinon I think he's referring to the [correct] "you can't parse HTML with a regex" reply, but I don't think he's yet realized that's not how he wants to go about this, even if you could. – Marc Bollinger Jul 14 '10 at 19:18
  • Sorry, I should have explained myself better. I have a string var with a bunch of HTML in it. From that string of HTML I need to extract the value of an input tag, the name of the input tag is always the same (id) if that helps. – Jim Jul 14 '10 at 19:19

4 Answers4

15

If you want to extract some data from some HTML string, the best solution is often to work with the DOMDocument class, that can load HTML to a DOM Tree.

Then, you can use any DOM-related way of extracting data, like, for example, XPath queries.


Here, you could use something like this :

$html = <<<HTML
    <form action="blabla.php" method=post>

    <input type="text" name="campaign">  
    <input type="text" name="id" value="this-is-what-i-am-trying-to-extract">

    </form>
HTML;


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

$xpath = new DOMXPath($dom);

$tags = $xpath->query('//input[@name="id"]');
foreach ($tags as $tag) {
    var_dump(trim($tag->getAttribute('value')));
}

And you'd get :

string 'this-is-what-i-am-trying-to-extract' (length=35)
Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
4
$html=new DOMDocument();
$html->loadHTML('<form action="blabla.php" method=post>
    <input type="text" name="campaign">  
    <input type="text" name="id" value="this-is-what-i-am-trying-to-extract">
    </form>');

$els=$html->getelementsbytagname('input');

foreach($els as $inp)
  {
  $name=$inp->getAttribute('name');
  if($name=='id'){
    $what_you_are_trying_to_extract=$inp->getAttribute('value');
    break;
    }
  }

echo $what_you_are_trying_to_extract;
//produces: this-is-what-i-am-trying-to-extract
JAL
  • 21,295
  • 1
  • 48
  • 66
  • Will trigger *Strict Standards: Non-static method DOMDocument::loadHTML() should not be called statically* – Gordon Jul 14 '10 at 19:41
  • Yeah, I suppose... but it works. Might as well go with the preferred style. – JAL Jul 14 '10 at 21:57
0

Post the form to a php page. The value you want will be in $_POST['id'].

Dagg Nabbit
  • 75,346
  • 19
  • 113
  • 141
0

What do you mean regex isn't popular? I for one love regular expressions.

Anyway, what you want is something like:

$contents = file_get_contents('/path/to/file.html');
preg_match('/value="(\w+)"/',$contents,$result);
Josiah
  • 4,754
  • 1
  • 20
  • 19
  • 3
    Everyone loves them... just NOT FOR HTML PARSING. See http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 for a bit of context regarding this and SO! – JAL Jul 14 '10 at 19:23
  • Hahha I agree, regex is horrid for parsing HTML. However, for the question at hand, regex is a perfectly valid solution, though definitely not the only solution. – Josiah Jul 14 '10 at 19:27
  • What if there is other inputs in the HTML that have a value attribute? What if the attribute is not written precisely `value="foo"`, but `Value = "foo"`. What if the HTML contains this very comment? – Gordon Jul 14 '10 at 19:55
  • All of those can be solved in regex. The solution simply becomes less and less elegant. I'm not in any way advocating regex as a solution for parsing anything as loosely formed as HTML. The question asked for a regex in PHP for a very specific, closed-ended situation. – Josiah Jul 14 '10 at 22:21
  • Agreed, your regex does work and it fits the question, but the best thing to do is to advise the asker not to use regexes for this and to instead propose a solution that incorporates a better practice. – JAL Jul 17 '10 at 20:12
  • I prefer not to use a tank when a quad will do. So preg_match_all was the perfect solution for me. – kklepper Sep 12 '21 at 19:31