Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: building a URL path

Tags:

php

I have a few strings to combine to build a full path. e.g.

$base = "http://foo.com";
$subfolder = "product/data";
$filename = "foo.xml";

// How to do this?
$url = append_url_parts($base, $subfolder, $filename); ???

String concatenation won't do, that would omit the necessary forward slashes.

In Win32 I'd use PathCombine() or PathAppend(), which would handle adding any necessary slashes between strings, without doubling them up. In PHP, what should I use?

like image 202
Graham Perks Avatar asked Sep 14 '10 16:09

Graham Perks


1 Answers

Try this:

$base = "http://foo.com";
$subfolder = "product/data";
$filename = "foo.xml";

function stripTrailingSlash(&$component) {
    $component = rtrim($component, '/');
}
$array = array($base, $subfolder, $filename);
array_walk_recursive($array, 'stripTrailingSlash'); 
$url = implode('/', $array);
like image 172
Jacob Relkin Avatar answered Nov 07 '22 07:11

Jacob Relkin