dlang语言实现laravel的IlluminateSupportStr以下几个方法并标明注释StrafterStrafterLastStrasciiStrbeforeStrbeforeLastStrbetweenStrcamelStrcontainsStrcontainsAllStrendsWithStrexcerptStrfinishStrheadlineStrisStrisAsciiStr
// Str::after
// 返回在指定字符串之后的所有字符
public static function after($subject, $search)
{
if ($search === '') {
return $subject;
}
$pos = strpos($subject, $search);
if ($pos === false) {
return '';
}
return substr($subject, $pos + strlen($search));
}
// Str::afterLast
// 返回在字符串中最后一个出现的指定字符串之后的所有字符
public static function afterLast($subject, $search)
{
if ($search === '') {
return $subject;
}
$pos = strrpos($subject, $search);
if ($pos === false) {
return '';
}
return substr($subject, $pos + strlen($search));
}
// Str::ascii
// 将字符串转换为 ASCII
public static function ascii($value)
{
$foreignChars = Config::get('ascii.foreign_chars');
foreach ($foreignChars as $key => $value) {
$value = is_array($value) ? $value : [$value];
$value = implode('|', $value);
$value = str_replace('\*', '.*', preg_quote($value, '/'));
$pattern = '/'.$value.'/u';
$callback = function ($matches) use ($key) {
return Config::get('ascii.replacements.'.$key, '');
};
$value = preg_replace_callback($pattern, $callback, $value);
}
return preg_replace('/[^\x20-\x7E]/u', '', $value);
}
// Str::before
// 返回在指定字符串之前的所有字符
public static function before($subject, $search)
{
if ($search === '') {
return $subject;
}
$pos = strpos($subject, $search);
if ($pos === false) {
return '';
}
return substr($subject, 0, $pos);
}
// Str::beforeLast
// 返回在字符串中最后一个出现的指定字符串之前的所有字符
public static function beforeLast($subject, $search)
{
if ($search === '') {
return $subject;
}
$pos = strrpos($subject, $search);
if ($pos === false) {
return '';
}
return substr($subject, 0, $pos);
}
// Str::between
// 返回位于两个指定字符串之间的所有字符
public static function between($subject, $start, $end)
{
if ($start === '' || $end === '') {
return $subject;
}
$startPos = strpos($subject, $start);
if ($startPos === false) {
return '';
}
$endPos = strpos($subject, $end, $startPos + strlen($start));
if ($endPos === false) {
return '';
}
return substr($subject, $startPos + strlen($start), $endPos - ($startPos + strlen($start)));
}
// Str::camel
// 将字符串转换为驼峰式大小写
public static function camel($value)
{
static $camelCache = [];
if (isset($camelCache[$value])) {
return $camelCache[$value];
}
return $camelCache[$value] = lcfirst(static::studly($value));
}
// Str::contains
// 判断指定字符串是否在给定的字符串中出现
public static function contains($haystack, $needles)
{
foreach ((array) $needles as $needle) {
if ($needle !== '' && strpos($haystack, $needle) !== false) {
return true;
}
}
return false;
}
// Str::containsAll
// 判断指定字符串是否包含所有给定的子字符串
public static function containsAll($haystack, array $needles)
{
foreach ($needles as $needle) {
if (!static::contains($haystack, $needle)) {
return false;
}
}
return true;
}
// Str::endsWith
// 判断指定字符串是否以给定字符串结尾
public static function endsWith($haystack, $needles)
{
foreach ((array) $needles as $needle) {
if ((string) $needle === substr($haystack, -strlen($needle))) {
return true;
}
}
return false;
}
// Str::excerpt
// 从字符串中提取一段摘录
public static function excerpt($value, $phrase, $radius = 100, $ending = '...')
{
if (is_null($phrase)) {
return static::substr($value, 0, $radius).$ending;
}
$phrases = (array) $phrase;
foreach ($phrases as $phrase) {
$pos = strpos($value, $phrase);
if ($pos !== false) {
break;
}
}
if ($pos === false) {
return static::substr($value, 0, $radius).$ending;
}
$start = max(0, $pos - $radius);
$length = min(strlen($value), $pos + strlen($phrase) + $radius) - $start;
return ($start > 0 ? $ending : '').static::substr($value, $start, $length).($pos + strlen($phrase) < strlen($value) ? $ending : '');
}
// Str::finish
// 给定字符串以指定字符串结尾
public static function finish($value, $cap)
{
$quoted = preg_quote($cap, '/');
return preg_replace('/(?:'.$quoted.')+$/u', '', $value).$cap;
}
// Str::headline
// 将字符串转换为标题大小写
public static function headline($value)
{
return ucwords(static::snake($value, ' '));
}
// Str::is
// 判断字符串是否与给定模式匹配
public static function is($pattern, $value)
{
if ($pattern === $value) {
return true;
}
$patterns = is_array($pattern) ? $pattern : [$pattern];
foreach ($patterns as $pattern) {
if ($pattern === $value) {
return true;
}
$pattern = preg_quote($pattern, '#');
$pattern = str_replace('\*', '.*', $pattern);
if (preg_match('#^'.$pattern.'\z#u', $value) === 1) {
return true;
}
}
return false;
}
// Str::isAscii
// 判断字符串是否全部由 ASCII 字符组成
public static function isAscii($value)
{
return preg_match('/^[\x00-\x7F]+$/', $value) > 0;
}
// Str::isUuid
// 判断字符串是否为 UUID
public static function isUuid($value)
{
return preg_match('/^[a-f\d]{8}(-[a-f\d]{4}){3}-[a-f\d]{12}$/i', $value) === 1;
}
``
原文地址: https://www.cveoy.top/t/topic/flM4 著作权归作者所有。请勿转载和采集!