Go Back   vb.org Archive > vBulletin 4 Discussion > vB4 General Discussions

Reply
 
Thread Tools Display Modes
  #1  
Old 01-24-2012, 05:30 AM
DonosOdD DonosOdD is offline
 
Join Date: Mar 2011
Posts: 121
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default How do I change words in post texts?

Hi everyone,



I wanted to have a wordfilter, to replace a censored word for a word I want, in all forum sections but one specific.

when I was looking online for how to solve this, I've found this link:
https://vborg.vbsupport.ru/showthread.php?t=219968

which contained this code:
PHP Code:
$word = array(
'word1',
'word2'
);

$link = array(
'<a href="/forums/link1.php">Word1</a>',
'<a href="/forums/link2.php">Word2</a>'
);

$this->post['message'] = str_ireplace($word$link$this->post['message']); 


So I changed it to:

PHP Code:
$censoredword = array(
'word1',
'word2'
);

$changedword = array(
'word3',
'word4'
);

$this->post['message'] = str_ireplace($censoredword$changedword$this->post['message']); 

I wonder if this code is right, where should I put it, and how would I make it work for all subforums but one.


Sorry for this silly question, but I don't know PHP.


Thanks in advance!
Reply With Quote
  #2  
Old 01-24-2012, 11:05 AM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

That code looks right - it will of course replace all occurences of 'word1' and 'word2' even if they are part of another word (or a bbcode, or a link, etc), so you have to be a little careful how it's used.

The linked post says you should create a new plugin using hook location postbit_display_complete and that code.
Reply With Quote
  #3  
Old 01-24-2012, 04:50 PM
DonosOdD DonosOdD is offline
 
Join Date: Mar 2011
Posts: 121
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thank you, however, it still tricks me in those 3 points:

1- how do I create a new plugin (and using this hook location)?


