![]() |
Trouble with a function I am using
Folks, I am having some problems with a function I am using and I would appreciate your help.
Let me explain myself. I have a function which enters a pair of brackets around a censored word, if such a word is found in a post. This function is shown below: Code:
function bracket_censored_text($text) I have the following words (abbreviations) in my censored words list: bs, b.s., BS, B.S. Now if I have a post with the word "best" in it, everything works fine. But if in the same post, there is one of the above abbreviations, then the abbreviation is put in square brackets, but so is the word "best", which is actually replaced by "[b.s.]". So this: Code:
This is the best world. Code:
This is the best world in the B.S. universe. Even though I can work with PHP, I have absolutely no idea how the preg_ functions work, so I am at a loss on how to correct this problem. So if one of you experts can help me out, I would greatly appreciate it. |
The dots in your censor word b.s. are being interpreted as the "any single character" expression, which is a dot (.) in the regular expression. What you need to do is escape any characters that have a special meaning for regexes, in your $censorword, before calling preg_replace In PHP, you can use preg_quote to do this.
Code:
$text = preg_replace('#(?<=[^a-z]|^)(' . preg_quote($censorword, '#') . ')(?=[^a-z]|$)#si', "[$censorword]", $text); |
Analogpoint, you are a life-saver. Many thanks!
|
All times are GMT. The time now is 03:48 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 | |
---|---|
|
|
![]() |
|
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|