vendor/lm/util/src/Str.php line 44

Open in your IDE?
  1. <?php
  2. namespace Lm\Util;
  3. class Str
  4. {
  5.     /**
  6.      * Convert underscore_strings to camelCase (medial capitals).
  7.      *
  8.      * @link https://stackoverflow.com/questions/2791998/convert-string-with-dashes-to-camelcase
  9.      * @param string $str
  10.      * @return string
  11.      */
  12.     public static function snakeToCamel($str)
  13.     {
  14.         // Remove underscores, capitalize words, squash, lowercase first.
  15.         return lcfirst(str_replace(' '''ucwords(str_replace('_'' '$str))));
  16.     }
  17.     /**
  18.      * Convert underscore_strings to camelCase (medial capitals).
  19.      *
  20.      * @link https://stackoverflow.com/questions/2791998/convert-string-with-dashes-to-camelcase
  21.      * @param string $str
  22.      * @return string
  23.      */
  24.     public static function kebabToCamel($str)
  25.     {
  26.         // Remove underscores, capitalize words, squash, lowercase first.
  27.         return lcfirst(str_replace(' '''ucwords(str_replace('-'' '$str))));
  28.     }
  29.     /**
  30.      * カンマ区切り文字列(=csv)を文字列の配列に変換する
  31.      *
  32.      * @param string $str
  33.      * @param string $delimiter
  34.      * @return false|string[]
  35.      */
  36.     public static function csvToArray($str$delimiter ',')
  37.     {
  38.         //
  39.         $result explode($delimiter$str);
  40.         // TODO: オプション化 - 空白文字のみの要素は省略
  41.         $result array_filter($result, function ($col) {
  42.             return trim($col) !== '';
  43.         });
  44.         // TODO: オプション化 - 前後の空白文字はトリムする
  45.         $result array_map(function ($col) {
  46.             return trim($col);
  47.         }, $result);
  48.         //
  49.         $result array_values($result);
  50.         //
  51.         return $result;
  52.     }
  53.     /**
  54.      * @link https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address
  55.      * @param $email
  56.      * @return false|int
  57.      */
  58.     public static function isValidEmailAddress($email)
  59.     {
  60.         return preg_match('/^[a-zA-Z0-9.!#$%&\'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/'$email);
  61.     }
  62.     /**
  63.      * 機種依存文字を代替文字に変換する
  64.      * @link https://qiita.com/ymd_a/items/79125b2b9bb6c51ccd9c
  65.      * @param $str
  66.      * @return array|string|string[]
  67.      */
  68.     public static function replaceMachineChar($str)
  69.     {
  70.         //
  71.         $search = [
  72.             'Ⅰ''Ⅱ''Ⅲ''Ⅳ''Ⅴ''Ⅵ''Ⅶ''Ⅷ''Ⅸ''Ⅹ',
  73.             'ⅰ''ⅱ''ⅲ''ⅳ''ⅴ''ⅵ''ⅶ''ⅷ''ⅸ''ⅹ',
  74.             '①''②''③''④''⑤''⑥''⑦''⑧''⑨''⑩',
  75.             '⑪''⑫''⑬''⑭''⑮''⑯''⑰''⑱''⑲''⑳',
  76.             '№''㈲''㈱''㈹',
  77.             '㊤''㊦''㊥''㊧''㊨',
  78.             '髙''﨑''彅''塚''增''寬''敎''晴''朗''﨔''橫''德''瀨',
  79.             '淸''瀨''凞''猪''益''礼''神''祥''福''靖''精''濵''琦''昻',
  80.             '緖''羽''薰''諸''賴''逸''郞''都''鄕''閒''隆''靑''飯''飼''館',
  81.             '鶴''黑',
  82.             '㍉''㌔''㌢''㍍''㌘''㌧''㌃''㌶''㍑',
  83.             '㍗''㌍''㌦''㌣''㌫''㍊''㌻',
  84.             '㎜''㎝''㎞''㎏''㏄''㎡',
  85.             '㍻''〝''〟''℡''㍾''㍽''㍼''㏍'
  86.         ];
  87.         //
  88.         $replace = [
  89.             'I''II''III''IV''V''VI''VII''VIII''IX''X',
  90.             'i''ii''iii''vi''v''vi''vii''viii''ix''x',
  91.             '(1)''(2)''(3)''(4)''(5)''(6)''(7)''(8)''(9)''(10)',
  92.             '(11)''(12)''(13)''(14)''(15)''(16)''(17)''(18)''(19)''(20)',
  93.             'No.''(有)''(株)''(代)',
  94.             '(上)''(下)''(中)''(左)''(右)',
  95.             '高''崎''なぎ''塚''増''寛''教''晴''朗''欅''横''徳''瀬',
  96.             '清''瀬''煕''猪''益''礼''神''祥''福''靖''精''濱''埼''昂',
  97.             '緒''羽''薫''諸''頼''逸''郎''都''郷''間''隆''青''飯''飼''館',
  98.             '鶴''黒',
  99.             'ミリ''キロ''センチ''メートル''グラム''トン''アール''ヘクタール''リットル',
  100.             'ワット''カロリー''ドル''セント''パーセント''ミリバール''ページ',
  101.             'mm''cm''km''kg''cc''平方メートル',
  102.             '平成''"''"''TEL''明治''大正''昭和''K.K.'
  103.         ];
  104.         //
  105.         return str_replace($search$replace$str);
  106.     }
  107. }