anarchoi Posted March 8, 2008 Share Posted March 8, 2008 what syntax can i use with str_replace to delete ALL images tags in a variable? i'd like to take out all <IMG SRC='""> but there is a lot and the url vary, and there are often others tags like WIDTH="" and HEIGHT="" could anyone give me the syntax to use? thanks Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 9, 2008 Share Posted March 9, 2008 I would suggest preg_replace() instead. $new_text = preg_replace('/<img.*?>/', '', $old_text); Quote Link to comment Share on other sites More sharing options...
effigy Posted March 10, 2008 Share Posted March 10, 2008 /<img[^>]*>/ Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 10, 2008 Share Posted March 10, 2008 @effigy, Our RegEx's appear to do the exact same thing, but using different logic. After reviewing yours it appears more elegant and easier to understand. However, I'm curious, did you see a flaw in my expression or did you just want to post your version? Quote Link to comment Share on other sites More sharing options...
effigy Posted March 10, 2008 Share Posted March 10, 2008 My expression is more efficient in theory. It's best to use greediness when you can expect a certain format; for instance, it's impossible for an open HTML tag to contain another (e.g., <img <h1>x</h1>>) and, knowing this, we can tell the engine to race forward gobbling non ">"s, rather than checking at every step along the way to make sure a ">" is not ahead. As a general rule, when you can, be as specific as possible. Greedy quantifiers and negated characters classes are typically more efficient and internally optimized compared to lazy quantifiers. My apologies for not being as informative up front. Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 10, 2008 Share Posted March 10, 2008 Thanks for the elaboration. A quick test verified your position. It's not that I don't believe you - I just always test something, if possible, instead of blindly accepting. Running through 10,000 itterations of both methods gave varrying results, but the latter method was approximately 50% faster: 0.1 seconds vs. .05 seconds. Considering that is for 10,000 itterations the difference may not seem relevant. But, for sites with a large amount of traffic every little bit adds up. Thanks again. 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.