Page 1 of 1
Function to find inside tags. - PHP innerHTML (?)
Posted: Wed May 20, 2009 8:12 pm
by lovelf
file name hello.html
I need to open the file with PHP, then with a function find "Hello" which is inside the h1 tag, the only h1 tag on the entire document.
How to find what's inside a unique tag? A function that grabs what's inside <h1> </h1>
Re: Function to find inside tags. - PHP innerHTML (?)
Posted: Wed May 20, 2009 9:24 pm
by JAB Creations
Amateurs refer to XHTML elements are "tags" and software developers refer to them as "nodes". You're posting in the wrong forum as you're very likely asking how to get the textNode from the header.
You'll want to read about JavaScript and nodes and a good place to start is here...
http://www.howtocreate.co.uk/tutorials/ ... /dombasics
Do
not use innerHTML unless it's for backwards compatibility purposes. Learning how to properly work with "nodes" in JavaScript can be greatly rewarding in the long term if you come to realize what you can eventually do with it.

Re: Function to find inside tags. - PHP innerHTML (?)
Posted: Wed May 20, 2009 9:39 pm
by Eran
He was asking about using PHP to parse HTML from a file.. (not to mention innerHTML is definitely to be used, and not for backwards compatibility)
You can search for the tag using strpos() and some basic logic -
http://us2.php.net/manual/en/function.strpos.php
Alternatively you can load the document using a DOMDocument method called loadHTMLFile -
http://us.php.net/manual/en/domdocument ... mlfile.php, but that would be difficult if the HTML is malformed and its probably too much just for finding a tag.
Re: Function to find inside tags. - PHP innerHTML (?)
Posted: Wed May 20, 2009 9:51 pm
by lovelf
Thanks pytrin.
Re: Function to find inside tags. - PHP innerHTML (?)
Posted: Thu May 21, 2009 3:10 am
by onion2k
JAB Creations wrote:Do not use innerHTML unless it's for backwards compatibility purposes.
I've found innerHTML is several orders of magnitude faster than DOM scripting if you're building complex structures.
Re: Function to find inside tags. - PHP innerHTML (?)
Posted: Fri Jul 03, 2009 11:58 pm
by JAB Creations
Granted innerHTML is faster however it's nearly impossible to work with (X)HTML loaded via innerHTML because it's not added to the DOM. Therefor you may script something and it may or may not appear inside the DOM. I'm just trying to make sure this is understood so lovelf does not end up wondering down the road why they can't script this or that.

Re: Function to find inside tags. - PHP innerHTML (?)
Posted: Sat Jul 04, 2009 10:47 am
by Eran
(X)HTML loaded via innerHTML because it's not added to the DOM.
That's incorrect. HTML inserted via innerHTML is added to the DOM, the same as with DOM methods.
Re: Function to find inside tags. - PHP innerHTML (?)
Posted: Sun Jul 05, 2009 2:40 pm
by JAB Creations
I've had numerous difficulties accessing elements by their ID with innerHTML which is why I've mentioned it. It half-works and half doesn't. It really depends on your goal(s), approach, etc as to whether or not you encounter this error or not. There is always the possibility that innerHTML used to not work with the DOM as desired and now does with newer versions of given rendering engines. Still I highly recommend working with JavaScript nodes however when you need to do a simple write without any desire to read to a containing element I have nothing against using innerHTML.
Somewhere I've read that they've scrapped DOM level 3, haven't heard of DOM4, and that DOM5 is only what the browser vendors have implemented...as if more subjectivity were truly desired on the topic!
