Re: Working out the image path...partly solved, please have a look at my code

From: Date: Mon, 11 Oct 2004 22:11:04 +0000
Subject: Re: Working out the image path...partly solved, please have a look at my code
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-199237@lists.php.net to get a copy of this message
> <?php > > $url='http://www.textx.com/t1/t2/t3/blah.html'; > > function ret_url($rel_path, $base = '') > { > $base_path = substr($base, 0, strpos($base, '/',7)); > > if(substr($rel_path,0,1)=='/' && > !strpos($rel_path,'/../')) > { return $base_path.$rel_path; } > > elseif(strpos($rel_path,'://') > 0) > { return $rel_path; } > > else { return > collapse(dirname($base).'/'.$rel_path); } > > return $rel_path; > } > > /* this function removes the /../ parts */ > function collapse($path) > { > > while(strpos($path,'/../') !== false) > {$p2 = ereg_replace('/([^/]*)/\.\./','/',$path); > if($p2==$path) > break; > $path = $p2; > } > > return $path; > } > > $uris = array(); > $uris[] = '/blah.jpg'; > $uris[] ='/imgs/blah.jpg'; > $uris[] ='imgs/blah.jpg'; > $uris[] ='../imgs/blah.jpg'; > $uris[] ='/../imgs/blah.jpg'; // ## not working ## > $uris[] ='/../../imgs/blah.jpg'; // ## not working ## > $uris[] ='http://some-site-blah.com/imgs/blah.jpg'; > > echo '<table border=1>'; > foreach($uris as $uri) > { > echo '<tr><td>'.htmlspecialchars($uri).'</ > td><td>'.htmlspecialchars(ret_url($uri,$url)).'</td></tr>'; > } > echo '</table>'; > ?> try this: function ret_url($rel_path, $base = '') { $base_path = substr($base, 0, strpos($base, '/',7)); if(substr($rel_path,0,1)=='/' && strpos($rel_path,'/../') === false) { return $base_path.$rel_path; } elseif(strpos($rel_path,'://') > 0) { return $rel_path; } else { return collapse(dirname($base).'/'.$rel_path); } return $rel_path; }

« previous php.general (#199237) next »