Jump to content

trying to make a function to convert relative links into absolute links!


Recommended Posts

ok, I am having problems with this so let me tell you guys what I am trying to make. I want a function, that I could call like this:

 

returnabsolutelink($thispage,$relativelink);

 

Where $thispage could be something like:

http://mydomain.com

http://mydomain.com/contact/

http://mydomain.com/contact.php

http://mydomain.com/contact/index.php

and $relativelink could be something like:

index.php

/contact/index.php

/contact/

/contact

http://mydomain.com/contact.php

(yes, it might actually be an absolute link, not a relative link!)

 

My question is, how in the world could I do this? Or better yet, is there a function that already does this?? I need some guidance/ ideas please!! :examine::anim_reading::confused:

Ok, here is what I have so far:

 

<?php

function makeglobalurl($targeturl){
global $url; //this is the page where "target url" was scraped from
$parsetarget = parse_url($targeturl);
$parseurl = parse_url($url);
if(checkstring($targeturl, "http")){//checkstring is a function I made, modified from strpos
	return $targeturl;
}else{
	$targeturl = http_build_url($url,
    array(
        "scheme" => $parseurl[scheme],
        "host" => $parseurl[host],
        "path" => $parsetarget[path],
        "query" => $parsetarget[query]
    ),
    HTTP_URL_STRIP_AUTH | HTTP_URL_JOIN_PATH | HTTP_URL_JOIN_QUERY | HTTP_URL_STRIP_FRAGMENT
);
}


//make it global
return $targeturl;
}

?>

 

My problem is when I run it, I get: Fatal error: Call to undefined function http_build_url()

 

Anyone know how I can define the function? Here is where I got it from- http://www.php.net/manual/en/function.http-build-url.php

 

So what do you guys think of my function so far? what is wrong with it!!

<?php
function abslink($files,$base='http://www.example.com') { // Change 'http://www.example.com' to your website
	$base = substr($base,-1,1) == '/' ? substr($base,0,-1) : $base;
	$files = is_array($files) ? $files : array($files);
	foreach($files as $file) {
		$file_path = parse_url($file,PHP_URL_PATH);
		$file_path = substr($file_path,0,1) == '/' ? $file_path : '/' . $file_path;
		print $file_path;
		$links[] = $base . $file_path;
	}
	return $links;
}
var_dump(abslink('/test/test.php'));
var_dump(abslink('http://www.example.com/test/test.php'));
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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