Jump to content

Loop through html, grab all URLS, and replace hrefs/srcs


Recommended Posts

Hey sorry to ask for such a complicated piece of code but I've been trying for hours without any luck.

 

I need to loop through html code find all of the urls that start with http:// or https:// - parse them then replace the entire url with something like this: redirect.php?link=http://originallinkWeAreReplacing

 

 

this way I can replace all urls in the html code with my own php file that will redirect to the original urls that were there..

 

 

Can anyone help? I need it to work for ALL urls.. including style sheets, images, etc

 

thanks

 

Can anyone help? I need it to work for ALL urls.. including style sheets, images, etc

 

That is asking for a lot, the forum is designed to help people with existing code.

 

If you change the urls to specific files like css,images,etc they would no longer work.

 

Changing the clickable href urls while appending your redirect could be done.

This really wouldn't be a very tough script, assuming you're just looking to run it from command line against a code base. Read in a file into a string, explode the string on 'http://' and then step through the resulting array exploding on \" and the first element of THAT array will be the full url to have your way with.

Have you tried using PHP's DOMDocument class?

http://php.net/manual/en/class.domdocument.php

 

Once the HTML code is loaded into DOMDocument, you could get all the <a> tags with getElementsByTagName(). Then you can use getAttribute() to get the href values and setAttribute() to modify them.

I have the html code.. pulled from a curl call then I need to replace all URLS with my own php file like masked.php?link=theoriginallink

 

I didn't show any code because my attempts were complete jokes... i apologize

 

I'm starting to think I won't be getting much help from any php communities lol

The php script i would be replacing with.. would be a redirect script (header location). I would pass the original link through a variable.

 

That is asking for a lot, the forum is designed to help people with existing code.

 

If you change the urls to specific files like css,images,etc they would no longer work.

 

Changing the clickable href urls while appending your redirect could be done.

I have the html code.. pulled from a curl call then I need to replace all URLS with my own php file like masked.php?link=theoriginallink

 

I didn't show any code because my attempts were complete jokes... i apologize

 

I'm starting to think I won't be getting much help from any php communities lol

 

From what has been mentioned so far, DOMDocument seems like it would work. Of course, you would need to expand the example I mentioned earlier so that it can detect more than just links in <a> tags.

I'm starting to think I won't be getting much help from any php communities lol

You've gotten 2 relatively easy to implement suggestions in this thread. It sounds like the 'help' you're looking for is someone to just do it for you

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.