Remove images from html


/ Published in: PHP
Save to your folder(s)

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


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. function get_images($file,$url){
  4. $regs=array();
  5. $images=array();
  6. preg_match_all('/<\s?img(.*?)>/i',$file,$regs);
  7. foreach($regs[0] as $key=>$value){
  8. $items = explode('=',$value);
  9. $count = count($items);
  10. for($i=0;$i<$count;$i++){
  11. $item = str_replace(strrchr($items[$i+1],' '),'',$items[$i+1]);
  12. $item = trim(preg_replace('/("|\')/','',$item));
  13. if(preg_match('/src/i',$items[$i])){
  14. if(trim($item)!=''){
  15. if(preg_match('/((.*)(\.gif|\.jpeg|\.jpg|\.png))(.*)/',$item,$reg)){
  16. $images[$key]['src']=find_true_path($url,$reg[1]);
  17. }
  18. }else{
  19. continue;
  20. }
  21. }
  22. elseif(preg_match('/alt/i',$items[$i])){
  23. if($item!=''){
  24. if(isset($images[$key]['src'])){
  25. $images[$key]['alt']=$item;
  26. }else{
  27. continue;
  28. }
  29. }else{
  30. continue;
  31. }
  32. }
  33. elseif(preg_match('/title/i',$items[$i])){
  34. if($item!=''){
  35. if(isset($images[$key]['src'])){
  36. $images[$key]['title']=preg_replace('/[\"\']/','',$item);
  37. }else{
  38. continue;
  39. }
  40. }else{
  41. continue;
  42. }
  43. }else{
  44. continue;
  45. }
  46. }
  47. }
  48. return($images);
  49. }
  50.  
  51. function find_true_path($url,$image_location){
  52. if(preg_match('/http/',$image_location)){
  53. return($image_location);
  54. }else{
  55. $true_location='';
  56.  
  57. if(substr($image_location,0,2)=='./'){
  58. $image_location=substr($image_location,2);
  59. }
  60. elseif(substr($image_location,0,1)=='/'){
  61. $image_location=substr($image_location,1);
  62. }else{
  63. $image_location=$image_location;
  64. }
  65. $back_folder_count = substr_count($image_location,'../');
  66. $url = substr($url,0,strrpos($url,'/'));
  67. for($i=0;$i<$back_folder_count;$i++){
  68. if(strrpos($url,'/')==strlen($url)-1){
  69. $url = substr($url,0,-1);
  70. $url = substr($url,0,strrpos($url,'/'));
  71. }else{
  72. $url = substr($url,0,strrpos($url,'/'));
  73. }
  74. }
  75. if(substr($url,-1)!='/'){
  76. $url = $url.'/';
  77. }
  78. $image_location = preg_replace('/(\.\.\/)/','',$image_location);
  79. $true_location = $url.$image_location;
  80. return($true_location);
  81. }
  82. }
  83. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.