Trim img off a content

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
lauthiamkok
Forum Contributor
Posts: 153
Joined: Wed Apr 01, 2009 2:23 pm
Location: Plymouth, United Kingdom

Trim img off a content

Post by lauthiamkok »

Hi,
How can I trim the img tag ONLY off a content? This is the code i tried but doesnt work...

Code: Select all

<?php
$source = '<img src="pic1.jpg"/><a href="link1.html">Duis autem </a> vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, <img src="pic2.jpg"/>vel illum dolore eu <a href="link1.html">feugiat</a> nulla facilisis ...'; 
 
$trimmed = trim($source, '/<img src="([^>]*)"\/>/si');
echo $trimmed;
?>
thanks,
Lau
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Trim img off a content

Post by superdezign »

lauthiamkok wrote:

Code: Select all

$trimmed = trim($source, '/<img src="([^>]*)"\/>/si');
trim() uses plaintext, not regex. What you want is preg_replace().
lauthiamkok
Forum Contributor
Posts: 153
Joined: Wed Apr 01, 2009 2:23 pm
Location: Plymouth, United Kingdom

Re: Trim img off a content

Post by lauthiamkok »

superdezign wrote:
lauthiamkok wrote:

Code: Select all

$trimmed = trim($source, '/<img src="([^>]*)"\/>/si');
trim() uses plaintext, not regex. What you want is preg_replace().
sorry i meant to say - remove the img tags from the a content....
i will try preg_replace thanks.
lauthiamkok
Forum Contributor
Posts: 153
Joined: Wed Apr 01, 2009 2:23 pm
Location: Plymouth, United Kingdom

Re: Trim img off a content

Post by lauthiamkok »

superdezign wrote:
lauthiamkok wrote:

Code: Select all

$trimmed = trim($source, '/<img src="([^>]*)"\/>/si');
trim() uses plaintext, not regex. What you want is preg_replace().
it wont remove the img tags from my contents... here is one of my contents from a database...

Code: Select all

<p><strong>Roll-up, roll-up, roll-up....</strong> in celebration of the launch of my first blog I feel it's fitting to give something away! Below are pictures of the t-shirt I designed for the guys at My Yard in Bristol and I will put two up for grabs in this competition. What you need to do to win is leave a comment on this post and I'll pick two email addresses at random and ship one out to each winner completely free of charge.<br /><br />I have one medium and one small so you need to leave your preferred size on your comment.<br /><br />So to recap, here's what you need to do:<br />&bull; Click make a comment at the bottom of this post<br />&bull; Verify your email address<br />&bull; Leave a comment stating your preferred size and any other message you like<br />&bull; Wait to see if you have won<br /><br />The competition will close at <strong>12.00 GMT on the 21st of May</strong> and I will announce the winner on the blog as soon as I receive a reply from the two randomly selected emails.<br /><br />Good Luck & have cracking day!<br />Tom (Ginger Monkey)</p>
 
<p><img title="161_2009_04_30_154038.jpg" src="img_contenties/161_2009_04_30_154038.jpg" alt="161_2009_04_30_154038.jpg" width="500" height="332" /></p>
<p><img title="160_2009_04_30_154002.jpg" src="img_contenties/160_2009_04_30_154002.jpg" alt="160_2009_04_30_154002.jpg" width="500" height="325" /></p>
<p><img title="162_2009_04_30_154151.jpg" src="img_contenties/162_2009_04_30_154151.jpg" alt="162_2009_04_30_154151.jpg" width="500" height="332" /></p>
it still display the same even though i have used preg_replace,

Code: Select all

$content = preg_replace('/<img src="([^>]*)" title="([^>]*)" alt="([^>]*)"\/>/si', '', $row_feeds['pg_content']);
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Trim img off a content

Post by requinix »

Code: Select all

<img title="..." src="..." alt="..." width="..." height="..." />

Code: Select all

<img src="([^>]*)" title="([^>]*)" alt="([^>]*)"/>
preg_replace is very strict. For instance, if you say the src comes before the title which comes before the alt which is the last thing in the tag, then preg_replace will only do the replacement if that's exactly how it shows up. In your text, title comes first, then src, then alt, then two more attributes your expression doesn't even mention, and then the end of the tag.

If you want to replace all <img>s then try something less specific. What's the simplest way to explain how an image tag looks?
Answer: "<img ", then a bunch of stuff - but not too much - then a ">".
While you're writing an expression for that, use .*? and not .*.
lauthiamkok
Forum Contributor
Posts: 153
Joined: Wed Apr 01, 2009 2:23 pm
Location: Plymouth, United Kingdom

Re: Trim img off a content

Post by lauthiamkok »

tasairis wrote:

Code: Select all

<img title="..." src="..." alt="..." width="..." height="..." />

Code: Select all

<img src="([^>]*)" title="([^>]*)" alt="([^>]*)"/>
preg_replace is very strict. For instance, if you say the src comes before the title which comes before the alt which is the last thing in the tag, then preg_replace will only do the replacement if that's exactly how it shows up. In your text, title comes first, then src, then alt, then two more attributes your expression doesn't even mention, and then the end of the tag.

If you want to replace all <img>s then try something less specific. What's the simplest way to explain how an image tag looks?
Answer: "<img ", then a bunch of stuff - but not too much - then a ">".
While you're writing an expression for that, use .*? and not .*.
got it thanks!

.*? is my choice thanks :mrgreen:
Post Reply