tags:

views:

83

answers:

4

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

A: 

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

no
A: 

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
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!
Alex JL
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
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
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
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.
Alex JL
+3  A: 

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
Try `echo $xPath->evaluate('string(//input[@name="id"]/@value)');`
Gordon
A: 
$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
Alex JL
Will trigger *Strict Standards: Non-static method DOMDocument::loadHTML() should not be called statically*
Gordon
Yeah, I suppose... but it works. Might as well go with the preferred style.
Alex JL