2-where is postbit_display_complete? (I haven't found it, so I thought it exists only in previous versions, not vb4)

3-how do I make it work for all forum sections but one? I thought using a simple if statement:

PHP Code:
if (forum_id <> protected_id) { php code 
but I do not know what's the name of the forum id variable.


Again, thanks in advance!
Reply With Quote
  #4  
Old 01-24-2012, 04:58 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Create a new plugin by going to Add New Plugin under Plugins & Products in the adminCP. Choose the hook location from the dropdown menu, enter a title so you'll remember what it does later, and paste the code in the large text area. Click the "Yes" radio button to enable it, and press "Save".

To have one forum be exempt, put this around the code:

Code:
if ($forum['forumid'] != X)
{
// insert code here
}

and of course replace X with the forum id.
Reply With Quote
  #5  
Old 01-24-2012, 06:31 PM
DonosOdD DonosOdD is offline
 
Join Date: Mar 2011
Posts: 121
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I've done it, it was very simple indeed, and worked without problems.

Thank you! Hope this question helps out other people too.
Reply With Quote
  #6  
Old 01-27-2012, 01:00 AM
DonosOdD DonosOdD is offline
 
Join Date: Mar 2011
Posts: 121
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Hello again

I've tried it and it was fine - until I decided to use the function preg_replace instead of str_replace. And what happened was, all the posts went blank (all characters removed).

I wanted to use preg_replace because it's better for general word replacing.

Any thoughts?

Thanks!
Reply With Quote
  #7  
Old 01-27-2012, 01:05 AM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

preg_replace returns NULL if there's an error, so my guess would be that you're passing it something that's not a valid pattern.

You could try something like this (for testing only):

Code:
$ret = preg_replace($pat, $rep, $this->post['message']);
if ($ret !== NULL)
   $this->post['message'] = $ret;
else
   $this->post['message'] .= '<BR /><BR />Pattern error: ' . preg_last_error();
Reply With Quote
  #8  
Old 01-27-2012, 05:55 AM
DonosOdD DonosOdD is offline
 
Join Date: Mar 2011
Posts: 121
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Yeah, tried it and got Pattern Error 0.

This is the code I'm using, trying to simply change "foo" to "boo".

PHP Code:
if ($forum['forumid'] != 2)

$pat = array(
'/\bfoo\b/i'
);

$rep = array(
' boo'
);

$ret preg_replace($pat$rep$this->post['message']);
if (
$ret !== NULL)
   
$this->post['message'] = $ret;
else
   
$this->post['message'] .= '<BR /><BR />Pattern error: ' preg_last_error();


I guess the problem is with the preg_replace parameters, I'm passing an array whilst I should have passed an string. Is that it? How do I solve it?


Thanks again!
Reply With Quote
  #9  
Old 01-27-2012, 08:18 AM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Well, slightly bad choice of error message wording on my part - 0 is actually 'no error', so it's apparently not a pattern error. [S]But what I don't understand is why you seem to be getting NULL returned if there's no error.[/S] (See below)

I tried your code in a test script and it works for me:

PHP Code:
<?php
$pat 
= array(
'/\bfoo\b/i'
);

$rep = array(
' boo'
);

$text " foo ";

$ret preg_replace($pat$rep$text);
if (
$ret !== NULL)
   echo 
"new text :" $ret;
else
   echo (
"preg_replace error: " preg_last_error());
and the output is:

Code:
new text : boo

ETA: OK, further testing, that error code returned from preg_last_error isn't very useful - I purposely put in a bad pattern and I got NULL as a return, but 0 from preg_last_error(), just like you did. So I guess you *do* have an error in one of your patterns. I assume what you posted is a simplified version of what you're actually using, so I guess you'll have to look them over carefully to check for bad pattern string syntax.
Reply With Quote
Благодарность от:
DonosOdD
  #10  
Old 02-04-2012, 04:25 AM
DonosOdD DonosOdD is offline
 
Join Date: Mar 2011
Posts: 121
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Hello again, and sorry for the late response (I haven't worked on the forum for some time).



Your answers were great, and I was able to do this:
PHP Code:
if ($forum['forumid'] != 2


    
$pat = array(
    
'f1',
        
'f2',
        
'f3',
        
'f4',
        
'f5',
        
'f6',
        
'f7',
        
'f8',
        
'f9',
        
'f10'
         
);
    

        
$rep = array(
    
'e1',
        
'e2',
        
'e3',
        
'e4',
        
'e5',
        
'e6',
        
'e7',
        
'e8',
        
'e9',
        
'e10'
         
);


    foreach (
$pat as &$value) {
        
$value "/\b" $value "\b/iu" ;
    }


    
$ret preg_replace($pat$rep$this->post['message']);
    if (
$ret !== NULL)
       
$this->post['message'] = $ret;


And it worked fine. However, my forum is in portuguese, and when I change something to a word with any accentuation, like ? or ?, the code changes nothing at all (I guess ret == null).



I'm bypassing it by writing the words without accentuation.

Any thoughts?


Thanks!


EDIT: It doesn't work. Well, it worked for a while, but after two tests and two F5s, it stopped working. I can't understand why. And I was using this exact code, with this silly words.
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 10:57 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04925 seconds
  • Memory Usage 2,287KB
  • Queries Executed 13 (?)
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
  • (3)bbcode_code
  • (6)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (2)pagenav_pagelink
  • (10)post_thanks_box
  • (1)post_thanks_box_bit
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (1)post_thanks_postbit
  • (10)post_thanks_postbit_info
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)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_postinfo_query
  • fetch_postinfo
  • 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
  • fetch_musername
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • post_thanks_function_fetch_thanks_bit_start
  • post_thanks_function_show_thanks_date_start
  • post_thanks_function_show_thanks_date_end
  • post_thanks_function_fetch_thanks_bit_end
  • post_thanks_function_fetch_post_thanks_template_start
  • post_thanks_function_fetch_post_thanks_template_end
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete