SanitizeService.php 906 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. namespace App\Services;
  3. use Stevebauman\Purify\Facades\Purify;
  4. class SanitizeService
  5. {
  6. public function purify($html)
  7. {
  8. $cleaned = Purify::clean($html);
  9. return $cleaned;
  10. }
  11. public function html($html)
  12. {
  13. return $this->cleanHtmlWithSpacing($html);
  14. }
  15. public function cleanHtmlWithSpacing($html)
  16. {
  17. $blockTags = ['a', 'b', 'blockquote', 'br', 'code', 'del', 'div', 'em', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'i', 'img', 'li', 'ol', 'p', 'pre', 's', 'strike', 'strong', 'u', 'ul'];
  18. foreach ($blockTags as $tag) {
  19. $html = preg_replace("/<\/{$tag}>/i", "</{$tag}> ", $html);
  20. }
  21. $html = preg_replace("/<br\s*\/?>/i", '<br /> ', $html);
  22. $cleaned = Purify::clean($html);
  23. $cleaned = preg_replace('/\s+/', ' ', $cleaned);
  24. $cleaned = trim($cleaned);
  25. return $cleaned;
  26. }
  27. }