php - how can I retrieve a div tag attribute value

25,179

Solution 1

XPath is quite the standard for querying XML structures.

However, note that if you want to parse HTML from an untrusted source, that is a source where HTML is not absolutely well formed, you should prefer DOMDocument::loadHTML() to SimpleXML variants, in particular simplexml_load_string.

For Example

<?php
$html = '
<div id="btn-loc" class="hidden" attrLoc="1">
  ...
</div>';

$doc = DOMDocument::loadHTML($html);
$xpath = new DOMXPath($doc);
$query = "//div[@id='btn-loc']";
$entries = $xpath->query($query);
foreach ($entries as $entry) {
  echo "Found: " . $entry->getAttribute("attrloc");
}

Hope it helps!

Solution 2

Using jQuery in JavaScript

var state = $('#btn-loc').attr('attrLoc');

Then you can send the value to PHP

EDIT:

If you are working with an HTML page/DOM in PHP you can use SimpleXML to traverse the DOM and pull your attributes that way

$xml = simplexml_load_string(
    '<div id="btn-loc" class="hidden" attrLoc="1">
    ...
    </div>'
);

foreach (current($xml->xpath('/*/div'))->attributes() as $k => $v)
{
    var_dump($k,' : ',$v,'<br />');
}

You will see the name and the value of the attributes dumped

id : btn-loc
class : hidden
attrLoc : 1

Solution 3

You can also use Document Object Model

<?php
$str = '<div id="btn-loc" class="hidden" attrLoc="1">
text
</div>';
$doc = new DOMDocument();
$d=$doc->loadHtml($str);
$a = $doc->getElementById('btn-loc');
var_dump($a->getAttribute('attrloc'));

Solution 4

to do this with php use simple html dom parser. has a bit of learning curve, but kind of useful

http://simplehtmldom.sourceforge.net/

Solution 5

How about this

$str = '<div id="btn-loc" class="hidden" attrLoc="1">';
$pattern = '/<div id="btn-loc".*\sattrLoc="([0-9])">/';
preg_match($pattern, $str, $matches);
var_dump($matches);

Outputs

array
  0 => string '<div id="btn-loc" class="hidden" attrLoc="1">' (length=45)  
  1 => string '1' (length=1)
Share:
25,179
Marc
Author by

Marc

I am a newbie in web development working on several different projects. The stackoverflow community is a blessing for me and helps me tremendously moving forward. If you are looking my profile it is probably because you helped me, or you are helping me, or you are about to help me. No matter the situation you rock. Thanks for the help!!! Cheers:)

Updated on July 17, 2022

Comments

  • Marc
    Marc almost 2 years

    I have a div which can be hidden or not, depending on the user. That div has an attribute called 'attrLoc'. What I would like is to be abble to retrieve that attribute value from php. Hope someone can help. Thank you in advance for your replies. Cheers. Marc.

    My HTML:

    <div id="btn-loc" class="hidden" attrLoc="1">
    ...
    </div>
    
  • Marc
    Marc over 12 years
    Again, I am looking for a way to extract that value from my php script, not using Jquery. Thanks anyway...
  • Marc
    Marc over 12 years
    Could you rather develop the solution you propose in "EDIT:" rather that then mentioning Jquery?
  • Marc
    Marc over 12 years
    Hello subdesign. The thing is, my web app is mainly ajax driven. The default page only contins the wrappers and not their content. So the div tag which's attribute I need is not displayed in the default source code. Considering that, will the solution you propose work?
  • Marc
    Marc over 12 years
    Thank you Darryn for the extension. The thing is, my web app is mainly ajax driven. The default page only contins the wrappers and not their content. So the div tag which's attribute I need is not displayed in the default source code of the page. Considering that, will the solution you propose work?
  • darryn.ten
    darryn.ten over 12 years
    I will need more information on your problem in order to help further. sorry
  • Tom Desp
    Tom Desp over 12 years
    Probably the shortest way, since we have an id attribute but XPath is much more versatile, imho. Desperately waiting for a getElementBySelector() from PHP team...
  • subdesign
    subdesign over 12 years
    I don't see what you mean not displayed in the default source code. With this lib you can read from string, url and file, and can get attributes, element objects..
  • Tom Desp
    Tom Desp over 12 years
    Again, simplexml_load_string is not reliable if the HTML source is not well formed, from an XML perspective. You should use DOMDocument::loadHTML() as I suggested. Cheers
  • Marc
    Marc over 12 years
    Hello Kuba. Thanks for the input. All this is a little new to me. So I will take a little time to get into it. Thank you...
  • Marc
    Marc over 12 years
    Thank you very much Tom for the help. What you propose is beyond my knowledge but I will read this carefully to understand it. Thks
  • Marc
    Marc over 12 years
    It seems that you have to link an html page to use what you are proposing. The thing is my html page contains only one div called wrapper. That div is empty. No elements, nothing. The content of that div (lots of elements), is loaded through ajax calls based on the user interaction. So if i use get_html() on the default page, their will be only the main wrapper. So impossible to identify the element I am interested in. Right?
  • subdesign
    subdesign over 12 years
    I guess if the element is loaded dynamically this php script can't process it, but I'm not sure..
  • Tom Desp
    Tom Desp over 12 years
    You should really try and learn XPath language, it's a kind of life saver with regard to XML navigation ! Pretty much equivalent to CSS selectors, may be a bit more verbose and less HTML centric though.
  • kjdion84
    kjdion84 over 7 years
    should be loadHTML()