$query = "Ahmed"; //what you are querying in the db
$string = "Ahmed is the user name"; //the result you got from the database
$regex = "/\b(?!{$query}\b)\w+/"; //add the query to your regular expression
$result = preg_replace($regex,'*',$string);
var_dump($result); // Ahmed * * * *