Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
  #1  
Old 12-08-2009, 11:06 PM
mme42 mme42 is offline
 
Join Date: Feb 2009
Posts: 101
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Help with simple regex filter

I'm trying to write a simple plug to filter links out from certain domains. I'd like to replace the entire link with a message whether it's a hyperlinked, enclosed in CODE bb tags, or simply typed. But, I'm a newb to php let alone regex. I've tried a number of different variations and always seem to get parse errors. The only time I had it working at all was with one domain and basically no regex in a way that it might as well have just been using an str_replace. But, that doesn't work because I'd like to filter out the whole link rather than just the domain name.

Here's something similar to what I'm looking for (though this doesn't work):

PHP Code:
$filterthese = array('domain1''domain2''domain3');
$replacement 'LINKS HAVE BEEN FILTERED';
$regex = array('/^http+$filterthese+(rar|zip|html)$/i','/^<a+$filterthese+<\/a>$/i');
$this->post['message'] = preg_replace($regex$replacement$this->post['message']); 
Can anybody clue me on whatever (probably obvious to somebody with more experience) mistake that I'm making?

I'd also add in some conditionals for which forums it filters etc. But, for now, I'd just like to get the filter part working.

Thanks

EDIT: Ok, I got rid of the parse errors for now. But, the replacement isn't working. I suspect that there is something that I'm not understanding about regexes, but I'm not sure where I'm messing up. Basically, I want a regex that would find this string:

1. Starts with http
2. Could contain any number of any characters before the domain/word
3. Has the domain somewhere in the middle
4. Could any number of any characters after
5. Ends with a number of extentions such as (html|htm|rar|zip|001)

I have a feeling that it's numbers 2 and 4 that are tripping me up. I now have this which I thought might work, but it doesn't:

PHP Code:
$filterthese = array('domain1''domain2''domain3');
$replacement 'LINKS HAVE BEEN FILTERED';
$regex = array('!^(http)+(.*)?($filterthese)+(.*)?(html|htm|rar|zip|001)$!i');
$this->post['message'] = preg_replace($regex$replacement$this->post['message']); 

Or maybe I'm not understanding regexes whatsoever I don't know.

EDIT 2:
The more I've tried and asked around, this doesn't seem so "simple" anymore. And, I'm not sure that I'd even be doing this the best way even if I got the REGEX right. So, for now, I've decided to just start with a str_replace plug (filter just the domain/host name) to get the job done even if it's a bit dirty. Though, I do plan to look into replacing the entire URL eventually. So, any ideas are certainly still welcome.
Reply With Quote
Reply

Thread Tools
Display Modes

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

Forum Jump


All times are GMT. The time now is 11:00 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.03318 seconds
  • Memory Usage 2,153KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (2)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)post_thanks_box
  • (1)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (1)post_thanks_postbit_info
  • (1)postbit
  • (1)postbit_onlinestatus
  • (1)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete