Re: Working out the image path...
| From: | Curt Zirzow | Date: | Mon, 11 Oct 2004 20:12:55 +0000 |
| Subject: | Re: Working out the image path... | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-199234@lists.php.net to get a copy of this message | ||
* Thus wrote Mag:
> Hey!
> Am having a little problem working out the logic in
> this, basically I pass a graphics path to a function
> and it should give/retrun me the URL path.
>
> <?php
> $url='http://x.com/t1/t2/t3/blah.html';
>
> function ret_url($rel_path)
> { }
>
> ?>
>
> so I pass a relitive url like this
> $a=ret_url('/blah.jpg';)
>...
>
> /blah.jpg // should return http://x.com/blah.jpg
>
> /imgs/blah.jpg // return http://x.com/imgs/ blah.jpg
Basic concating a file:
return $base_url . $rel_path;
>
> imgs/blah.jpg //return
> http://x.com/t1/t2/t3/imgs/blah.jpg
ensure first char is absolute:
if ($rel_path{0} != '/') {
$rel_path = '/' . $rel_path;
}
>
> ../imgs/blah.jpg // etc
>
> /../imgs/blah.jpg //etc
I tend to use str_replace to remove this.
/* remove .. and translate // to / */
$rel_path = str_replace(array('..', '//', array('', '/'),
$rel_path);
>
> http://some-site-blah.com/imgs/blah.jpg // return the
> same url
check for http://
if(substr($rel_path, 0, 6) == 'http://') )
return $rel_path;
Put those all together and you'll have your function.
Curt
--
Quoth the Raven, "Nevermore."