Jump to content

DOMdocument :: getElementsByTagName :: Query


MoFish

Recommended Posts

Hi,

 

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,

 

MoFish

<?php

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

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

$dom = new DOMDocument();
$dom->loadHTML($html);

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

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

 

Cheers,

 

MoFish

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

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.

{{source}}
(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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.