Nalin nishant
Nalin nishant

Reputation: 15

How to Get value with name by Dom

Hy friends I am using this method to get all href links from tag from a site

$DOM = new DOMDocument();
    @$DOM->loadHTML($data);
    @$links = $DOM->getElementsByTagName('a');
    foreach($links as $link){
        $url = $link->getAttribute('href');
echo $url;

Now I don't know how to get the value by name fb_dtsg ..... Here is the source code

<input type="hidden" name="fb_dtsg" value="AQF0dSiG6Lyr:AQEnJP0PhWzy" autocomplete="off" />

I want to get it's value with DOm how to do this...... Thanks in advance

Upvotes: 0

Views: 870

Answers (4)

Nimeshka Srimal
Nimeshka Srimal

Reputation: 8930

You can use DOMXpath()'s query method to get elements by the name attribute.

$DOM = new DOMDocument();
@$DOM->loadHTML($data);
@$links = $DOM->getElementsByTagName('a');

$xpath = new DOMXpath($DOM);
$input = $xpath->query('//input[@name="fb_dtsg"]');

echo $input[0]->getAttribute('value');

This will print the value of the first input element with name 'fb_dtsg'.

Hope it helps :) Feel free to ask if you need to know anything more.

Upvotes: 0

Naveed Ramzan
Naveed Ramzan

Reputation: 3593

$DOM->getElementsByTagName('a'); // for tag name


$DOM->getElementsByName('fb_dtsg'); // for name


document.getElementById('fb_dtsg_id').value // for showing value of the field

Upvotes: 0

jeprubio
jeprubio

Reputation: 18002

$DOM = new DOMDocument();
@$DOM->loadHTML($data);
@$links = $DOM->getElementsByTagName('input');
foreach($inputs as $input) {
     if ($input->getAttribute('name') == 'fb_dtsg') {
         echo 'found, do whatever';
         break;
    }
}

Upvotes: 1

Andrei Todorut
Andrei Todorut

Reputation: 4526

Use xpath for that.

$DOM = new DOMDocument();
@$DOM->loadHTML($data);

$xpath = new DOMXpath($DOM);
$elementByName = $xpath->query("//input[@name='fb_dtsg']");

...

http://php.net/manual/ro/class.domxpath.php

Upvotes: 0

Related Questions