nuttycoder Posted December 12, 2008 Share Posted December 12, 2008 I'm trying to strip out all tags from an image tag leaving just the image source behind. This is to be used on a dynamic stylesheet the following code is stored in a database: <p><img src='assets/editor/uploaded/bg.png' alt='' width='15' height='15' /></p> Here's what I'm using: <?php // strip out everything but the image path I want the final out come to be: assets/editor/uploaded/bg.png $src = "<p><img src='assets/editor/uploaded/bg.png' alt='' width='15' height='15' /></p>"; $pattern[0] = "/\<p>(.*?)\<\/p\>/is"; $pattern[1] = "/\<img src=\"(.*?)\"/is"; $pattern[2] = "/\width=\"(.*?)\"/is"; $pattern[3] = "/\height=\"(.*?)\"/is"; $pattern[4] = "/\alt=\"(.*?)\"/is"; $replace[0] = "$1"; $replace[1] = "$1"; $replace[2] = ""; $replace[3] = ""; $replace[4] = ""; $output = preg_replace($pattern, $replace, $src); //print result echo $output; // result is: <img src='assets/editor/uploaded/bg.png' alt='' width='15' height='15' /> ?> Can't seem to get it to work hope someone can point me in the right direction. Link to comment https://forums.phpfreaks.com/topic/136730-solved-strip-out-image-attributes-using-preg_replace/ Share on other sites More sharing options...
nrg_alpha Posted December 12, 2008 Share Posted December 12, 2008 I'm not sure I follow.. you haven't provided a before AND after sample...(no offense..most people do not know how to accurately describe what they are looking for.. that is why it is always advisable to show a sample of what you start with [which you did], and what you want it to be afterwards.. If you can provide the 'after' part of the sample: <p><img src='assets/editor/uploaded/bg.png' alt='' width='15' height='15' /></p> that would help out alot. Link to comment https://forums.phpfreaks.com/topic/136730-solved-strip-out-image-attributes-using-preg_replace/#findComment-714065 Share on other sites More sharing options...
nuttycoder Posted December 12, 2008 Author Share Posted December 12, 2008 okay the after part I'd like it to get just the image path excluding any height,wide,alt attributes like: assets/editor/uploaded/bg.png But when I run the code I get back <img src='assets/editor/uploaded/bg.png' alt='' width='15' height='15' /> This will be apart of a dynamic stylesheet so I need the image path but none of the attributes. Link to comment https://forums.phpfreaks.com/topic/136730-solved-strip-out-image-attributes-using-preg_replace/#findComment-714073 Share on other sites More sharing options...
nrg_alpha Posted December 12, 2008 Share Posted December 12, 2008 Well, using preg_replace, (and assuming the image tag is always nested within the p tag): $str = "<p><img src='assets/editor/uploaded/bg.png' alt='' width='15' height='15' /></p>"; $str = preg_replace('#<p>.+?src=[\'"]([^\'"]+)[\'"].+</p>#i', "$1", $str); echo $str; Output: assets/editor/uploaded/bg.png Link to comment https://forums.phpfreaks.com/topic/136730-solved-strip-out-image-attributes-using-preg_replace/#findComment-714086 Share on other sites More sharing options...
nuttycoder Posted December 12, 2008 Author Share Posted December 12, 2008 That works great Thanks very much. Link to comment https://forums.phpfreaks.com/topic/136730-solved-strip-out-image-attributes-using-preg_replace/#findComment-714090 Share on other sites More sharing options...
nrg_alpha Posted December 12, 2008 Share Posted December 12, 2008 Excellent EDIT - In hind sight.. best to use this version instead.. $str = preg_replace('#<p>.+?src=[\'"]([^\'"]+)[\'"].*</p>#i', "$1", $str); Note the star in .*</p> instead of a +. This version would work in the event there is nothing between the src quotes and the closing p tag (which I assume would not be the case..but would still not hurt). Link to comment https://forums.phpfreaks.com/topic/136730-solved-strip-out-image-attributes-using-preg_replace/#findComment-714094 Share on other sites More sharing options...
mraza Posted November 3, 2009 Share Posted November 3, 2009 sorry for bumping so old topic: Sir @nrg_alpha about exactly something i am looking for but what if i don't have <p> tag around what code it should be thanks for the help. Link to comment https://forums.phpfreaks.com/topic/136730-solved-strip-out-image-attributes-using-preg_replace/#findComment-950485 Share on other sites More sharing options...
nrg_alpha Posted November 3, 2009 Share Posted November 3, 2009 sorry for bumping so old topic: Sir @nrg_alpha about exactly something i am looking for but what if i don't have <p> tag around what code it should be thanks for the help. Wow.. a thread nearing 1 year old, bumped back from the grave. So what you are looking for is to grab the contents of the src attribute? If you could provide a sample string and what exactly you are looking for, this would help out. Link to comment https://forums.phpfreaks.com/topic/136730-solved-strip-out-image-attributes-using-preg_replace/#findComment-950595 Share on other sites More sharing options...
mraza Posted November 4, 2009 Share Posted November 4, 2009 haha yes sir here the post where i describe exactly what's going on. http://www.phpfreaks.com/forums/index.php/topic,275471.0.html Thanks for any help Link to comment https://forums.phpfreaks.com/topic/136730-solved-strip-out-image-attributes-using-preg_replace/#findComment-950830 Share on other sites More sharing options...
nrg_alpha Posted November 4, 2009 Share Posted November 4, 2009 Seems you got your answer in that thread (which is solved). If not, then post the newer problem either in that thread (since it's much newer and more relevant to your problem) or start a new thread. Link to comment https://forums.phpfreaks.com/topic/136730-solved-strip-out-image-attributes-using-preg_replace/#findComment-951086 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.