PHP DOMDocument: How do I get the value of an input field

后端 未结 2 1177
执笔经年
执笔经年 2020-12-21 01:49

How do I get the value of an input field like the one below where it does not have an ID attribute using PHP\'s DOMDocument?



        
相关标签:
2条回答
  • 2020-12-21 02:35

    XPath makes it simple, assuming that's the only text input with "make" as its name:

    $dom = new DOMDocument();
    $dom->loadHTML(...);
    $xp = new DOMXpath($dom);
    $nodes = $xp->query('//input[@name="make"]');
    $node = $nodes->item(0);
    
    $car_make = $node->getAttribute('value');
    

    If there's more than one input with that particular field name on the page (which is entirely possible), then you'll have to do some extra work to narrow down WHICH of those multiple inputs you want.

    0 讨论(0)
  • 2020-12-21 02:35
    $dom = new DOMDocument();
    $dom->loadHTML($result);
    $xpath = new DOMXpath($dom);
    $node = $xpath->query('//input[@name="token"]/attribute::value');
    $token = $node->item(0)->nodeValue;
    
    0 讨论(0)
提交回复
热议问题