2
For example, I want to take bad words out of a text.
How can I do using the substitution method ? Something that is faster.
Use the str_replace()
is not very fast.
2
For example, I want to take bad words out of a text.
How can I do using the substitution method ? Something that is faster.
Use the str_replace()
is not very fast.
1
Can use preg_replace_callback()
he’s fast.
In If you want to replace bad words with '*' here is a good example:
$badwords = array('bad1', 'bad2', 'bad3', 'ass');
$text = 'This is a test. Ass. Grass. bad1.';
function filterBadwords($text, array $badwords, $replaceChar = '*') {
return preg_replace_callback(
array_map(function($w) { return '/\b' . preg_quote($w, '/') . '\b/i'; }, $badwords),
function($match) use ($replaceChar) { return str_repeat($replaceChar, strlen($match[0])); },
$text
);
}
echo filterBadwords($text, $badwords);
echo will print:
This is a test. ***. Grass. ****.
If on the other hand instead of replacing you want to highlight words you can use this function:
$badwords = array('bad1', 'bad2', 'bad3', 'ass');
$text = 'This is a test. Ass. Grass. bad1.';
function badwords_filter($string,$badwords){
$p = implode('|', array_map('preg_quote', $words ));
$string = preg_replace(
'/('.$p.')/i',
'<span style="background:#fe5723; color:#fff">$1</span>',
$string
);
return $string;
}
echo badwords_filter($text, $badwords);
Browser other questions tagged php
You are not signed in. Login or sign up in order to post.
What do you mean it’s not fast? It would be nice to show how you’re doing, speed problem can be in the way you apply. o
str_replace
accepted arrays including, can make several substitutions in a single line. I gave example here, including thestr_ireplace
: http://answall.com/questions/35562/str-replace-usando-arrays– Bacco
You can use the preg_replace() PREG_REPLACE In terms of performance I do not know if it makes much difference, but it is a good alternative, creates the Pattern and applies on the text, everything depends on the application.
– Fernando A.W.
I can use array on
str_replace()
?– Diego Souza
@Zoom has an example here http://answall.com/questions/35562/str-replace-usando-arrays
– Bacco
@Zoom yes, the 1st and 2nd parameters can be array.
– Guilherme Lautert
In Preg_replace you can use arrays...
– Fernando A.W.
Taking that same idea, I can do
highlights
for searched words ?– Diego Souza
@Fernandoa.W. but he wants something faster, not slower.
– Bacco
@zoom
str_replace ( array( 'zoom', 'SOpt') , array( '<b>zoom</b>' , '<b>SOpt</b>' ), $texto )
so it will trade everything that is zoom by zoom and all that is Sopt by Sopt.– Bacco
But that’s where the problem lies. To use
array
in thestr_replace
, both thefrom
like theto
has to bearray
. Isn’t it ? The text has to be converted every word intoarray
.– Diego Souza
@Zoom no, text is string. Array are the original and replacement lists.
– Bacco
Okay. I get it, I create a
array
with the list of ugly words and another for words I want to replace them or####
.– Diego Souza
str_replace ( array( 'zoom', 'SOpt') , array( '<b>zoom</b>' , '<b>SOpt</b>' ), 'Eu sou o zoom do SOpt' )
will turnEu sou o <b>zoom</b> do <b>SOpt</b>
– Bacco
@Zoom in this case, you can put the ugly ones as an array, and on the other side just '####' (so they all become the same thing). Then Voce uses array in search, and string in substitute.
– Bacco
All right. I’ll do it.
– Diego Souza