This is a discussion on DOM - Replacing text in the DOM within the alt.comp.lang.php forums, part of the PHP Programming Forums category; I want to miror what I can do in javascript with regard to some DOM manipulation. I have done lots ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
I want to miror what I can do in javascript with regard to some DOM
manipulation. I have done lots of PHP programming but not in relation to the DOM. I have a html test file :- .......................... <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html><head><title></title></head><body> <div id="books">This is the text in books</div> </body></html> .................... I can access the text 'This is the text in books' with the code:- ......................................... <?php $doc = new DomDocument(); $doc->loadHTMLFile("test.html"); $text=$doc->getElementById('books')->firstChild->textContent; echo "The text is ".$text; ?> .............................................. This all works fine. What I want to do is replace the text in the DOM. How do I do this? In practice, of course, I will have a much bigger html document. I find the manual confusing especially as some commands don't seem to be recognised by my PHP5 installation. The manual often doesn't give any examples of use, or it only half tells the story, and, to me, that is often as useful as a chocolate teapot. I have consulted php_manual_notes.chm (though mine might be out of date and I can't find where to download a new copy). Lots of googling to find an answer to my problem hasn't helped either...So can anyone help please. How do I replace the text in the DOM? Remove 'no_spam_' from email address. Skype Free Zone!! |
|
|||
|
On Sat, 30 Sep 2006 11:48:56 GMT, Brian A
<no_spam_bca1000@hotmail.com> wrote: >I want to miror what I can do in javascript with regard to some DOM >manipulation. I have done lots of PHP programming but not in relation >to the DOM. > >I have a html test file :- > >......................... ><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >"http://www.w3.org/TR/html4/loose.dtd"> > ><html><head><title></title></head><body> > ><div id="books">This is the text in books</div> > ></body></html> >................... > >I can access the text 'This is the text in books' with the code:- > >........................................ ><?php >$doc = new DomDocument(); >$doc->loadHTMLFile("test.html"); >$text=$doc->getElementById('books')->firstChild->textContent; >echo "The text is ".$text; >?> .... >How do I replace the text in the DOM? I have never used these functions. My approach as always been more of of JS/DHTML/AJAX/COMET. However, if you can call the text you want replaced dynamically, replacing the text on a web page is fairly simple: <script type="text/javascript"> document.getElementById(''books').innerHTML = <?=$text?>; </script> How you actually trigger the display is not shown here but, I assume, you will have to create a JS function to do it. You might Google AJAX and COMET for an alternative approach. |
|
|||
|
On Sat, 30 Sep 2006 07:04:02 -0700, Tyrone Slothrop <ts@paranoids.com>
wrote: >On Sat, 30 Sep 2006 11:48:56 GMT, Brian A ><no_spam_bca1000@hotmail.com> wrote: > >>I want to miror what I can do in javascript with regard to some DOM >>manipulation. I have done lots of PHP programming but not in relation >>to the DOM. >> >>I have a html test file :- >> >>......................... >><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >>"http://www.w3.org/TR/html4/loose.dtd"> >> >><html><head><title></title></head><body> >> >><div id="books">This is the text in books</div> >> >></body></html> >>................... >> >>I can access the text 'This is the text in books' with the code:- >> >>........................................ >><?php >>$doc = new DomDocument(); >>$doc->loadHTMLFile("test.html"); >>$text=$doc->getElementById('books')->firstChild->textContent; >>echo "The text is ".$text; >>?> >... >>How do I replace the text in the DOM? > >I have never used these functions. My approach as always been more of >of JS/DHTML/AJAX/COMET. However, if you can call the text you want >replaced dynamically, replacing the text on a web page is fairly >simple: > ><script type="text/javascript"> >document.getElementById(''books').innerHTML = <?=$text?>; ></script> > >How you actually trigger the display is not shown here but, I assume, >you will have to create a JS function to do it. > >You might Google AJAX and COMET for an alternative approach. ************************************************** ********************************* Thanks for your answer. However, I want this to be just a PHP thing. I am going to be using AJAX with the site too, and I have that working, but I have to cater for those who do not have javascript. I will do this with PHP rather than mess with html because I want to store some of my content in a database. - providing I can get an answer as to what commands I need to use to replace the text in the DOM as in my original post. ************************************************** ******************************** I can do what I want to do in Javascript but, as I said, I don't want to use javascript for this task. I don't thiink 'innerHTML' on javascript is part of the official DOM even though the main browsers support it. For that reasonm I don't tend to use it. It takes more code in Javascript to do it via the official DOM commands I do admit. Remove 'no_spam_' from email address. Skype Free Zone!! |
|
|||
|
Brian A wrote:
> I want to miror what I can do in javascript with regard to some DOM > manipulation. I have done lots of PHP programming but not in relation > to the DOM. > > I have a html test file :- > > ......................... > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" > "http://www.w3.org/TR/html4/loose.dtd"> > > <html><head><title></title></head><body> > > <div id="books">This is the text in books</div> > > </body></html> > ................... > > I can access the text 'This is the text in books' with the code:- > > ........................................ > <?php > $doc = new DomDocument(); > $doc->loadHTMLFile("test.html"); > $text=$doc->getElementById('books')->firstChild->textContent; > echo "The text is ".$text; > ?> > ............................................. > This all works fine. What I want to do is replace the text in the DOM. > How do I do this? In practice, of course, I will have a much bigger > html document. > > I find the manual confusing especially as some commands don't seem to > be recognised by my PHP5 installation. > The manual often doesn't give any examples of use, or it only half > tells the story, and, to me, that is often as useful as a chocolate > teapot. > I have consulted php_manual_notes.chm (though mine might be out of > date and I can't find where to download a new copy). > Lots of googling to find an answer to my problem hasn't helped > either...So can anyone help please. > How do I replace the text in the DOM? > > Remove 'no_spam_' from email address. > Skype Free Zone!! php_manual_notes.chm is apart of the PHP Extended CHM Format. http://us2.php.net/docs-echm.php If you can not find the download I can send you a copy I have. Think you want to use DOMCharacterData->replaceData() http://us2.php.net/manual/en/functio...eplacedata.php Article: DOMCharacterData Function replaceData http://www.topxml.com/php_xml_dom/do...eplacedata.asp -- Thanks in Advance... IchBin, Pocono Lake, Pa, USA http://weconsultants.phpnet.us __________________________________________________ ________________________ 'If there is one, Knowledge is the "Fountain of Youth"' -William E. Taylor, Regular Guy (1952-) |
|
|||
|
On Sat, 30 Sep 2006 11:48:56 +0000, Brian A wrote:
> I want to miror what I can do in javascript with regard to some DOM > manipulation. I have done lots of PHP programming but not in relation > to the DOM. > > snip > > This all works fine. What I want to do is replace the text in the DOM. > How do I do this? In practice, of course, I will have a much bigger html > document. > > I find the manual confusing especially as some commands don't seem to be > recognised by my PHP5 installation. The manual often doesn't give any > examples of use, or it only half tells the story, and, to me, that is > often as useful as a chocolate teapot. > I have consulted php_manual_notes.chm (though mine might be out of date > and I can't find where to download a new copy). > Lots of googling to find an answer to my problem hasn't helped > either...So can anyone help please. > How do I replace the text in the DOM? > Unless I'm misunderstanding your intentions, you can't mirror javascript with PHP as they're fundamentally different technologies. Client-side vs. server-side. Any changes with PHP require the document to be reloaded. In my experience, using the DOM with PHP requires a lot of trial and error as the documentation is lacking, as you've mentioned. My advice would be to save yourself the headache and use a simple preg_replace. If you're intent on using the DOM functions, my guess would be replaceChild() http://ca3.php.net/manual/en/functio...placechild.php There's some good user comments on that page that should help you along. -- Schluppy |
|
|||
|
Brian A wrote: > I want to miror what I can do in javascript with regard to some DOM > manipulation. I have done lots of PHP programming but not in relation > to the DOM. > > I have a html test file :- > > ......................... > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" > "http://www.w3.org/TR/html4/loose.dtd"> > > <html><head><title></title></head><body> > > <div id="books">This is the text in books</div> > > </body></html> > ................... > > I can access the text 'This is the text in books' with the code:- > > ........................................ > <?php > $doc = new DomDocument(); > $doc->loadHTMLFile("test.html"); > $text=$doc->getElementById('books')->firstChild->textContent; > echo "The text is ".$text; > ?> > ............................................. > This all works fine. What I want to do is replace the text in the DOM. > How do I do this? In practice, of course, I will have a much bigger > html document. > > I find the manual confusing especially as some commands don't seem to > be recognised by my PHP5 installation. > The manual often doesn't give any examples of use, or it only half > tells the story, and, to me, that is often as useful as a chocolate > teapot. > I have consulted php_manual_notes.chm (though mine might be out of > date and I can't find where to download a new copy). > Lots of googling to find an answer to my problem hasn't helped > either...So can anyone help please. > How do I replace the text in the DOM? > > According to the manual, the "textContent" property of a DomNode object is not read only. You've already managed to get access to the property, so just set it to something else: $doc = new DomDocument(); $doc->loadHTMLFile("test.html"); $text=$doc->getElementById('books')->firstChild->textContent; $doc->getElementById('books')->firstChild->textContent = "Some new text"; |
|
|||
|
On 30 Sep 2006 12:22:27 -0700, "ZeldorBlat" <zeldorblat@gmail.com>
wrote: > >Brian A wrote: >> I want to miror what I can do in javascript with regard to some DOM >> manipulation. I have done lots of PHP programming but not in relation >> to the DOM. >> >> I have a html test file :- >> >> ......................... >> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >> "http://www.w3.org/TR/html4/loose.dtd"> >> >> <html><head><title></title></head><body> >> >> <div id="books">This is the text in books</div> >> >> </body></html> >> ................... >> >> I can access the text 'This is the text in books' with the code:- >> >> ........................................ >> <?php >> $doc = new DomDocument(); >> $doc->loadHTMLFile("test.html"); >> $text=$doc->getElementById('books')->firstChild->textContent; >> echo "The text is ".$text; >> ?> >> ............................................. >> This all works fine. What I want to do is replace the text in the DOM. >> How do I do this? In practice, of course, I will have a much bigger >> html document. >> >> I find the manual confusing especially as some commands don't seem to >> be recognised by my PHP5 installation. >> The manual often doesn't give any examples of use, or it only half >> tells the story, and, to me, that is often as useful as a chocolate >> teapot. >> I have consulted php_manual_notes.chm (though mine might be out of >> date and I can't find where to download a new copy). >> Lots of googling to find an answer to my problem hasn't helped >> either...So can anyone help please. >> How do I replace the text in the DOM? >> >> > >According to the manual, the "textContent" property of a DomNode object >is not read only. You've already managed to get access to the >property, so just set it to something else: > >$doc = new DomDocument(); >$doc->loadHTMLFile("test.html"); >$text=$doc->getElementById('books')->firstChild->textContent; >$doc->getElementById('books')->firstChild->textContent = "Some new >text"; I agree with you absolutely. I does indeed say that it isn't read only. So I had already tried that and it didn't work :-( I tried:- $doc->getElementById('books')->firstChild->textContent="The quick brown fox"; but no luck. I wasn't sure exactly how to use replaceChild(). I had already tried that too. I tried it but I got an error stating that it couldn't find the method (or some such statement). It could be that I just formed the syntax incorrectly - but the manual isn't exactly clear. Some of the commands aren't 'official' and get dropped I think. These manuals are often written by people who understand the subject so well they hardly think it is worth explaining - so they skim over the subject and miss out all the vital detail - much the same as Microsoft help files. I hardly ever find them to be any use. What is needed is some practical examples. Sometimes some are shown but, again, even then, they are often not sufficient to get a grasp of how to use the commands. I've checked out several books and still can't find a definitive answer. Remove 'no_spam_' from email address. Skype Free Zone!! |
|
|||
|
Brian A wrote: > On 30 Sep 2006 12:22:27 -0700, "ZeldorBlat" <zeldorblat@gmail.com> > wrote: > > > > >Brian A wrote: > >> I want to miror what I can do in javascript with regard to some DOM > >> manipulation. I have done lots of PHP programming but not in relation > >> to the DOM. > >> > >> I have a html test file :- > >> > >> ......................... > >> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" > >> "http://www.w3.org/TR/html4/loose.dtd"> > >> > >> <html><head><title></title></head><body> > >> > >> <div id="books">This is the text in books</div> > >> > >> </body></html> > >> ................... > >> > >> I can access the text 'This is the text in books' with the code:- > >> > >> ........................................ > >> <?php > >> $doc = new DomDocument(); > >> $doc->loadHTMLFile("test.html"); > >> $text=$doc->getElementById('books')->firstChild->textContent; > >> echo "The text is ".$text; > >> ?> > >> ............................................. > >> This all works fine. What I want to do is replace the text in the DOM. > >> How do I do this? In practice, of course, I will have a much bigger > >> html document. > >> > >> I find the manual confusing especially as some commands don't seem to > >> be recognised by my PHP5 installation. > >> The manual often doesn't give any examples of use, or it only half > >> tells the story, and, to me, that is often as useful as a chocolate > >> teapot. > >> I have consulted php_manual_notes.chm (though mine might be out of > >> date and I can't find where to download a new copy). > >> Lots of googling to find an answer to my problem hasn't helped > >> either...So can anyone help please. > >> How do I replace the text in the DOM? > >> > >> > > > >According to the manual, the "textContent" property of a DomNode object > >is not read only. You've already managed to get access to the > >property, so just set it to something else: > > > >$doc = new DomDocument(); > >$doc->loadHTMLFile("test.html"); > >$text=$doc->getElementById('books')->firstChild->textContent; > >$doc->getElementById('books')->firstChild->textContent = "Some new > >text"; > I agree with you absolutely. I does indeed say that it isn't read > only. So I had already tried that and it didn't work :-( > I tried:- > $doc->getElementById('books')->firstChild->textContent="The quick > brown fox"; > but no luck. > I wasn't sure exactly how to use replaceChild(). I had already tried > that too. > I tried it but I got an error stating that it couldn't find the method > (or some such statement). It could be that I just formed the syntax > incorrectly - but the manual isn't exactly clear. Some of the commands > aren't 'official' and get dropped I think. These manuals are often > written by people who understand the subject so well they hardly think > it is worth explaining - so they skim over the subject and miss out > all the vital detail - much the same as Microsoft help files. I hardly > ever find them to be any use. What is needed is some practical > examples. Sometimes some are shown but, again, even then, they are > often not sufficient to get a grasp of how to use the commands. > I've checked out several books and still can't find a definitive > answer. > > Remove 'no_spam_' from email address. > Skype Free Zone!! $doc->getElementById('books')->firstChild is an instance of DomCharacterData, so try the following instead: $doc->getElementById('books')->firstChild->data = "Some new text"; |
|
|||
|
On 30 Sep 2006 18:20:40 -0700, "ZeldorBlat" <zeldorblat@gmail.com>
wrote: > >Brian A wrote: >> On 30 Sep 2006 12:22:27 -0700, "ZeldorBlat" <zeldorblat@gmail.com> >> wrote: >> >> > >> >Brian A wrote: >> >> I want to miror what I can do in javascript with regard to some DOM >> >> manipulation. I have done lots of PHP programming but not in relation >> >> to the DOM. >> >> >> >> I have a html test file :- >> >> >> >> ......................... >> >> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >> >> "http://www.w3.org/TR/html4/loose.dtd"> >> >> >> >> <html><head><title></title></head><body> >> >> >> >> <div id="books">This is the text in books</div> >> >> >> >> </body></html> >> >> ................... >> >> >> >> I can access the text 'This is the text in books' with the code:- >> >> >> >> ........................................ >> >> <?php >> >> $doc = new DomDocument(); >> >> $doc->loadHTMLFile("test.html"); >> >> $text=$doc->getElementById('books')->firstChild->textContent; >> >> echo "The text is ".$text; >> >> ?> >> >> ............................................. >> >> This all works fine. What I want to do is replace the text in the DOM. >> >> How do I do this? In practice, of course, I will have a much bigger >> >> html document. >> >> >> >> I find the manual confusing especially as some commands don't seem to >> >> be recognised by my PHP5 installation. >> >> The manual often doesn't give any examples of use, or it only half >> >> tells the story, and, to me, that is often as useful as a chocolate >> >> teapot. >> >> I have consulted php_manual_notes.chm (though mine might be out of >> >> date and I can't find where to download a new copy). >> >> Lots of googling to find an answer to my problem hasn't helped >> >> either...So can anyone help please. >> >> How do I replace the text in the DOM? >> >> >> >> >> > >> >According to the manual, the "textContent" property of a DomNode object >> >is not read only. You've already managed to get access to the >> >property, so just set it to something else: >> > >> >$doc = new DomDocument(); >> >$doc->loadHTMLFile("test.html"); >> >$text=$doc->getElementById('books')->firstChild->textContent; >> >$doc->getElementById('books')->firstChild->textContent = "Some new >> >text"; >> I agree with you absolutely. I does indeed say that it isn't read >> only. So I had already tried that and it didn't work :-( >> I tried:- >> $doc->getElementById('books')->firstChild->textContent="The quick >> brown fox"; >> but no luck. >> I wasn't sure exactly how to use replaceChild(). I had already tried >> that too. >> I tried it but I got an error stating that it couldn't find the method >> (or some such statement). It could be that I just formed the syntax >> incorrectly - but the manual isn't exactly clear. Some of the commands >> aren't 'official' and get dropped I think. These manuals are often >> written by people who understand the subject so well they hardly think >> it is worth explaining - so they skim over the subject and miss out >> all the vital detail - much the same as Microsoft help files. I hardly >> ever find them to be any use. What is needed is some practical >> examples. Sometimes some are shown but, again, even then, they are >> often not sufficient to get a grasp of how to use the commands. >> I've checked out several books and still can't find a definitive >> answer. >> >> Remove 'no_spam_' from email address. >> Skype Free Zone!! > >$doc->getElementById('books')->firstChild is an instance of >DomCharacterData, so try the following instead: > >$doc->getElementById('books')->firstChild->data = "Some new text"; You've done it!! Many thanks. I have learned something from this and that is to check that the instance matches the object. That is something that I had missed. Thanks, works just fine :-) Remove 'no_spam_' from email address. Skype Free Zone!! |