MoFish Posted March 30, 2017 Share Posted March 30, 2017 (edited) 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 />"; } } ?> Edited March 30, 2017 by MoFish Quote Link to comment https://forums.phpfreaks.com/topic/303576-domdocument-getelementsbytagname-query/ Share on other sites More sharing options...
requinix Posted March 30, 2017 Share Posted March 30, 2017 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. Quote Link to comment https://forums.phpfreaks.com/topic/303576-domdocument-getelementsbytagname-query/#findComment-1544794 Share on other sites More sharing options...
MoFish Posted March 30, 2017 Author Share Posted March 30, 2017 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}}" /> Quote Link to comment https://forums.phpfreaks.com/topic/303576-domdocument-getelementsbytagname-query/#findComment-1544797 Share on other sites More sharing options...
Solution requinix Posted March 30, 2017 Solution Share Posted March 30, 2017 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. Quote Link to comment https://forums.phpfreaks.com/topic/303576-domdocument-getelementsbytagname-query/#findComment-1544799 Share on other sites More sharing options...
MoFish Posted March 30, 2017 Author Share Posted March 30, 2017 Thank you requinix. Quote Link to comment https://forums.phpfreaks.com/topic/303576-domdocument-getelementsbytagname-query/#findComment-1544830 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.