StringsUtils.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Service\Utils;
  4. use App\Tests\Service\Utils\StringsUtilsTest;
  5. /**
  6. * Class StringsUtils : méthodes d'aide pour la gestion de string.
  7. * @package App\Service\Utils
  8. */
  9. class StringsUtils
  10. {
  11. /**
  12. * Supprime les quotes d'une chaine de caractères
  13. * @param string $str
  14. * @return string
  15. * @see StringsUtilsTest::testUnquote()
  16. */
  17. public static function unquote(string $str): string {
  18. return str_replace("'", "", $str);
  19. }
  20. /**
  21. * Convert CamelCase formatted string into snake_case
  22. * @see https://stackoverflow.com/a/40514305/4279120
  23. *
  24. * @param string $string
  25. * @param string $sep
  26. * @return string
  27. *
  28. * @see StringsUtilsTest::testCamelToSnake()
  29. */
  30. public static function camelToSnake(string $string, string $sep = "_"): string {
  31. return strtolower(
  32. preg_replace(
  33. '/(?<=\d)(?=[A-Za-z])|(?<=[A-Za-z])(?=\d)|(?<=[a-z])(?=[A-Z])/',
  34. $sep,
  35. $string
  36. )
  37. );
  38. }
  39. /**
  40. * @param string $html
  41. * @return string
  42. *
  43. * @see StringsUtilsTest::testConvertHtmlToText()
  44. */
  45. public function convertHtmlToText(string $html): string
  46. {
  47. return strip_tags(preg_replace('{<(head|style)\b.*?</\1>}is', '', $html));
  48. }
  49. }