physaux Posted January 19, 2010 Share Posted January 19, 2010 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!! Quote Link to comment https://forums.phpfreaks.com/topic/189052-trying-to-make-a-function-to-convert-relative-links-into-absolute-links/ Share on other sites More sharing options...
laffin Posted January 19, 2010 Share Posted January 19, 2010 look into pathinfo() and parseurl() functions Quote Link to comment https://forums.phpfreaks.com/topic/189052-trying-to-make-a-function-to-convert-relative-links-into-absolute-links/#findComment-998175 Share on other sites More sharing options...
physaux Posted January 19, 2010 Author Share Posted January 19, 2010 thanks- looking into it now- wow its complicated.. I'll keep reading and check this if anyone comes up with something else Quote Link to comment https://forums.phpfreaks.com/topic/189052-trying-to-make-a-function-to-convert-relative-links-into-absolute-links/#findComment-998176 Share on other sites More sharing options...
physaux Posted January 19, 2010 Author Share Posted January 19, 2010 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!! Quote Link to comment https://forums.phpfreaks.com/topic/189052-trying-to-make-a-function-to-convert-relative-links-into-absolute-links/#findComment-998186 Share on other sites More sharing options...
laffin Posted January 19, 2010 Share Posted January 19, 2010 for that u need to install PECL extensions, which requires PEAR http_build_url (PECL pecl_http >= 0.21.0) Quote Link to comment https://forums.phpfreaks.com/topic/189052-trying-to-make-a-function-to-convert-relative-links-into-absolute-links/#findComment-998189 Share on other sites More sharing options...
physaux Posted January 19, 2010 Author Share Posted January 19, 2010 eee :-\ Ok well I have to contact my support people to install those modules.. Anyone have any better way to do it? Maybe faster?.. Quote Link to comment https://forums.phpfreaks.com/topic/189052-trying-to-make-a-function-to-convert-relative-links-into-absolute-links/#findComment-998194 Share on other sites More sharing options...
crabfinger Posted January 19, 2010 Share Posted January 19, 2010 <?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')); ?> Quote Link to comment https://forums.phpfreaks.com/topic/189052-trying-to-make-a-function-to-convert-relative-links-into-absolute-links/#findComment-998198 Share on other sites More sharing options...
physaux Posted January 19, 2010 Author Share Posted January 19, 2010 Ah thank you that works great!! Quote Link to comment https://forums.phpfreaks.com/topic/189052-trying-to-make-a-function-to-convert-relative-links-into-absolute-links/#findComment-998199 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.