Function to find inside tags. - PHP innerHTML (?)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Function to find inside tags. - PHP innerHTML (?)

Post by lovelf »

Code: Select all

<h1>Hello</h1>
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>
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Function to find inside tags. - PHP innerHTML (?)

Post 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. :)
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Function to find inside tags. - PHP innerHTML (?)

Post 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.
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Re: Function to find inside tags. - PHP innerHTML (?)

Post by lovelf »

Thanks pytrin.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Function to find inside tags. - PHP innerHTML (?)

Post 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.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Function to find inside tags. - PHP innerHTML (?)

Post 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. :wink:
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Function to find inside tags. - PHP innerHTML (?)

Post 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.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Function to find inside tags. - PHP innerHTML (?)

Post 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! :roll: :lol:
Post Reply