To briefly go over it...
You have to define how many page links you want shown. So for this example, we'll use 5 ($display will equal 5). So you'll have 5 page links, plus the Previous & Next links. So it'll end up looking like:
« Previous 1 ... 8 [ 9 ] 10 ... 25 Next »
We first look to see if $current_page is within 3 of the last or first page. So in our example if $current_page is between 2 and 4 or 24 and 22, we just show the starting or ending 4. As well as the last page and Previous & Next links.
If it's not within the starting or ending 4 (as explained above) then we need to figure out the display. With this, we can now say that we'll show $display - 2 (for the first & last pages); this will be saved as $spread.
Now, if $spread is greater than 0, we take the spread, subtract 1 (for the current page) and then divide that by 2. That is the number before and after the current page we'll go to. We'll save this as $pad.
From here, we just need to loop through starting at $current_page - $pad, and going to $current_page + $pad.
Then just concatenate the beginning and ending as needed 😉
That's the basic outline of it. I don't have time to write the code for you to see, but this should really help you out.