Jump to content

Removing IMG tags


MikeDXUNL

Recommended Posts

class ImageTags {
   protected static $src;
   public function parse($string) {
           if (preg_replace('/<img src="(.+)"(.*) \/>/Usie', "ImageTags::StoreSource('$1')", $string)) {
                         return TRUE;
           }
           else {
                        return FALSE;
          }
   }
   public static function StoreSource($src) {
         self::src[] = $src;
         reset($src);
   }
   public function getSource($elem=NULL) {
          if ($elem == NULL) {
               $currsrc = current($this->src);
               next($this->src);
              return $currsrc;
          }
         else {
              return $this->src[$elem];
         }
   }
}

 

At least that's how I'd probably do it.           

Usage:

$imgtag = new ImageTags();

if ($imgtag->parse($var)) {

       $src = $imgtag->getSource();

}

else {

       echo "\$var contained no img tags.";

}

 

EDIT: Forgot next() in the getSource function.

EDIT 2: Fixed missing function declaration and error in static function. =X

EDIT 3: Wow, another typo.  I'm so tired. =X

Link to comment
https://forums.phpfreaks.com/topic/110119-removing-img-tags/#findComment-565128
Share on other sites

LOL found another mistake.  And it won't let me edit that post.  Sorry.  Here you go:

EDIT AGAIN: Finally fixed. Sorry about that.

 

class ImageTags {
    protected static $src = array();
    public function parse($string) {
            if (preg_replace('/<img src="(.+)"(.*)\/>/sie', "ImageTags::StoreSource('$1')", $string)) {
                          return TRUE;
            }
            else {
                         return FALSE;
           }
    }
    public static function StoreSource($src) {
          self::$src[] = $src;
          reset(self::$src);
          return $src;
    }
    public function getSource($elem=NULL) {
           if ($elem == NULL) {
               $currsrc = current(self::$src);
               next(self::$src);
               return $currsrc;
           }
          else {
               return self::$src[$elem];
          }
    }
}

 

At least that's how I'd probably do it.           

Usage:

$imgtag = new ImageTags();

if ($imgtag->parse($var)) {

       $src = $imgtag->getSource();

}

else {

       echo "\$var contained no img tags.";

}

 

Link to comment
https://forums.phpfreaks.com/topic/110119-removing-img-tags/#findComment-565137
Share on other sites

You know, I'm actually going to make an HTML tag-parser class this weekend.  That ImageTags class now works properly.

 

I just did:

<?php
class ImageTags {
    protected static $src = array();
    public function parse($string) {
            if (preg_replace('/<img src="(.+)"(.*)\/>/Usie', "ImageTags::StoreSource('$1')", $string)) {
                          return TRUE;
            }
            else {
                         return FALSE;
           }
    }
    public static function StoreSource($src) {
          self::$src[] = $src;
          reset(self::$src);
          return $src;
    }
    public function getSource($elem=NULL) {
           if ($elem == NULL) {
               $currsrc = current(self::$src);
               next(self::$src);
               return $currsrc;
           }
          else {
               return self::$src[$elem];
          }
    }
}
$img = new ImageTags();
$var = '<img src="lol.png" alt="Rofl" />';
if ($img->parse($var)) {
      echo $img->getSource();
}
else {
      echo "Nope.";
}
echo "\n<br />";
?>

 

And it output "lol.png".

 

Link to comment
https://forums.phpfreaks.com/topic/110119-removing-img-tags/#findComment-565140
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.