1

I'm trying to run through some html and insert some custom tags around every instance of an "A" tag. I've got so far, but the last step of actually appending my pseudotags to the link tags is eluding me, can anyone offer some guidance?

It all works great up until the last line of code - which is where I'm stuck. How do I place these pseudotags either side of the selected "A" tag?

$dom = new domDocument;
$dom->loadHTML($section);
$dom->preserveWhiteSpace = false;
$ahrefs = $dom->getElementsByTagName('a');
foreach($ahrefs as $ahref) {
    $valueID = $ahref->getAttribute('name');
    $pseudostart = $dom->createTextNode('%%' . $valueID . '%%');
    $pseudoend = $dom->createTextNode('%%/' . $valueID . '%%');
    $ahref->parentNode->insertBefore($pseudostart, $ahref);
    $ahref->parentNode->appendChild($pseudoend);
    $expression[] = $valueID; //^$link_name[0-9a-z_()]{0,3}$
    $dom->saveHTML();
}
//$dom->saveHTML();

I'm hoping to get this to perform the following:

<a href="xxx" name="yyy">text</a> 

turned into

%%yyy%%<a href="xxx" name="yyy">text</a>%%/yyy%%

But currently it doesn't appear to do anything - the page outputs, but there are no replacements or nodes added to the source.

11
  • Did you try my edited suggestion? Commented Nov 25, 2009 at 15:56
  • Hiya yeah just tried that - same error as before! Commented Nov 25, 2009 at 16:26
  • OK updated the above completely to reflect the new situation - I really need to get this working, I can't see another way of doing it :) Commented Nov 26, 2009 at 10:33
  • You forgot to $pseudostart = $dom->createTextNode('%%' . $valueID . '%%'); etc. Commented Nov 26, 2009 at 10:55
  • 1
    How do you dump the DOM-tree back into a string? $dom->saveXML()? Commented Nov 26, 2009 at 17:22

1 Answer 1

1

In order to make sure that the ahref node is wrapped...

foreach($ahrefs as $ahref) {
    $valueID = $ahref->getAttribute('name');
    $pseudostart = $dom->createTextNode('%%' . $valueID . '%%');
    $pseudoend = $dom->createTextNode('%%/' . $valueID . '%%');
    $ahref->parentNode->insertBefore($pseudostart, $ahref);
    $ahref->parentNode->insertBefore($ahref->cloneNode(true), $ahref); // Inserting cloned element (in order to insert $pseudoend immediately after)
    $ahref->parentNode->insertBefore($pseudoend, $ahref);
    $ahref->parentNode->removeChild($ahref); // Removing old element
}
print $dom->saveXML();
Sign up to request clarification or add additional context in comments.

4 Comments

Hiya, I'm looking to find each complete A tag <a href="xx">description</a> and then put $pseudostart before the <a and $pseudoend after the </a>
In that case you would probably want to add the "pseudo elements" as textNodes using insertBefore() and appendChild() (on the ahref's parent).
can you elaborate on how to do that? The PHP manual documentation is less than clear to me.
OK updated the above completely to reflect the new situation - I really need to get this working, I can't see another way of doing it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.