<?php
namespace Lm\Util;
class Str
{
/**
* Convert underscore_strings to camelCase (medial capitals).
*
* @link https://stackoverflow.com/questions/2791998/convert-string-with-dashes-to-camelcase
* @param string $str
* @return string
*/
public static function snakeToCamel($str)
{
// Remove underscores, capitalize words, squash, lowercase first.
return lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $str))));
}
/**
* Convert underscore_strings to camelCase (medial capitals).
*
* @link https://stackoverflow.com/questions/2791998/convert-string-with-dashes-to-camelcase
* @param string $str
* @return string
*/
public static function kebabToCamel($str)
{
// Remove underscores, capitalize words, squash, lowercase first.
return lcfirst(str_replace(' ', '', ucwords(str_replace('-', ' ', $str))));
}
/**
* カンマ区切り文字列(=csv)を文字列の配列に変換する
*
* @param string $str
* @param string $delimiter
* @return false|string[]
*/
public static function csvToArray($str, $delimiter = ',')
{
//
$result = explode($delimiter, $str);
// TODO: オプション化 - 空白文字のみの要素は省略
$result = array_filter($result, function ($col) {
return trim($col) !== '';
});
// TODO: オプション化 - 前後の空白文字はトリムする
$result = array_map(function ($col) {
return trim($col);
}, $result);
//
$result = array_values($result);
//
return $result;
}
/**
* @link https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address
* @param $email
* @return false|int
*/
public static function isValidEmailAddress($email)
{
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);
}
}