8

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>
3
  • It doesn't look like a PHP problem. Could You describe it little more? Commented Feb 28, 2012 at 8:09
  • @Michas: hello. Why do say so? Commented Feb 28, 2012 at 8:20
  • @Marc 1. I don't see any PHP code in Your question. 2. In PHP it is uncommon to working directly on html source. 3. Working on html source is typical for JavaScript. Commented Feb 28, 2012 at 9:11

5 Answers 5

15

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!

Sign up to request clarification or add additional context in comments.

2 Comments

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
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.
6

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'));

3 Comments

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...
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...
should be loadHTML()
5

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

5 Comments

Again, I am looking for a way to extract that value from my php script, not using Jquery. Thanks anyway...
Could you rather develop the solution you propose in "EDIT:" rather that then mentioning Jquery?
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?
I will need more information on your problem in order to help further. sorry
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
2

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)

Comments

1

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

http://simplehtmldom.sourceforge.net/

4 Comments

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?
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..
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?
I guess if the element is loaded dynamically this php script can't process it, but I'm not sure..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.