jeffkas Posted January 24, 2011 Share Posted January 24, 2011 Hi all. Is there a function.. or easy way.. to completely remove a line from my description? For example: I want to remove any instances of <img src= * /> anywhere it is found in $description. That is.. anything in between the beginning tag and end tag, I want removed. Thank you! Quote Link to comment Share on other sites More sharing options...
mikecampbell Posted January 24, 2011 Share Posted January 24, 2011 Try preg_replace() .... http://ca2.php.net/manual/en/function.preg-replace.php Something like this: $text = preg_replace('/<img [^>]*>/', '', $text); Quote Link to comment Share on other sites More sharing options...
jeffkas Posted January 24, 2011 Author Share Posted January 24, 2011 preg_replace looks to be just what I need. However, as I'm stripping this from an rss feed, the syntax is slightly different and I'm having trouble making it work. I've looked up preg_replace on a couple sites but it's a bit confusing! Here's an example of a line I'm trying to remove: <img src="http://someurl.com/img1.gif" /> So I'm trying this: $text = preg_replace('<img src=" [^>]*/>', '', $text); No luck yet. Can you spot what I'm doing wrong? Error: Unknown modifier 'q' in ... Thanks again. Quote Link to comment Share on other sites More sharing options...
jeffkas Posted January 24, 2011 Author Share Posted January 24, 2011 I'm much closer. $text = preg_replace('/<img [^>]*\/>/', '', $text); works, but... It will parse all the page code and take everything out in $text until it reaches the very last closing tag (removing a couple paragraphs I want to keep). I want it to only remove what it finds up to the img closing tag. Thoughts? Quote Link to comment Share on other sites More sharing options...
mikecampbell Posted January 24, 2011 Share Posted January 24, 2011 The problem is the [^>]* bit, which tells the regex to keep reading characters that are NOT >. Since you don't have > in your text, it just keeps reading characters. To solve this, I believe you will need to use a lookahead assertion. http://www.php.net/manual/en/regexp.reference.assertions.php $text = preg_replace('/<img .*(?!\/>)\/>/', '', $text); Lookaheads will make the regex much slower. Maybe there is a better way to do this. Quote Link to comment Share on other sites More sharing options...
codefossa Posted January 24, 2011 Share Posted January 24, 2011 The problem is the [^>]* bit, which tells the regex to keep reading characters that are NOT >. Since you don't have > in your text, it just keeps reading characters. To solve this, I believe you will need to use a lookahead assertion. http://www.php.net/manual/en/regexp.reference.assertions.php $text = preg_replace('/<img .*(?!\/>)\/>/', '', $text); Lookaheads will make the regex much slower. Maybe there is a better way to do this. Is there a reason it can't be decoded then worked with? Quote Link to comment Share on other sites More sharing options...
jeffkas Posted January 25, 2011 Author Share Posted January 25, 2011 Not sure what you mean by decoding it. I'm pretty new to php. I'm just trying to clean this up from an rss feed. Be glad to give it a try though it you point me in the right direction. Quote Link to comment Share on other sites More sharing options...
requinix Posted January 25, 2011 Share Posted January 25, 2011 *coughungreedy* #<img .*?/># Quote Link to comment Share on other sites More sharing options...
mikecampbell Posted January 25, 2011 Share Posted January 25, 2011 *coughungreedy* #<img .*?/># Knew there was a better way. Quote Link to comment Share on other sites More sharing options...
jeffkas Posted January 25, 2011 Author Share Posted January 25, 2011 *coughungreedy* #<img .*?/># Yes! Thank you to you and Mike. You both were a big help. Could you explain the # symbol? That surprised me since I don't see it in the code anywhere. Quote Link to comment Share on other sites More sharing options...
sasa Posted January 25, 2011 Share Posted January 25, 2011 or <?php $text = 'bla <img src="http://someurl.com/img1.gif" /> bla '; $text = htmlspecialchars_decode($text); $text = preg_replace('/<img [^>]*>/', '', $text); echo $text; ?> Quote Link to comment Share on other sites More sharing options...
jeffkas Posted January 25, 2011 Author Share Posted January 25, 2011 or <?php $text = 'bla <img src="http://someurl.com/img1.gif" /> bla '; $text = htmlspecialchars_decode($text); $text = preg_replace('/<img [^>]*>/', '', $text); echo $text; ?> Holy smokes! I spent the next 3 hours last night building an array to strip the above tag and the many others I was finding... with plans to continue today and then find this post and realize that... $text = htmlspecialchars_decode($text); was all that was needed! It took care of formatting everything! It's all good though... learned some new things. A huge thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.