3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace NeverMindWhoIWorkFor; use DOMDocument; class Paginator { /** @var DOMDocument $dom */ private $dom; /** @var int $numLinks */ private $numLinks; /** @var int $checkNum */ private $checkNum; public function __construct() { $this->dom = new DOMDocument(); } public function paginate($uri, $currentPage, $totalPages, $numLinks = 5) { $this->numLinks = ($numLinks % 2 == 1) ? $numLinks : $numLinks - 1; $this->checkNum = ($this->numLinks - 1) / 2; $nav = $this->dom->createElement('nav'); $ul = $this->dom->createElement('ul'); $ul->setAttribute('class', 'pagination'); // Goto page 1 link $li = $this->dom->createElement('li'); $a = $this->dom->createElement('a'); $a->setAttribute('href',$this->setPageUri($uri, 1)); if($currentPage < 2) { $li->setAttribute('class', 'disabled'); $a->setAttribute('href','#'); } $span = $this->dom->createElement('span','&laquo;'); $a->appendChild($span); $li->appendChild($a); $ul->appendChild($li); // Previous Page Link $li = $this->dom->createElement('li'); $a = $this->dom->createElement('a'); $a->setAttribute('href',$this->setPageUri($uri, $currentPage - 1)); if($currentPage < 2) { $li->setAttribute('class', 'disabled'); $a->setAttribute('href','#'); } $span = $this->dom->createElement('span','&lsaquo;'); $a->appendChild($span); $li->appendChild($a); $ul->appendChild($li); // Figure out which page numbers to display $startPage = $this->getStartPageNumber($currentPage, $totalPages, $numLinks); $endPage = $this->getEndPageNumber($currentPage, $totalPages); // Loop through and generate links for($x = $startPage; $x <= $endPage; $x ++) { $li = $this->dom->createElement('li'); $a = $this->dom->createElement('a'); if($currentPage == $x) { $li->setAttribute('class', 'active'); } $a->setAttribute('href',$this->setPageUri($uri, $x)); $span = $this->dom->createElement('span',$x); $a->appendChild($span); $li->appendChild($a); $ul->appendChild($li); } // Next Page Link $li = $this->dom->createElement('li'); $a = $this->dom->createElement('a'); $a->setAttribute('href',$this->setPageUri($uri, $currentPage + 1)); if($currentPage >= $totalPages) { $li->setAttribute('class', 'disabled'); $a->setAttribute('href','#'); } $span = $this->dom->createElement('span','&rsaquo;'); $a->appendChild($span); $li->appendChild($a); $ul->appendChild($li); // Last Page Link $li = $this->dom->createElement('li'); $a = $this->dom->createElement('a'); $a->setAttribute('href',$this->setPageUri($uri, $totalPages)); if($currentPage >= $totalPages) { $li->setAttribute('class', 'disabled'); $a->setAttribute('href','#'); } $span = $this->dom->createElement('span','&raquo;'); $a->appendChild($span); $li->appendChild($a); $ul->appendChild($li); // Add the ul to the nav, add the nav to the DOM document $nav->appendChild($ul); $this->dom->appendChild($nav); // Get the generated HTML $html = $this->dom->saveHTML(); // Reset the DOM Document $this->dom = new DOMDocument(); return $html; } /** * @param $uri * @param $pageNum * @return mixed */ private function setPageUri($uri, $pageNum) { return preg_replace('#page=\d+#','page='.$pageNum, $uri); } /** * @param $currentPage * @param $totalPages * @return int */ private function getStartPageNumber($currentPage, $totalPages, $numLinks) { if($numLinks >= $totalPages) { return 1; } $startPage = ($currentPage - $this->checkNum < $this->checkNum) ? 1 : $currentPage - $this->checkNum; if($totalPages - $currentPage <= $this->checkNum) { return $totalPages - ($this->checkNum * 2); } return $startPage; } /** * @param $currentPage * @param $totalPages * @return mixed */ private function getEndPageNumber($currentPage, $totalPages) { if($totalPages < $this->numLinks) { return $totalPages; } $currentPage = ($currentPage > $totalPages) ? $totalPages : $currentPage; $endPage = ($currentPage + $this->checkNum >= $totalPages) ? $totalPages : $currentPage + $this->checkNum; if($endPage < $this->numLinks) { return $this->numLinks; } return $endPage; } } $p = new Paginator(); echo $p->paginate('/some-url?page=1', 4, 7); //page=1 will be replaced, 4 is current page, 7 is total pages
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
<nav><ul class="pagination"><li><a href="/some-url?page=1"><span>&laquo;</span></a></li><li><a href="/some-url?page=3"><span>&lsaquo;</span></a></li><li><a href="/some-url?page=2"><span>2</span></a></li><li><a href="/some-url?page=3"><span>3</span></a></li><li class="active"><a href="/some-url?page=4"><span>4</span></a></li><li><a href="/some-url?page=5"><span>5</span></a></li><li><a href="/some-url?page=6"><span>6</span></a></li><li><a href="/some-url?page=5"><span>&rsaquo;</span></a></li><li><a href="/some-url?page=7"><span>&raquo;</span></a></li></ul></nav>
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 <nav><ul class="pagination"><li><a href="/some-url?page=1"><span>&laquo;</span></a></li><li><a href="/some-url?page=3"><span>&lsaquo;</span></a></li><li><a href="/some-url?page=2"><span>2</span></a></li><li><a href="/some-url?page=3"><span>3</span></a></li><li class="active"><a href="/some-url?page=4"><span>4</span></a></li><li><a href="/some-url?page=5"><span>5</span></a></li><li><a href="/some-url?page=6"><span>6</span></a></li><li><a href="/some-url?page=5"><span>&rsaquo;</span></a></li><li><a href="/some-url?page=7"><span>&raquo;</span></a></li></ul></nav>

preferences:
203.6 ms | 403 KiB | 186 Q