Jump to content

DOMdocument :: getElementsByTagName :: Query

Go to solution Solved by requinix,

Recommended Posts



I am trying to use DOMdocument to find <edit> tags in my HTML.


When I use an <edit> tag on it's own it works fine and finds all the information I require :)


On the odd occasion, I am required to put my <edit> tags inside quotes (as per my second example commented out) which falls over. I'm assuming it does not like the quotes and is bombing out :(


Can anyone shed some light on if this is possible? or will this simply not work with the DOMdocument due to it's structure? I understand its' not the norm.


Many Thanks,




// works
$html = "<edit name='source' label='Source' help='boom' />";

// doesnt work
// $html = "<img src='<edit name='source' label='Source' help="boom" />' />";

$dom = new DOMDocument();

$edits = $dom->getElementsByTagName('edit');
foreach ($edits as $edit) {
    foreach ($edit->attributes as $attr) {
      echo "Name '$attr->nodeName' | Value '$attr->nodeValue'<br />";

Edited by MoFish
Link to comment
Share on other sites

Putting in an attribute turns it from an element into a normal string. Unfortunately DOM doesn't have a querySelector method, which you could use for this, so...


No, it's not really possible. Not in a way you're thinking. You'd have to iterate over all elements (at least those you suspect might have the ) and search their attributes manually.


Putting markup in an attribute [that isn't going to be processed by Javascript] is a bad idea anyways.

Link to comment
Share on other sites

Hi requinix,


Thanks for your reply.


I may look at parsing the document for items in double braces instead, hopefully that makes things a little simpler.





<img src='{{source}}' label='{{label}}' help="{{help}}" />
Link to comment
Share on other sites

  • Solution

That would be much better.


Templating has two approaches:

1. Textual. Do stuff like {{token}} and use text replacement. Very powerful but it's easy to make mistakes and create invalid HTML.

(self-closing tags are deprecated in HTML, by the way)

2. Syntactic. XSLT is an example of this. Uses the syntax of the HTML itself. Has some restrictions, which you can workaround with added complexity, but is always valid markup. Might look like

#1 is what just about everybody does. #2 is a dated approach that is mostly suited to specific circumstances.
Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.