Hi All,
At the moment I am trying to resolve paths of products for a price comparison site. Problem is, when products are added, and the provider does not provide a product stream, I need to parse the page, and retieve the info from there.
I how have all the images on a page in an array, and am trying to resolve the full path to image. So I wrote the function below, but I am not sure whether I am setting myself up for a big fall.
I cannot get the regexp to work properly; when I insert a file like '/path/to/file.jpg' it is not matched by the expression '^\//'
Any suggestions?
function image_path_and_size($image, $url, $dimensions=array(350,300))
{
/*
parameters:
$image: The image url we want to resolve
$url: Url of product page
$dimensions: Preferred dimensions. Return rankingvalue
Path options:
./[info] = Within directory path
../[info] = Relative, in higher level path
http://..[info] = Absolute path
[info = Within directory path
*/
$pp = parse_url($url);
$patterns = array(
'/^\//',
'/^\.\//',
'/^http/');
$replace = array(
$pp['scheme'].$pp['host'],
$pp['scheme'].$pp['host'].$pp['path'],
$pp['scheme']);
if(! preg_replace($patterns, $replace, $image))
{
// Explode the path
$imgpcs = explode('/',$image);
$i = 0;
$image = '';
foreach($imgpcs as $pc)
{
if($pc == '..')
{
$i++;
}
else
{
if($i == 0)
{
$image .= '/'.$pc;
}
else
{
$i--;
}
}
}
}
return $image;
}