Re: DOM - Replacing text in the DOM
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!!
|