tags:

views:

45

answers:

1

Hello!

I'm using DOM's loadHTMLFile to grab a page from elsewhere.

I need to find all hyperlinks on the page and then append them so they begin with another, fixed address. E.G.:

Take:

<a href="http://www.google.com"&gt; Google yay! </a>

And turn it into:

<a href="http://cheese.com/http://www.google.com"&gt; Google yay! </a>

Unsure of how to go about doing this. Many thanks in advance for any help. E-Beer for the correct answer.

+2  A: 

use $dom->getElementsByTagName('a') to get a nodeList. Check if the ->length is greater than 0. If so, iterate with a foreach or for loop using the ->length as the counter and $nodelist->item($i). Grab the ->getAttribute('href'). If it matches a certain pattern using regex testing, then $el->setAttribute($newhref);

meder
That sounds great but my limited knowledge of DOM means it'll take me at least 7 years to implement. Could you point me in the right direction?
DrShamoon
@DrShamoon the answer is already pointing you into the right direction. You want the codez :)
Gordon
Whilst that would save me a lot of time, it's not necessary. I wouldn't know where to begin with the regex and not sure why the length thing exists or how to use it as a counter? The rest i reckon i could probably scramble together.
DrShamoon
You don't actually really need to use regex.. you could though. The length is on the nodelist returned by the first method called.
meder