I have this PHP function to replace words in text from my list file with some words
My function
function replace_text_wps($text){
$dir = plugin_dir_path( __FILE__ );
$file= $dir."bad2.list";
$badlist = file($file, FILE_IGNORE_NEW_LINES);
$replace = '[censored]';
$text = str_replace($badlist, $replace, $text);
return $text;
}
For example I have word ABC in my bad2.list
When I enter text ABC my function change ABC to [censored] , but if I enter word DFGABC in change it to DFG[censored]
How to replace only match words from my file? Im new in PHP? sorry for noob questions
Update:
HD, thank you! your solution works for me!
This is work version
function replace_text_wps($text){
$dir = plugin_dir_path( __FILE__ );
$file= $dir."bad2.list";
$badlist = file($file, FILE_IGNORE_NEW_LINES);
$replacement = "[CENSORED]";
$badlist = array_map(function($v) { return "\b". $v ."\b"; }, $badlist);
foreach($badlist as $f) {
$text = preg_replace("/".$f."/u", $replacement, $text);
return $text;
}