3

I have HTML stored in a string. The markup contains form input fields called initval and endval which are the value attribute values I need. How can I get them from this string markup?

<form id="compute">
   <input type="hidden" name="initval" value="tal:00far" />
   <input type="hidden" name="endval" value="utl:80er" />
</form>
Gumbo
  • 643,351
  • 109
  • 780
  • 844
sami
  • 7,515
  • 8
  • 33
  • 37
  • 3
    @Jere: You can’t say that in general. It depends on the HTML code and what you’re trying to do with it. – Gumbo Dec 05 '10 at 14:10
  • 1
    @Jere: That isn't a pro tip, that is often wrong, it is a lack-of-understanding tip. – Orbling Dec 05 '10 at 14:14
  • Is the form always just like that, or can it be wildly varied, invalid, etc? – Orbling Dec 05 '10 at 14:15
  • @Gumbo Already having this argument today, wish people would learn the difference between parsing and minor text matching/alterations: http://stackoverflow.com/questions/4358677/regex-selecting-everything-but-img-tag – Orbling Dec 05 '10 at 14:26

3 Answers3

3

Presuming that the structure is very reliably like that, try the following:

$htmlCode = "...";
$matches = array();

if (preg_match_all('/name="(initval|endval)"\s+value="([^"]+)"/', $htmlCode, $matches)) {
    $formValues = array_combine($matches[1], $matches[2]);
} else {
   // error
}

This assumes only whitespace between the name and value attributes, you'll need to make a small change if it differs. preg_match_all() returns an array with the whole regexp match at [0], and then the individual group matches in their corresponding locations [1] & [2], the array combine takes one as keys, one as values and puts it together so you have an associative lookup to get your results.

Orbling
  • 20,413
  • 3
  • 53
  • 64
  • No it won't, he said it was in that order though, changing it to suit any order is not overly complex. It's also a bit unsafe not putting it within an input tag, but like I say, this is the bare minimum for a specific use-case. – Orbling Dec 05 '10 at 14:31
0

If I have got your question right, In HTML

<form id="compute" action="somefile.php" method="GET">
  <input type="hidden" name="initval" value="tal:00far" />
  <input type="hidden" name="endval" value="utl:80er" />
  <input type="submit" value="Click!">
</form>

Upon clicking submit the data is sent the the php script, where it can be read as

$initval = $_GET['initval'];
$endval =$_GET['endval'];

EDIT: It seems I have got the question wrong, Sorry. :-(

ajreal
  • 46,720
  • 11
  • 89
  • 119
UltraInstinct
  • 43,308
  • 12
  • 81
  • 104
  • The form isn't a form that's being submitted. It's just markup stored in a string, so I mean getting the values via string manipulation. – sami Dec 05 '10 at 14:15
  • you can always delete your answer if you want to do that to avoid confusing future readers. all these comments will be gone with it also. – sami Dec 05 '10 at 14:26
0

Try Using htmldomlibraries for parsing html.

XMen
  • 29,384
  • 41
  • 99
  • 151