You are currently looping from 1 to $number_of_pages when displaying your page numbers.
What you want to do is loop from $page - 2 to $page + 2, taking care not to go below 0 or above $number_of_pages in the process. Get your range of pages to loop over with some simple math and the help of the min/max functions then loop over that range.
$start = max(1, $page - 2);
$end = min($number_of_pages, $page + 2);
for ($p=$start; $p<=$end; $p++){
//...
}