Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
regexp for removing non-local images
Old 07-11-2011, 04:32 PM regexp for removing non-local images
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
I've added a wysiwyg editor (CKEditor) on a guestbook I'm building, which among other things has insertable smiley images. When validating a submited entry form I run strip_tags() on the message. Ideally I would like to only allow <b>, <i> and <a> tags but I'm forced to allow <img> tags in order to not strip away smileys.

So, after running strip_tags(), I would like to go through the message to also strip away any <img> tags that is not a smiley. That is, strip away all images that have a src that doesn't point to the local folder where all the images are.

I recon this can be done with preg_replace() or similar functions, but regexp really isn't my strongest side
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is online now
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
 
Register now for full access!
Old 07-11-2011, 04:45 PM Re: regexp for removing non-local images
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
You could use DOM ( http://us2.php.net/manual/en/book.dom.php ) to parse out any images where the href attribute points to an external site.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 07-11-2011, 05:08 PM Re: regexp for removing non-local images
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Why not just use the native BBCode extension or the Pear HTML_BBCodeParser??
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 07-11-2011, 07:41 PM Re: regexp for removing non-local images
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Thanks for your replies. I looked in to the DOM and the native BBCode extension, but I found them too big to dig into just for this small, one-time application. And I'd prefer not to drag a big framework like Pear into this, again, just for this small application.

Nevertheless, after several hours of trial and error I managed to get it working just the way I wanted using some regex functions. I used preg_replace_callback() with the pattern "|<img[^>]*>|i" to find all <img> tags, and a callback function in which I used preg_find_all() with the pattern "|([a-z]+)=\"[^\"]*\"|i" to find all attributes. I went through all the attributes and stripped away those that I will not allow and also checked the src attribute, to see if it points to a local file. And if it doesn't I'll return and empty string which will then replace the entire <img> tag.
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.

Last edited by lizciz; 07-11-2011 at 07:43 PM.. Reason: Typo
lizciz is online now
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Reply     « Reply to regexp for removing non-local images
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.19954 seconds with 12 queries