There is a way to do it in mysql. Other answers may give PHP solution, but in mysql:
SELECT CONCAT(LEFT(text,1), REPEAT('*',LENGTH(text)-2), RIGHT(text,1)) AS text FROM exercise
simply concatenate the first letter, then your asterisks, then last letter.
Adding the PHP code just in case:
// New Connection
$db = new mysqli('localhost','user','pass','database');
$result = $db->query("SELECT CONCAT(LEFT(text,1), REPEAT('*',LENGTH(text)-2), RIGHT(text,1)) AS text FROM exercise");
if($result){
// Cycle through results
while ($row = $result->fetch_object()){
echo $row;
}
// Free result set
$result->close();
}
alternatively, doing the replacement in PHP 7.1 or later
// New Connection
$db = new mysqli('localhost','user','pass','database');
$result = $db->query("SELECT text FROM exercise");
if($result){
// Cycle through results
while ($row = $result->fetch_object()){
if (strlen($row) > 2) {
echo $row[0] . str_repeat('*', strlen($row)-2) . $row[-1]
}
else {
echo $row
}
}
// Free result set
$result->close();
}