3
Contributors
2
Replies
19
Views
4 Years
Discussion Span
Last Post by CodeAngry
0

So where are you getting these links? Are they the link of the page the user is currently on? Or are you retrieving them from a database or something like that?

For the rest, you could use regular expressions to find the base part of the link. I think something like this should work:

/^https?:\/\/[a-z0-9-.]+/

E.g.:

<?php
$regex = '/^https?:\/\/[a-z0-9\-.]+/';
preg_match($regex, $full_website_url, $matches);
$base_url = $matches[0];

That should return the base part of the website url, for example the "http://www.daniweb.com" part out of "http://www.daniweb.com/web-development/php/threads/451964/change-all-urls-to-absolute".

Edited by minitauros

0

You need to do output buffering. Parse the markup for @href and @src attributes and start normalizing them. For normalizing urls... it's quite easy. Break by separators '/' into an array (keep note if it ends in /), exclude single dots, exclude two dots and preceeding element (if any) in the array. And at the end join it all together tailing it with a / if it initially had a / at the end.

I have this whole thing written recently in C++ :) I'll publish it here as a PHP tutorial with both output buffering and url resolving. It's currently integrated in my framework, need to rip it out and revisit the code a bit :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.