本文整理汇总了PHP中Illuminate\Support\Str::length方法的典型用法代码示例。如果您正苦于以下问题:PHP Str::length方法的具体用法?PHP Str::length怎么用?PHP Str::length使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Illuminate\Support\Str
的用法示例。
在下文中一共展示了Str::length方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: handle
/**
* Handle the event.
*
* @param \App\Events\WechatUserSubscribed $event
* @return \App\Models\Message
*/
public function handle(WechatUserSubscribed $event)
{
$m = $event->message;
/**
* openId is always unique to our official account, so if user subscribes
* again, we just need to toggle un-subscribed flag.
*/
if ($subscriber = Subscriber::where('openId', $m->fromUserName)->where('unsubscribed', true)->first()) {
$subscriber->unsubscribed = false;
} else {
$subscriber = new Subscriber();
$subscriber->openId = $m->fromUserName;
}
$subscriber->save();
// Link profile with subscriber if subscribe comes from profile page.
if ($key = $m->messageable->eventKey) {
Profile::find(Str::substr($key, Str::length('qrscene_')))->update(['weixin' => $m->fromUserName]);
event(new ChangeSubscriberGroup($subscriber));
}
return $this->greetMessage($m->fromUserName, !is_null($key));
}
示例2: wildcardLikeString
/**
* Adds % wildcards to the given string.
*
* @param string $str
* @param bool $lowercase
* @return string
*/
public function wildcardLikeString($str, $lowercase = true)
{
$wild = '%';
$length = Str::length($str);
if ($length) {
for ($i = 0; $i < $length; $i++) {
$wild .= $str[$i] . '%';
}
}
if ($lowercase) {
$wild = Str::lower($wild);
}
return $wild;
}
示例3: prepareRequest
protected function prepareRequest($request, array &$options)
{
$method = $request->getMethod();
$uri = $request->getUri();
$basePath = $options['base_uri']->getPath();
$path = Str::substr($uri->getPath(), Str::length($basePath));
if ($method === 'GET') {
parse_str($uri->getQuery(), $options['query']);
} else {
$body = (string) $request->getBody();
$options['json'] = json_decode($body, true);
}
return [$method, $path];
}
示例4: function
use Illuminate\Support\Str;
if (!Str::hasMacro('after')) {
/**
* Get the part of haystack after needle.
*
* @param string $haystack
* @param string $needle
* @return string
*/
Str::macro('after', function ($haystack, $needle) {
$pos = strpos($haystack, $needle);
if ($pos === false) {
return $haystack;
}
return Str::substr($haystack, $pos + Str::length($needle));
});
}
if (!Str::hasMacro('before')) {
/**
* Get the part of haystack before needle.
*
* @param string $haystack
* @param string $needle
* @return string
*/
Str::macro('before', function ($haystack, $needle) {
$pos = strpos($haystack, $needle);
if ($pos === false) {
return $haystack;
}
示例5: limitHtml
/**
* Limits HTML with specific length with a proper tag handling.
* @param string $html HTML string to limit
* @param int $maxLength String length to truncate at
* @param string $end
* @return string
*/
public static function limitHtml($html, $maxLength, $end = '...')
{
$printedLength = 0;
$position = 0;
$tags = array();
$re = '{</?([a-z]+)[^>]*>|&#?[a-zA-Z0-9]+;|[\\x80-\\xFF][\\x80-\\xBF]*}';
$result = '';
while ($printedLength < $maxLength && preg_match($re, $html, $match, PREG_OFFSET_CAPTURE, $position)) {
list($tag, $tagPosition) = $match[0];
$str = mb_substr($html, $position, $tagPosition - $position);
if ($printedLength + StrHelper::length($str) > $maxLength) {
$result .= mb_substr($str, 0, $maxLength - $printedLength) . $end;
$printedLength = $maxLength;
break;
}
$result .= $str;
$printedLength += StrHelper::length($str);
if ($printedLength >= $maxLength) {
$result .= $end;
break;
}
if ($tag[0] == '&' || ord($tag) >= 0x80) {
$result .= $tag;
$printedLength++;
} else {
$tagName = $match[1][0];
if ($tag[1] == '/') {
$openingTag = array_pop($tags);
$result .= $tag;
} else {
if ($tag[StrHelper::length($tag) - 2] == '/') {
$result .= $tag;
} else {
$result .= $tag;
$tags[] = $tagName;
}
}
}
$position = $tagPosition + StrHelper::length($tag);
}
if ($printedLength < $maxLength && $position < StrHelper::length($html)) {
$result .= substr($html, $position, $maxLength - $printedLength);
}
while (!empty($tags)) {
$result .= sprintf('</%s>', array_pop($tags));
}
return $result;
}
示例6: handle
/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
pcntl_signal(SIGINT, [$this, 'handleInterrupt']);
$mlpmaPath = Config::get('ponyfm.files_directory') . '/mlpma';
$tmpPath = Config::get('ponyfm.files_directory') . '/tmp';
if (!File::exists($tmpPath)) {
File::makeDirectory($tmpPath);
}
$UNKNOWN_GENRE = Genre::firstOrCreate(['name' => 'Unknown', 'slug' => 'unknown']);
$this->comment('Enumerating MLP Music Archive source files...');
$files = File::allFiles($mlpmaPath);
$this->info(sizeof($files) . ' files found!');
$this->comment('Enumerating artists...');
$artists = File::directories($mlpmaPath);
$this->info(sizeof($artists) . ' artists found!');
$this->comment('Importing tracks...');
$totalFiles = sizeof($files);
$fileToStartAt = (int) $this->option('startAt') - 1;
$this->comment("Skipping {$fileToStartAt} files..." . PHP_EOL);
$files = array_slice($files, $fileToStartAt);
$this->currentFile = $fileToStartAt;
foreach ($files as $file) {
$this->currentFile++;
pcntl_signal_dispatch();
if ($this->isInterrupted) {
break;
}
$this->comment('[' . $this->currentFile . '/' . $totalFiles . '] Importing track [' . $file->getFilename() . ']...');
if (in_array($file->getExtension(), $this->ignoredExtensions)) {
$this->comment('This is not an audio file! Skipping...' . PHP_EOL);
continue;
}
// Has this track already been imported?
$importedTrack = DB::table('mlpma_tracks')->where('filename', '=', $file->getFilename())->first();
if ($importedTrack) {
$this->comment('This track has already been imported! Skipping...' . PHP_EOL);
continue;
}
//==========================================================================================================
// Extract the original tags.
//==========================================================================================================
$getId3 = new getID3();
// all tags read by getID3, including the cover art
$allTags = $getId3->analyze($file->getPathname());
// tags specific to a file format (ID3 or Atom), pre-normalization but with cover art removed
$rawTags = [];
// normalized tags used by Pony.fm
$parsedTags = [];
if (Str::lower($file->getExtension()) === 'mp3') {
list($parsedTags, $rawTags) = $this->getId3Tags($allTags);
} elseif (Str::lower($file->getExtension()) === 'm4a') {
list($parsedTags, $rawTags) = $this->getAtomTags($allTags);
} elseif (Str::lower($file->getExtension()) === 'ogg') {
list($parsedTags, $rawTags) = $this->getVorbisTags($allTags);
} elseif (Str::lower($file->getExtension()) === 'flac') {
list($parsedTags, $rawTags) = $this->getVorbisTags($allTags);
} elseif (Str::lower($file->getExtension()) === 'wav') {
list($parsedTags, $rawTags) = $this->getAtomTags($allTags);
}
//==========================================================================================================
// Determine the release date.
//==========================================================================================================
$modifiedDate = Carbon::createFromTimeStampUTC(File::lastModified($file->getPathname()));
$taggedYear = $parsedTags['year'];
$this->info('Modification year: ' . $modifiedDate->year);
$this->info('Tagged year: ' . $taggedYear);
if ($taggedYear !== null && $modifiedDate->year === $taggedYear) {
$releasedAt = $modifiedDate;
} elseif ($taggedYear !== null && Str::length((string) $taggedYear) !== 4) {
$this->error('This track\'s tagged year makes no sense! Using the track\'s last modified date...');
$releasedAt = $modifiedDate;
} elseif ($taggedYear !== null && $modifiedDate->year !== $taggedYear) {
$this->error('Release years don\'t match! Using the tagged year...');
$releasedAt = Carbon::create($taggedYear);
} else {
// $taggedYear is null
$this->error('This track isn\'t tagged with its release year! Using the track\'s last modified date...');
$releasedAt = $modifiedDate;
}
// This is later used by the classification/publishing script to determine the publication date.
$parsedTags['released_at'] = $releasedAt->toDateTimeString();
//==========================================================================================================
// Does this track have vocals?
//==========================================================================================================
$isVocal = $parsedTags['lyrics'] !== null;
//==========================================================================================================
// Fill in the title tag if it's missing.
//==========================================================================================================
if (!$parsedTags['title']) {
$parsedTags['title'] = $file->getBasename('.' . $file->getExtension());
}
//==========================================================================================================
// Determine the genre.
//==========================================================================================================
$genreName = $parsedTags['genre'];
//.........这里部分代码省略.........
示例7: testStringQuickRandom
public function testStringQuickRandom()
{
$str1 = Str::quickRandom();
$str2 = Str::length($str1);
$this->assertEquals($str2, 16);
$str1 = Str::quickRandom(3);
$str2 = Str::length($str1);
$this->assertEquals($str2, 3);
}
示例8: length
/**
* Return the length of the given string
**/
public function length()
{
return Str::length($this->value);
}
示例9: length
/**
* Return the length of the given string.
*
* @return int
*/
public function length()
{
return Str::length($this->string);
}
示例10: parseDateString
/**
* Parses a potentially-partial date string into a proper date object.
*
* The tagging formats we deal with base their date format on ISO 8601, but
* the timestamp may be incomplete.
*
* @link https://code.google.com/p/mp4v2/wiki/iTunesMetadata
* @link https://wiki.xiph.org/VorbisComment#Date_and_time
* @link http://id3.org/id3v2.4.0-frames
*
* @param string $dateString
* @return null|Carbon
*/
protected function parseDateString(string $dateString)
{
switch (Str::length($dateString)) {
// YYYY
case 4:
return Carbon::createFromFormat('Y', $dateString)->month(1)->day(1);
// YYYY-MM
// YYYY-MM
case 7:
return Carbon::createFromFormat('Y-m', $dateString)->day(1);
// YYYY-MM-DD
// YYYY-MM-DD
case 10:
return Carbon::createFromFormat('Y-m-d', $dateString);
break;
default:
// We might have an ISO-8601 string in our hooves.
// If not, give up.
try {
return Carbon::createFromFormat(Carbon::ISO8601, $dateString);
} catch (\InvalidArgumentException $e) {
return null;
}
}
}