0

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"> Google yay! </a>

And turn it into:

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

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

1 Answer 1

2

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);

Sign up to request clarification or add additional context in comments.

4 Comments

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 the answer is already pointing you into the right direction. You want the codez :)
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.
You don't actually really need to use regex.. you could though. The length is on the nodelist returned by the first method called.

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.