Strip out images

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Strip out images

Post by Addos »

I have a ‘home page’ that contains a few samples of a ‘news items’ that are taken from the main ‘news’ page within the site.

I need to remove any images that the client might add as they will look ok for the main ‘news’ pages but not on the home page. Is there any way I can strip out the images just for the home page? I have looked through the web but I can’t see or think of an easy way to do this. For example the path to these images would be <img style="margin: 5px; float: left;" src="news/art_small.jpg" alt="Art Image" /> and I can’t think of how to successfully remove this string as obviously it can change depending on the setting the client uses.

I use TinyMCE as the CMS editor and the images can be added by the client and tweaked from there. Maybe there is a regExp or similar but I’m really not sure.
Any ideas as to how I can approach this?
Thanks
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Strip out images

Post by jackpf »

It's a bit of a hack, but you could try this:

Code: Select all

preg_replace('/<img.*?>/is', null, $str);
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Re: Strip out images

Post by Addos »

Thanks very much. That worked very well and I appreciate your help.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Strip out images

Post by jackpf »

Awesome.
Post Reply