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

4
  • 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? Commented Jul 14, 2010 at 19:17
  • Is this while generating the page (with php) or after the form's submitted? Commented Jul 14, 2010 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. Commented Jul 14, 2010 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. Commented Jul 14, 2010 at 19:19

4 Answers 4

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Try echo $xPath->evaluate('string(//input[@name="id"]/@value)');
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

2 Comments

Will trigger Strict Standards: Non-static method DOMDocument::loadHTML() should not be called statically
Yeah, I suppose... but it works. Might as well go with the preferred style.
0

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

Comments

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);

6 Comments

Everyone loves them... just NOT FOR HTML PARSING. See stackoverflow.com/questions/1732348/… for a bit of context regarding this and SO!
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.
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?
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.
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.