janlindso
janlindso

Reputation: 1253

How to output url without domain

When I'm using wp_get_attachment_url( $name->name ) it uses the full URL (ie. http://domain.com/files/file.zip). It seems that this doesn't work for my use, as I only want to get the URL without the domain (just the subpath).

It works just fine if I manually enter files/file.zip in the code. How can I format the URL from wp_get_attachment to get the URL in this format, without the domain name?

Upvotes: 0

Views: 4900

Answers (2)

Cheery
Cheery

Reputation: 16214

echo ltrim(parse_url('http://domain.com/files/file.zip', PHP_URL_PATH), '/');

or

echo substr(parse_url('http://domain.com/files/file.zip', PHP_URL_PATH), 1);

Upvotes: 3

Trevor Hutto
Trevor Hutto

Reputation: 2142

You can try to use a regex and test it like this...

<?php
$string = 'http://domain.com/files/file.zip';
$pattern = '/\/([\w+\/]+\.\w+)$/';
$replacement = '$1';
echo preg_replace($pattern, $replacement, $string);
?>

Upvotes: 1

Related Questions