noodlez Posted October 7, 2015 Share Posted October 7, 2015 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 Quote Link to comment https://forums.phpfreaks.com/topic/298476-loop-through-html-grab-all-urls-and-replace-hrefssrcs/ Share on other sites More sharing options...
QuickOldCar Posted October 8, 2015 Share Posted October 8, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/298476-loop-through-html-grab-all-urls-and-replace-hrefssrcs/#findComment-1522641 Share on other sites More sharing options...
seandisanti Posted October 8, 2015 Share Posted October 8, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/298476-loop-through-html-grab-all-urls-and-replace-hrefssrcs/#findComment-1522645 Share on other sites More sharing options...
seandisanti Posted October 8, 2015 Share Posted October 8, 2015 That is quick and dirty approach using a few easy to implement functions, I'm sure that a better approach exists, but it sounds like you're looking for a single use script Quote Link to comment https://forums.phpfreaks.com/topic/298476-loop-through-html-grab-all-urls-and-replace-hrefssrcs/#findComment-1522646 Share on other sites More sharing options...
cyberRobot Posted October 8, 2015 Share Posted October 8, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/298476-loop-through-html-grab-all-urls-and-replace-hrefssrcs/#findComment-1522684 Share on other sites More sharing options...
noodlez Posted October 8, 2015 Author Share Posted October 8, 2015 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 Quote Link to comment https://forums.phpfreaks.com/topic/298476-loop-through-html-grab-all-urls-and-replace-hrefssrcs/#findComment-1522695 Share on other sites More sharing options...
noodlez Posted October 8, 2015 Author Share Posted October 8, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/298476-loop-through-html-grab-all-urls-and-replace-hrefssrcs/#findComment-1522696 Share on other sites More sharing options...
cyberRobot Posted October 8, 2015 Share Posted October 8, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/298476-loop-through-html-grab-all-urls-and-replace-hrefssrcs/#findComment-1522730 Share on other sites More sharing options...
seandisanti Posted October 8, 2015 Share Posted October 8, 2015 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 Quote Link to comment https://forums.phpfreaks.com/topic/298476-loop-through-html-grab-all-urls-and-replace-hrefssrcs/#findComment-1522746 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.