vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB4 General Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=251)
-   -   How do I change words in post texts? (https://vborg.vbsupport.ru/showthread.php?t=277456)

DonosOdD 01-24-2012 05:30 AM

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!

kh99 01-24-2012 11:05 AM

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.

DonosOdD 01-24-2012 04:50 PM

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!

kh99 01-24-2012 04:58 PM

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.

DonosOdD 01-24-2012 06:31 PM

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

Thank you! Hope this question helps out other people too.

DonosOdD 01-27-2012 01:00 AM

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!

kh99 01-27-2012 01:05 AM

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();


DonosOdD 01-27-2012 05:55 AM

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!

kh99 01-27-2012 08:18 AM

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.

DonosOdD 02-04-2012 04:25 AM

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.


All times are GMT. The time now is 01:19 PM.

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.01124 seconds
  • Memory Usage 1,774KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (3)bbcode_code_printable
  • (6)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (2)pagenav_pagelink
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (10)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.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/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.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
  • printthread_start
  • pagenav_page
  • pagenav_complete
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete