Reading attribute values ​​from HTML

I have HTML stored in a string. The markup contains form fields inputcalled initval and endval, which are the attribute values valuethat I need. How can I get them from this line layout?

<form id="compute">
   <input type="hidden" name="initval" value="tal:00far" />
   <input type="hidden" name="endval" value="utl:80er" />
</form>
+3
source share
3 answers

Assuming the structure is very reliable like this, 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
}

name value, , . preg_match_all() [0], [1] [2], , , , .

+3

, 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>

php script,

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

. , , .: - (

0

htmldomlibraries html.

0

Source: https://habr.com/ru/post/1778594/


All Articles