Page 1 of 1

Switch order of HTML tags in document?

Posted: Wed Aug 10, 2011 2:25 pm
by erika
I'm trying to change the following:
[text]<h1><a id="blah">Blah</a></h1>[/text]
to:
[text]<a id="blah"></a><h1>Blah</h1>[/text]

There may be between zero and a zillion such instances in any given document. The document's contents are already loaded into a string in my script and I am using the simple html dom script (class? both?) for another purpose in the script. Attempts I have made to re-use the object, however, have resulted in errors indicating that maybe what I thought was an object, isn't (object-oriented stuff is still rather confusing to me--the one class I took in PHP years ago really didn't cover it very well).

I get the idea that it probably has something to do with traversing the DOM tree, but I really have no idea how to do that, even after reading documentation. Any assistance with figuring this out (especially examples :) ) would be greatly appreciated. Thank you!

--Erika

Re: Switch order of HTML tags in document? [Solved]

Posted: Fri Aug 12, 2011 12:34 am
by erika
After banging my head against my keyboard for several hours, I figured out what I was doing wrong with using DOM and, in a rather convoluted manner, accomplished my goal. I don't know if it was the "right" solution... it seems kind of bloated, but it works.

I dumped all the h1 tags into an array, then did a foreach on each of them, dumping all the parts of them into another array. Then I used the two arrays together to first do a str_replace, deleting the tag portions of the anchors but leaving the innertext. Then I concatenated the values of the anchor tags together and added what remained of the h1 tag to the end. Unfortunately, it requires that I send the whole megabyte-plus file through str_replace at least once for every h1 tag because I couldn't figure out how to swap the order within the object itself. If anyone has any input in that regard, I would appreciate it.