* 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."

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to