Jump to content

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


physaux

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'));
?>

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.