Jump to content

[SOLVED] Need absolute URL Function


iHack

Recommended Posts

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

 

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, != '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

Link to comment
https://forums.phpfreaks.com/topic/64420-solved-need-absolute-url-function/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.