I am developing a screening logic where complete text of content to be matched with list of keywords in php via regular expression.
I am using the following code which match words properly and make it bold.
$pattern = "/mango|apple|banana/";
$text = "i like banana and apple alot";
$replacement = "<strong>$0</strong>";
echo preg_replace($pattern, $replacement, $text);
this code properly match and make matched word enclosed in strong like
i like <strong>banana</strong> and <strong>apple</strong> alot.
But i want to replace banana as
b****a and apple as a****e
instead of making it bold.
Can anyone help me how it will be possible.
You can try using
preg_replace_callbackinstead ofpreg_replace.Updated to create a dynamic number of stars