Please login or register.

Login with username, password and session length
Advanced search  

News:

Get quality web hosting, virtual private servers, reseller web hosting, and dedicated servers from www.webhostfreaks.com or www.serverpowered.com!

Maintenance Notice

PHPFreaks has successfully moved to a new Dedicated Server, hosted by Server Powered. Please help support future upgrades by Donating.

Author Topic: [SOLVED] Need absolute URL Function  (Read 571 times)

0 Members and 1 Guest are viewing this topic.

iHack

  • Irregular
  • Offline Offline
  • Posts: 23
    • View Profile
[SOLVED] Need absolute URL Function
« on: August 11, 2007, 12:31:12 PM »
hey,

I need to a function converttorelative($page,$url) where $page is HTML code that I want to convert your relative links and $url is the url over the html code page, that the base url needs to be retrieved from.

It needs to convert both href= and src=.

I'm using this at the moment, but it doesn't convert the url to the base_url (as I don't have a function for that, or can't find one).

Code: [Select]
function absolute_url($page, $url){
  $needles = array('href="', 'src="', 'background="');
  $new_txt = '';
  if(substr($base_url,-1) != '/') $base_url .= '/';
  $new_base_url = $base_url;
  $base_url_parts = parse_url($base_url);

  foreach($needles as $needle){
    while($pos = strpos($txt, $needle)){
      $pos += strlen($needle);
      if(substr($txt,$pos,7) != 'http://' && substr($txt,$pos,8) != 'https://' && substr($txt,$pos,6) != 'ftp://' && substr($txt,$pos,9) != 'mailto://'){
        if(substr($txt,$pos,1) == '/') $new_base_url = $base_url_parts['scheme'].'://'.$base_url_parts['host'];
        $new_txt .= substr($txt,0,$pos).$new_base_url;
      } else {
        $new_txt .= substr($txt,0,$pos);
      }
      $txt = substr($txt,$pos);
    }
    $txt = $new_txt.$txt;
    $new_txt = '';
  }
  return $txt;
}

Thanks.

PS. The app that is using this can be found at http://cache.ihack.co.uk, if you find any other bugs (not related to absolute/relative URL's) please post  ;D
Logged

iHack

  • Irregular
  • Offline Offline
  • Posts: 23
    • View Profile
Re: [SOLVED] Need absolute URL Function
« Reply #1 on: August 11, 2007, 03:03:42 PM »
Wrote it myself  ;)

iHack
Logged
Pages: [1]
 

Page created in 0.044 seconds with 18 queries.