Skip to content

Instantly share code, notes, and snippets.

@jprivet-dev
Last active January 12, 2023 21:32
Show Gist options
  • Save jprivet-dev/1e55f90d2bb29891b831a48d286171ae to your computer and use it in GitHub Desktop.
Save jprivet-dev/1e55f90d2bb29891b831a48d286171ae to your computer and use it in GitHub Desktop.

Revisions

  1. jprivet-dev revised this gist Jan 12, 2023. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion StringUtil.php
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,9 @@
    <?php

    declare(strict_types=1);

    namespace App\Util;

    abstract class StringUtil
    {
    /**
    @@ -45,4 +51,4 @@ public static function trim(string $string): string

    return preg_replace("{^[$chars]++|[$chars]++$}uD", '', $string);
    }
    }
    }
  2. jprivet-dev revised this gist Jan 12, 2023. 1 changed file with 48 additions and 0 deletions.
    48 changes: 48 additions & 0 deletions StringUtil.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    abstract class StringUtil
    {
    /**
    * Replace the accents in a string.
    *
    * @param string $string
    * @return string
    */
    public static function noAccent(string $string): string
    {
    $lettersAccent = "ç,æ,œ,á,é,í,ó,ú,à,è,ì,ò,ù,ä,ë,ï,ö,ü,ÿ,â,ê,î,ô,û,å,ø,u";
    $lettersNoAccent = "c,ae,oe,a,e,i,o,u,a,e,i,o,u,a,e,i,o,u,y,a,e,i,o,u,a,o,u";
    $search = explode(",", sprintf("%s,%s", $lettersAccent, mb_strtoupper($lettersAccent)));
    $replace = explode(",", sprintf("%s,%s", $lettersNoAccent, mb_strtoupper($lettersNoAccent)));

    return str_replace($search, $replace, $string);
    }

    /**
    * Convert a string into camelCase format.
    *
    * @param string $string
    * @return string
    *
    * @see https://github.com/symfony/symfony/blob/6.3/src/Symfony/Component/DependencyInjection/Compiler/RegisterServiceSubscribersPass.php#L117
    */
    public static function camelCase(string $string): string
    {
    $string = self::noAccent(strtolower($string));

    return lcfirst(str_replace(' ', '', ucwords(preg_replace('/[^a-zA-Z0-9\x7f-\xff]++/', ' ', $string))));
    }

    /**
    * Remove all types of spaces.
    *
    * @param string $string
    * @return string
    *
    * @see https://github.com/symfony/symfony/blob/6.3/src/Symfony/Component/String/AbstractUnicodeString.php#L370
    */
    public static function trim(string $string): string
    {
    $chars = " \t\n\r\0\x0B\x0C\u{A0}\u{FEFF}";

    return preg_replace("{^[$chars]++|[$chars]++$}uD", '', $string);
    }
    }
  3. jprivet-dev revised this gist Sep 22, 2022. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion year-datetime-conversion.php
    Original file line number Diff line number Diff line change
    @@ -10,4 +10,5 @@
    $date = new DateTime('2022-01-01 00:00:00');
    echo $date->format('Y'); // 2022

    // See the result on https://onlinephp.io?s=dY7NCsIwEITvhb7DHpS0YJLaY6r14sEH8NJjsCkt2DQ0keDbu4k_iFjYyywz883uYHqTJpxD75yxgnPvPcMf08rxUeqbvHKleSudcsOoWDfNo3QspmKuUXKGGo5oOKMhTVb38NkDlEVZVqhDFrVW_uPKrJkH7bqMrC0ttnhQFCIe2UBsyHPMqks_QSyg9ZOckYaOtIWTGIQleQU4IYB-Wl7j3jwcGHYujCF_Csgi_guaJg8%2C&v=8.1.10
    // See the result on
    // https://onlinephp.io?s=dY7NCsIwEITvhb7DHpS0YJLaY6r14sEH8NJjsCkt2DQ0keDbu4k_iFjYyywz883uYHqTJpxD75yxgnPvPcMf08rxUeqbvHKleSudcsOoWDfNo3QspmKuUXKGGo5oOKMhTVb38NkDlEVZVqhDFrVW_uPKrJkH7bqMrC0ttnhQFCIe2UBsyHPMqks_QSyg9ZOckYaOtIWTGIQleQU4IYB-Wl7j3jwcGHYujCF_Csgi_guaJg8%2C&v=8.1.10
  4. jprivet-dev created this gist Sep 22, 2022.
    13 changes: 13 additions & 0 deletions year-datetime-conversion.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    <?php
    // https://www.php.net/manual/en/datetime.format.php

    // Year > DateTime
    $year = 2022;
    $date = new DateTime(sprintf('%s-01-01 00:00:00', $year));
    echo $date->format('Y-m-d H:i:s'); // 2022-01-01 00:00:00

    // DateTime > Year
    $date = new DateTime('2022-01-01 00:00:00');
    echo $date->format('Y'); // 2022

    // See the result on https://onlinephp.io?s=dY7NCsIwEITvhb7DHpS0YJLaY6r14sEH8NJjsCkt2DQ0keDbu4k_iFjYyywz883uYHqTJpxD75yxgnPvPcMf08rxUeqbvHKleSudcsOoWDfNo3QspmKuUXKGGo5oOKMhTVb38NkDlEVZVqhDFrVW_uPKrJkH7bqMrC0ttnhQFCIe2UBsyHPMqks_QSyg9ZOckYaOtIWTGIQleQU4IYB-Wl7j3jwcGHYujCF_Csgi_guaJg8%2C&v=8.1.10