Return to Snippet

Revision: 4255
at November 14, 2007 05:00 by gandalf


Initial Code
<?php

function get_images($file,$url){
$regs=array();
$images=array();
preg_match_all('/<\s?img(.*?)>/i',$file,$regs);
foreach($regs[0] as $key=>$value){
$items = explode('=',$value);
$count = count($items);
for($i=0;$i<$count;$i++){
$item = str_replace(strrchr($items[$i+1],' '),'',$items[$i+1]);
$item = trim(preg_replace('/("|\')/','',$item));
if(preg_match('/src/i',$items[$i])){
if(trim($item)!=''){
if(preg_match('/((.*)(\.gif|\.jpeg|\.jpg|\.png))(.*)/',$item,$reg)){
$images[$key]['src']=find_true_path($url,$reg[1]);
}
}else{
continue;
}
}
elseif(preg_match('/alt/i',$items[$i])){
if($item!=''){
if(isset($images[$key]['src'])){
$images[$key]['alt']=$item;
}else{
continue;
}
}else{
continue;
}
}
elseif(preg_match('/title/i',$items[$i])){
if($item!=''){
if(isset($images[$key]['src'])){
$images[$key]['title']=preg_replace('/[\"\']/','',$item);
}else{
continue;
}
}else{
continue;
}
}else{
continue;
}
}
}
return($images);
}

function find_true_path($url,$image_location){
if(preg_match('/http/',$image_location)){
return($image_location);
}else{
$true_location='';

if(substr($image_location,0,2)=='./'){
$image_location=substr($image_location,2);
}
elseif(substr($image_location,0,1)=='/'){
$image_location=substr($image_location,1);
}else{
$image_location=$image_location;
}
$back_folder_count = substr_count($image_location,'../');
$url = substr($url,0,strrpos($url,'/'));
for($i=0;$i<$back_folder_count;$i++){
if(strrpos($url,'/')==strlen($url)-1){
$url = substr($url,0,-1);
$url = substr($url,0,strrpos($url,'/'));
}else{
$url = substr($url,0,strrpos($url,'/'));
}
}
if(substr($url,-1)!='/'){
$url = $url.'/';
}
$image_location = preg_replace('/(\.\.\/)/','',$image_location);
$true_location = $url.$image_location;
return($true_location);
}
}
?>

Initial URL


Initial Description
These two functions allow you to remove images from html page.
The function returns an array of images with the absolute src and,if there are any, the alt and title attributes.
Arguments to pass to get_images() are $file = html source and $url = the page url.
enjoy

Initial Title
Remove images from html

Initial Tags
html, images

Initial Language
PHP