MikeDXUNL Posted June 13, 2008 Share Posted June 13, 2008 say I have this stored in a variable: $var = "<img src="/NR/somefolder/onemorefolder/thisimage.jpg" alt="Some ALT Text" width="85" height="120" />"; how can I remove the tag to retrieve only: /NR/somefolder/onemorefolder/thisimage.jpg Link to comment https://forums.phpfreaks.com/topic/110119-removing-img-tags/ Share on other sites More sharing options...
Dragoa Posted June 13, 2008 Share Posted June 13, 2008 This is pretty dirty, but it works provided they're all formatted the same way. $words = explode('=',$var); $words2 = explode('"',$words[1]); echo $words2[1]; No idea if there's an easier/better way of doing it. Link to comment https://forums.phpfreaks.com/topic/110119-removing-img-tags/#findComment-565123 Share on other sites More sharing options...
DarkWater Posted June 13, 2008 Share Posted June 13, 2008 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 More sharing options...
DarkWater Posted June 13, 2008 Share Posted June 13, 2008 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 More sharing options...
DarkWater Posted June 13, 2008 Share Posted June 13, 2008 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.