Go Back   HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials > Linux Forums > Programming/Scripts

Do you like HowtoForge? Please consider supporting us by becoming a subscriber.
Reply
 
Thread Tools Display Modes
  #1  
Old 19th April 2005, 17:59
xtothez xtothez is offline
Junior Member
 
Join Date: Apr 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default PHP: Turn relative links into absolute ones?

Hi,

does anyone know of a function to turn relative links in HTML code (<a href="..."></a>, <img src="...">, ...) into absolute links?

Any help is appreciated.

X
Reply With Quote
  #2  
Old 19th April 2005, 18:36
tagammeer tagammeer is offline
Junior Member
 
Join Date: Apr 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try this function:

PHP Code:
function absolute_url($txt$base_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;

$txt is your HTML code where you want to convert your relative links. Works like a charm for me!
Reply With Quote
  #3  
Old 20th April 2005, 19:32
xtothez xtothez is offline
Junior Member
 
Join Date: Apr 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hey, that's a nice one. Does exactly what I need.

Thanks, tagammeer (btw, what is "tagammeer"? Is that a name, or does it mean something? Just curious... )
Reply With Quote
  #4  
Old 20th April 2005, 20:00
tagammeer tagammeer is offline
Junior Member
 
Join Date: Apr 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
btw, what is "tagammeer"? Is that a name, or does it mean something? Just curious...
No, it's not a name, "Tag am Meer" is German for "a day at the beach". I hope it gets warmer soon...
Reply With Quote
  #5  
Old 3rd October 2008, 09:13
Leszek Leszek is offline
Senior Member
 
Join Date: Nov 2006
Location: Poland,Włocławek
Posts: 363
Thanks: 22
Thanked 33 Times in 29 Posts
Send a message via Skype™ to Leszek
Default

Like for example PHP-QT.
Reply With Quote
  #6  
Old 24th November 2008, 19:26
mmrs151 mmrs151 is offline
Junior Member
 
Join Date: Nov 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Some Example please

Quote:
Originally Posted by tagammeer View Post
Try this function:

PHP Code:
function absolute_url($txt$base_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;

$txt is your HTML code where you want to convert your relative links. Works like a charm for me!
Hi, if you can give me an example how to use this wonderfull piece of code...
Reply With Quote
  #7  
Old 25th November 2008, 17:51
falko falko is online now
Super Moderator
 
Join Date: Apr 2005
Location: Lüneburg, Germany
Posts: 31,853
Thanks: 781
Thanked 1,558 Times in 1,477 Posts
Default

Code:
$new_txt = absolute_url($txt, $base_url);
where $txt holds the text that you want to modify and $base_url is the URL of the site.
__________________
Falko
--
Follow me on:
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 00:32.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Sponsored Links: Unified Communications: Thoughts, Strategies and Predictions
Join the discussion.
www.seamlessenterprise.com

IP Convergence
Integrate your wireless and wireline networks.
Learn how from the experts at Sprint.
www.seamlessenterprise.com

Wireless & Wireline Integration
Thoughts, strategies and solutions: join the discussion
www.seamlessenterprise.com

Unified Communications 2009
Join the Discussion. Now.
www.seamlessenterprise.com

Red Hat Virtual Experience - a free virtual event. Dec. 9th