2

I have a html file loaded as a string in php, and I need to get the values of the input elements in the HTML string. Can someone help me build a function which takes the name of the input element and returns its value?

This is an example of the function I would like to do:

function getVal($name){

    $htmlStr = "<form action = \"action.php\"><input type=\"hidden\" name=\"command\" value=\"123456\">
                <input type=\"hidden\" name=\"quantity\" value=\"1\">
                <input type=\"hidden\" name=\"user_mode\" value=\"1\">
                <input type=\"hidden\" name=\"stock\" value=\"-1255303070\">
                <input type=\"hidden\" name=\"id\" value="429762082">
                <input type=\"hidden\" name=\"pidm\" value=\"2\"></form>";

            // I'd like to get the value of $name here probably using preg_match     

            return $value; //returns 123456

        }

$val = getVal("command"); //val should be 123456.

any idas? Thank you

Andy Lester
  • 91,102
  • 13
  • 100
  • 152
Jorge Zuverza
  • 885
  • 8
  • 26
  • 4
    This would be easier if you just posted the form to your script. – Patrick James McDougle Nov 27 '12 at 19:00
  • 2
    Use `DOMDocument` to parse the HTML string and `DOMXPath` to query for the value you want. There are zillions of examples here in SO (both for HTML and XML, it's the same most of the time). – Jon Nov 27 '12 at 19:00
  • instead, use ajax to run a php file in javascript and pass the value of the input field you want. – user1534664 Nov 27 '12 at 19:00
  • http://stackoverflow.com/questions/1280767/how-do-i-run-php-code-when-a-user-clicks-on-a-link – user1534664 Nov 27 '12 at 19:01
  • What exactly are you trying to do? Like @PatrickJamesMcDougle said if you post the form to your page you can access the post variables and manipulate them anyway you want. – hanleyhansen Nov 27 '12 at 19:02
  • DOM example code in this question: http://stackoverflow.com/q/12563400/50079 – Jon Nov 27 '12 at 19:03
  • @user1534664 - That question is completely irrelevant to this one. – Lion Nov 27 '12 at 19:05

2 Answers2

8

Here is an example with DOM:

$html = "<form action = \"action.php\">
  <input type=\"hidden\" name=\"command\" value=\"123456\">
  <input type=\"hidden\" name=\"quantity\" value=\"1\">
  <input type=\"hidden\" name=\"user_mode\" value=\"1\">
  <input type=\"hidden\" name=\"stock\" value=\"-1255303070\">
  <input type=\"hidden\" name=\"id\" value=\"429762082\">
  <input type=\"hidden\" name=\"pidm\" value=\"2\">
</form>";

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

$inputs = $document->getElementsByTagName("input");

foreach ($inputs as $input) {
  if ($input->getAttribute("name") == "id") {
    $value = $input->getAttribute("value");
  }
}

echo $value;
ioseb
  • 16,625
  • 3
  • 33
  • 29
1

Load the HTML in a DOMDocument, then a simple XPath query would to the trick:

$xpath = new DOMXPath($domDocument);
$items = $xpath->query('//*[@name="command"]/@value');
if ($items->length)
    $value = $items->item(0)->value;
netcoder
  • 66,435
  • 19
  • 125
  • 142
  • 1
    `$value = $xpath->evaluate('string(...)')` can be more convenient if you don't really care about the difference between "no such element" and "empty value". – Jon Nov 27 '12 at 19:08