jatdung 2年前

修改理由:

存在未翻译的内容

详细描述:

补充翻译

相关信息:


此投稿由 MArtian 2年前 合并。

标题修改:

+ 辅助函数

内容修改:

红色背景 为原始内容

绿色背景 为新增或者修改的内容

OldNewDifferences
33- [简介](#introduction)
44- [可用方法](#available-methods)
55- [其他实用工具](#other-utilities)
6   - [Benchmarking](#benchmarking)
7   - [Pipeline](#pipeline)
8   - [Lottery](#lottery)
 6 - [Benchmarking](#benchmarking)
 7 - [Pipeline](#pipeline)
 8 - [Lottery](#lottery)
99
1010<a name="introduction"></a>
 11  
1112## 简介
1213
1314Laravel 包含各种各样的全局 PHP 「辅助」函数,框架本身也大量的使用了这些功能函数;如果你觉的方便,你可以在你的应用中任意使用这些函数。
1415
1516<a name="available-methods"></a>
 17  
1618## 可用方法
1719
1820<style>
1921   .collection-method-list > p {
2022       columns: 10.8em 3; -moz-columns: 10.8em 3; -webkit-columns: 10.8em 3;
2123   }
 24  
2225
2326   .collection-method-list a {
2427       display: block;
 
2629       text-overflow: ellipsis;
2730       white-space: nowrap;
2831   }
 32  
2933</style>
3034
3135<a name="arrays-and-objects-method-list"></a>
 36  
3237### 数组 & 对象
3338
3439<div class="collection-method-list" markdown="1">
 40  
3541
3642[Arr::accessible](#method-array-accessible)
3743[Arr::add](#method-array-add)
 
7884</div>
7985
8086<a name="paths-method-list"></a>
 87  
8188### 路径
8289
8390<div class="collection-method-list" markdown="1">
 91  
8492
8593[app_path](#method-app-path)
8694[base_path](#method-base-path)
 
97105
98106
99107<a name="strings-method-list"></a>
 108  
100109### 字符串
101110
102111<div class="collection-method-list" markdown="1">
 112  
103113
104114[\__](#method-__)
105115[class_basename](#method-class-basename)
 
173183</div>
174184
175185<a name="fluent-strings-method-list"></a>
 186  
176187### 字符流处理
177188
178189<div class="collection-method-list" markdown="1">
 190  
179191
180192[after](#method-fluent-str-after)
181193[afterLast](#method-fluent-str-after-last)
 
271283
272284
273285<a name="urls-method-list"></a>
 286  
274287### URLs
275288
276289<div class="collection-method-list" markdown="1">
 290  
277291
278292[action](#method-action)
279293[asset](#method-asset)
 
286300</div>
287301
288302<a name="miscellaneous-method-list"></a>
 303  
289304### 杂项
290305
291306<div class="collection-method-list" markdown="1">
 307  
292308
293309[abort](#method-abort)
294310[abort_if](#method-abort-if)
 
346362</div>
347363
348364<a name="method-listing"></a>
 365  
349366## 方法列表
350367
351368<style>
 
353370       font-size: 14px;
354371   }
355372
 373  
356374   .collection-method:not(.first-collection-method) {
357375       margin-top: 50px;
358376   }
 377  
359378</style>
360379
361380<a name="arrays"></a>
 381  
362382## 数组 & 对象
363383
364384<a name="method-array-accessible"></a>
 385  
365386#### `Arr::accessible()` {.collection-method .first-collection-method}
366387
367 `Arr::accessible` 函数检查给定的值是否可被数组式访问:
 388`Arr::accessible` 方法检查给定的值是否可被数组式访问:
368389
369390   use Illuminate\Support\Arr;
370391   use Illuminate\Support\Collection;
371 
 392   
372393   $isAccessible = Arr::accessible(['a' => 1, 'b' => 2]);
373 
 394   
374395   // true
375 
 396   
376397   $isAccessible = Arr::accessible(new Collection);
377 
 398   
378399   // true
379 
 400   
380401   $isAccessible = Arr::accessible('abc');
381 
 402   
382403   // false
383 
 404   
384405   $isAccessible = Arr::accessible(new stdClass);
385 
 406   
386407   // false
387408
388409<a name="method-array-add"></a>
 410  
389411#### `Arr::add()` {.collection-method}
390412
391 如果给定的键名在数组中不存在键值或该键值设置为 `null` ,那么 `Arr::add` 函数将会把给定的键值对添加到数组中:
392 
393    use Illuminate\Support\Arr;
394 
 413如果给定的键名在数组中不存在键值或该键值设置为 `null` ,那么 `Arr::add` 方法将会把给定的键值对添加到数组中:
 414
 415   use Illuminate\Support\Arr;
 416   
395417   $array = Arr::add(['name' => 'Desk'], 'price', 100);
396 
 418   
397419   // ['name' => 'Desk', 'price' => 100]
398 
 420   
399421   $array = Arr::add(['name' => 'Desk', 'price' => null], 'price', 100);
400 
 422   
401423   // ['name' => 'Desk', 'price' => 100]
402424
403425
404426
405427
406428<a name="method-array-collapse"></a>
 429  
407430#### `Arr::collapse()` {.collection-method}
408431
409 `Arr::collapse` 函数将多个数组合并为一个数组:
410 
411    use Illuminate\Support\Arr;
412 
 432`Arr::collapse` 方法将多个数组合并为一个数组:
 433
 434   use Illuminate\Support\Arr;
 435   
413436   $array = Arr::collapse([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
414 
 437   
415438   // [1, 2, 3, 4, 5, 6, 7, 8, 9]
416439
417440<a name="method-array-crossjoin"></a>
 441  
418442#### `Arr::crossJoin()` {.collection-method}
419443
420 `Arr::crossJoin` 函数交叉连接给定的数组,返回具有所有可能排列的笛卡尔乘积:
421 
422    use Illuminate\Support\Arr;
423 
 444`Arr::crossJoin` 方法交叉连接给定的数组,返回具有所有可能排列的笛卡尔乘积:
 445
 446   use Illuminate\Support\Arr;
 447   
424448   $matrix = Arr::crossJoin([1, 2], ['a', 'b']);
425 
 449   
426450   /*
427451       [
428452           [1, 'a'],
 
431455           [2, 'b'],
432456       ]
433457   */
434 
 458   
435459   $matrix = Arr::crossJoin([1, 2], ['a', 'b'], ['I', 'II']);
436 
 460   
437461   /*
438462       [
439463           [1, 'a', 'I'],
 
448472   */
449473
450474<a name="method-array-divide"></a>
 475  
451476#### `Arr::divide()` {.collection-method}
452477
453 `Arr::divide` 函数返回一个二维数组,一个值包含原数组的键,另一个值包含原数组的值:
454 
455    use Illuminate\Support\Arr;
456 
 478`Arr::divide` 方法返回一个二维数组,一个值包含原数组的键,另一个值包含原数组的值:
 479
 480   use Illuminate\Support\Arr;
 481   
457482   [$keys, $values] = Arr::divide(['name' => 'Desk']);
458 
 483   
459484   // $keys: ['name']
460 
 485   
461486   // $values: ['Desk']
462487
463488<a name="method-array-dot"></a>
 489  
464490#### `Arr::dot()` {.collection-method}
465491
466 `Arr::dot` 函数将多维数组中所有的键平铺到一维数组中,新数组使用「.」符号表示层级包含关系:
467 
468    use Illuminate\Support\Arr;
469 
 492`Arr::dot` 方法将多维数组中所有的键平铺到一维数组中,新数组使用「.」符号表示层级包含关系:
 493
 494   use Illuminate\Support\Arr;
 495   
470496   $array = ['products' => ['desk' => ['price' => 100]]];
471 
 497   
472498   $flattened = Arr::dot($array);
473 
 499   
474500   // ['products.desk.price' => 100]
475501
476502<a name="method-array-except"></a>
 503  
477504#### `Arr::except()` {.collection-method}
478505
479 `Arr::except` 函数从数组中删除指定的键值对:
480 
481    use Illuminate\Support\Arr;
482 
 506`Arr::except` 方法从数组中删除指定的键值对:
 507
 508   use Illuminate\Support\Arr;
 509   
483510   $array = ['name' => 'Desk', 'price' => 100];
484 
 511   
485512   $filtered = Arr::except($array, ['price']);
486 
 513   
487514   // ['name' => 'Desk']
488515
489516<a name="method-array-exists"></a>
 517  
490518#### `Arr::exists()` {.collection-method}
491519
492 `Arr::exists` 检查给定的键是否存在提供的数组中:
493 
494    use Illuminate\Support\Arr;
495 
 520`Arr::exists` 方法检查给定的键是否存在提供的数组中:
 521
 522   use Illuminate\Support\Arr;
 523   
496524   $array = ['name' => 'John Doe', 'age' => 17];
497 
 525   
498526   $exists = Arr::exists($array, 'name');
499 
 527   
500528   // true
501 
 529   
502530   $exists = Arr::exists($array, 'salary');
503 
 531   
504532   // false
505533
506534<a name="method-array-first"></a>
 
508536
509537#### `Arr::first()` {.collection-method}
510538
511 `Arr::first` 函数返回数组中满足指定条件的第一个元素:
512 
513    use Illuminate\Support\Arr;
514 
 539`Arr::first` 方法返回数组中满足指定条件的第一个元素:
 540
 541   use Illuminate\Support\Arr;
 542   
515543   $array = [100, 200, 300];
516 
 544   
517545   $first = Arr::first($array, function (int $value, int $key) {
518546       return $value >= 150;
519547   });
520 
 548   
521549   // 200
522550
523551可将默认值作为第三个参数传递给该方法,如果数组中没有值满足指定条件,则返回该默认值:
524552
525553   use Illuminate\Support\Arr;
526 
 554   
527555   $first = Arr::first($array, $callback, $default);
528556
529557<a name="method-array-flatten"></a>
 558  
530559#### `Arr::flatten()` {.collection-method}
531560
532 `Arr::flatten` 函数将多维数组中数组的值取出平铺为一维数组:
533 
534    use Illuminate\Support\Arr;
535 
 561`Arr::flatten` 方法将多维数组中数组的值取出平铺为一维数组:
 562
 563   use Illuminate\Support\Arr;
 564   
536565   $array = ['name' => 'Joe', 'languages' => ['PHP', 'Ruby']];
537 
 566   
538567   $flattened = Arr::flatten($array);
539 
 568   
540569   // ['Joe', 'PHP', 'Ruby']
541570
542571<a name="method-array-forget"></a>
 572  
543573#### `Arr::forget()` {.collection-method}
544574
545 `Arr::forget` 函数使用「.」符号从深度嵌套的数组中删除给定的键值对:
546 
547    use Illuminate\Support\Arr;
548 
 575`Arr::forget` 方法使用「.」符号从深度嵌套的数组中删除给定的键值对:
 576
 577   use Illuminate\Support\Arr;
 578   
549579   $array = ['products' => ['desk' => ['price' => 100]]];
550 
 580   
551581   Arr::forget($array, 'products.desk');
552 
 582   
553583   // ['products' => []]
554584
555585<a name="method-array-get"></a>
 586  
556587#### `Arr::get()` {.collection-method}
557588
558 `Arr::get` 函数使用「.」符号从深度嵌套的数组中根据指定键检索值:
559 
560    use Illuminate\Support\Arr;
561 
 589`Arr::get` 方法使用「.」符号从深度嵌套的数组中根据指定键检索值:
 590
 591   use Illuminate\Support\Arr;
 592   
562593   $array = ['products' => ['desk' => ['price' => 100]]];
563 
 594   
564595   $price = Arr::get($array, 'products.desk.price');
565 
 596   
566597   // 100
567598
568 `Arr::get` 函数也可以接受一个默认值,如果数组中不存在指定的键,则返回默认值:
569 
570    use Illuminate\Support\Arr;
571 
 599`Arr::get` 方法也可以接受一个默认值,如果数组中不存在指定的键,则返回默认值:
 600
 601   use Illuminate\Support\Arr;
 602   
572603   $discount = Arr::get($array, 'products.desk.discount', 0);
573 
 604   
574605   // 0
575606
576607<a name="method-array-has"></a>
 608  
577609#### `Arr::has()` {.collection-method}
578610
579 `Arr::has` 函数使用「.」符号判断数组中是否存在指定的一个或多个键:
580 
581    use Illuminate\Support\Arr;
582 
 611`Arr::has` 方法使用「.」符号判断数组中是否存在指定的一个或多个键:
 612
 613   use Illuminate\Support\Arr;
 614   
583615   $array = ['product' => ['name' => 'Desk', 'price' => 100]];
584 
 616   
585617   $contains = Arr::has($array, 'product.name');
586 
 618   
587619   // true
588 
 620   
589621   $contains = Arr::has($array, ['product.price', 'product.discount']);
590 
 622   
591623   // false
592624
593625
594626
595627<a name="method-array-hasany"></a>
 628  
596629#### `Arr::hasAny()` {.collection-method}
597630
598 `Arr::hasAny` 函数使用「.」符号判断给定集合中的任一值是否存在于数组中:
599 
600    use Illuminate\Support\Arr;
601 
 631`Arr::hasAny` 方法使用「.」符号判断给定集合中的任一值是否存在于数组中:
 632
 633   use Illuminate\Support\Arr;
 634   
602635   $array = ['product' => ['name' => 'Desk', 'price' => 100]];
603 
 636   
604637   $contains = Arr::hasAny($array, 'product.name');
605 
 638   
606639   // true
607 
 640   
608641   $contains = Arr::hasAny($array, ['product.name', 'product.discount']);
609 
 642   
610643   // true
611 
 644   
612645   $contains = Arr::hasAny($array, ['category', 'product.discount']);
613 
 646   
614647   // false
615648
616649<a name="method-array-isassoc"></a>
 650  
617651#### `Arr::isAssoc()` {.collection-method}
618652
619 如果给定数组是关联数组,则 `Arr::isAssoc` 函数返回 `true`,如果该数组没有以零开头的顺序数字键,则将其视为「关联」数组:
620 
621    use Illuminate\Support\Arr;
622 
 653如果给定数组是关联数组,则 `Arr::isAssoc` 方法返回 `true`,如果该数组没有以零开头的顺序数字键,则将其视为「关联」数组:
 654
 655   use Illuminate\Support\Arr;
 656   
623657   $isAssoc = Arr::isAssoc(['product' => ['name' => 'Desk', 'price' => 100]]);
624 
 658   
625659   // true
626 
 660   
627661   $isAssoc = Arr::isAssoc([1, 2, 3]);
628 
 662   
629663   // false
630664
631665<a name="method-array-islist"></a>
 666  
632667#### `Arr::isList()` {.collection-method}
633668
634669如果给定数组的键是从零开始的连续整数,则 `Arr::isList` 方法返回 `true`:
635670
636671   use Illuminate\Support\Arr;
637 
 672   
638673   $isList = Arr::isList(['foo', 'bar', 'baz']);
639 
 674   
640675   // true
641 
 676   
642677   $isList = Arr::isList(['product' => ['name' => 'Desk', 'price' => 100]]);
643 
 678   
644679   // false
645680
646681<a name="method-array-join"></a>
 682  
647683#### `Arr::join()` {.collection-method}
648684
649685`Arr::join()`方法将给定数组的所有值通过给定字符串连接起来。使用此方法的第二个参数,您还可以为数组中的最后一个元素指定连接的字符串:
650686
651687   use Illuminate\Support\Arr;
652 
 688   
653689   $array = ['Tailwind', 'Alpine', 'Laravel', 'Livewire'];
654 
 690   
655691   $joined = Arr::join($array, ', ');
656 
 692   
657693   // Tailwind, Alpine, Laravel, Livewire
658 
 694   
659695   $joined = Arr::join($array, ', ', ' and ');
660 
 696   
661697   // Tailwind, Alpine, Laravel and Livewire
662698
663699<a name="method-array-keyby"></a>
 700  
664701#### `Arr::keyBy()` {.collection-method}
665702
666703`Arr::keyBy()`方法通过给定键名的值对该数组进行重组。如果数组中存在多个相同的值,则只有最后一个值会出现在新数组中:
667704
668705   use Illuminate\Support\Arr;
669 
 706   
670707   $array = [
671708       ['product_id' => 'prod-100', 'name' => 'Desk'],
672709       ['product_id' => 'prod-200', 'name' => 'Chair'],
673710   ];
674 
 711   
675712   $keyed = Arr::keyBy($array, 'product_id');
676 
 713   
677714   /*
678715       [
679716           'prod-100' => ['product_id' => 'prod-100', 'name' => 'Desk'],
 
684721
685722
686723<a name="method-array-last"></a>
 724  
687725#### `Arr::last()` {.collection-method}
688726
689727`Arr::last` 方法返回数组中满足指定条件的最后一个元素:
690728
691729   use Illuminate\Support\Arr;
692 
 730   
693731   $array = [100, 200, 300, 110];
694 
 732   
695733   $last = Arr::last($array, function (int $value, int $key) {
696734       return $value >= 150;
697735   });
698 
 736   
699737   // 300
700738
701739将默认值作为第三个参数传递给该方法,如果没有值满足条件,则返回该默认值:
702740
703741   use Illuminate\Support\Arr;
704 
 742   
705743   $last = Arr::last($array, $callback, $default);
706744
707745<a name="method-array-map"></a>
 746  
708747#### `Arr::map()` {.collection-method}
709748
710749`Arr::map` 方法用来遍历数组,并将每个值和键传递给给定的回调。数组值由回调返回的值替换:
711750
712751   use Illuminate\Support\Arr;
713 
 752   
714753   $array = ['first' => 'james', 'last' => 'kirk'];
715 
 754   
716755   $mapped = Arr::map($array, function (string $value, string $key) {
717756       return ucfirst($value);
718757   });
719 
 758   
720759   // ['first' => 'James', 'last' => 'Kirk']
721760
722761<a name="method-array-only"></a>
 762  
723763#### `Arr::only()` {.collection-method}
724764
725765`Arr::only` 方法仅返回给定数组中的指定键/值对:
726766
727767   use Illuminate\Support\Arr;
728 
 768   
729769   $array = ['name' => 'Desk', 'price' => 100, 'orders' => 10];
730 
 770   
731771   $slice = Arr::only($array, ['name', 'price']);
732 
 772   
733773   // ['name' => 'Desk', 'price' => 100]
734774
735775<a name="method-array-pluck"></a>
 776  
736777#### `Arr::pluck()` {.collection-method}
737778
738779`Arr::pluck` 方法从数组中检索给定键的所有值:
739780
740781   use Illuminate\Support\Arr;
741 
 782   
742783   $array = [
743784       ['developer' => ['id' => 1, 'name' => 'Taylor']],
744785       ['developer' => ['id' => 2, 'name' => 'Abigail']],
745786   ];
746 
 787   
747788   $names = Arr::pluck($array, 'developer.name');
748 
 789   
749790   // ['Taylor', 'Abigail']
750791
751792你也可以指定结果的键:
752793
753794   use Illuminate\Support\Arr;
754 
 795   
755796   $names = Arr::pluck($array, 'developer.name', 'developer.id');
756 
 797   
757798   // [1 => 'Taylor', 2 => 'Abigail']
758799
759800<a name="method-array-prepend"></a>
760801
761802
762803<a name="method-array-last"></a>
 804  
763805#### `Arr::last()` {.collection-method}
764806
765 `Arr::last` 函数返回数组中满足指定条件的最后一个元素:
766 
767    use Illuminate\Support\Arr;
768 
 807`Arr::last` 方法返回数组中满足指定条件的最后一个元素:
 808
 809   use Illuminate\Support\Arr;
 810   
769811   $array = [100, 200, 300, 110];
770 
 812   
771813   $last = Arr::last($array, function (int $value, int $key) {
772814       return $value >= 150;
773815   });
774 
 816   
775817   // 300
776818
777819将默认值作为第三个参数传递给该方法,如果没有值满足指定条件,则返回该默认值:
778820
779821   use Illuminate\Support\Arr;
780 
 822   
781823   $last = Arr::last($array, $callback, $default);
782824
783825<a name="method-array-map"></a>
 826  
784827#### `Arr::map()` {.collection-method}
785828
786829`Arr::map` 方法遍历数组并将每个键和值传递至给定的回调方法。数组的值将替换为该回调方法返回的值:
787830
788831   use Illuminate\Support\Arr;
789 
 832   
790833   $array = ['first' => 'james', 'last' => 'kirk'];
791 
 834   
792835   $mapped = Arr::map($array, function (string $value, string $key) {
793836       return ucfirst($value);
794837   });
795 
 838   
796839   // ['first' => 'James', 'last' => 'Kirk']
797840
798841<a name="method-array-only"></a>
 842  
799843#### `Arr::only()` {.collection-method}
800844
801 `Arr::only` 函数只返回给定数组中指定的键值对:
802 
803    use Illuminate\Support\Arr;
804 
 845`Arr::only` 方法只返回给定数组中指定的键值对:
 846
 847   use Illuminate\Support\Arr;
 848   
805849   $array = ['name' => 'Desk', 'price' => 100, 'orders' => 10];
806 
 850   
807851   $slice = Arr::only($array, ['name', 'price']);
808 
 852   
809853   // ['name' => 'Desk', 'price' => 100]
810854
811855<a name="method-array-pluck"></a>
 856  
812857#### `Arr::pluck()` {.collection-method}
813858
814 `Arr::pluck` 函数从数组中检索给定键的所有值:
815 
816    use Illuminate\Support\Arr;
817 
 859`Arr::pluck` 方法从数组中检索给定键的所有值:
 860
 861   use Illuminate\Support\Arr;
 862   
818863   $array = [
819864       ['developer' => ['id' => 1, 'name' => 'Taylor']],
820865       ['developer' => ['id' => 2, 'name' => 'Abigail']],
821866   ];
822 
 867   
823868   $names = Arr::pluck($array, 'developer.name');
824 
 869   
825870   // ['Taylor', 'Abigail']
826871
827872你还可以指定结果的键:
828873
829874   use Illuminate\Support\Arr;
830 
 875   
831876   $names = Arr::pluck($array, 'developer.name', 'developer.id');
832 
 877   
833878   // [1 => 'Taylor', 2 => 'Abigail']
834879
835880<a name="method-array-prepend"></a>
 
837882
838883#### `Arr::prepend()` {.collection-method}
839884
840 `Arr::prepend` 函数将一个值插入到数组的开始位置:
841 
842    use Illuminate\Support\Arr;
843 
 885`Arr::prepend` 方法将一个值插入到数组的开始位置:
 886
 887   use Illuminate\Support\Arr;
 888   
844889   $array = ['one', 'two', 'three', 'four'];
845 
 890   
846891   $array = Arr::prepend($array, 'zero');
847 
 892   
848893   // ['zero', 'one', 'two', 'three', 'four']
849894
850895你也可以指定插入值的键:
851896
852897   use Illuminate\Support\Arr;
853 
 898   
854899   $array = ['price' => 100];
855 
 900   
856901   $array = Arr::prepend($array, 'Desk', 'name');
857 
 902   
858903   // ['name' => 'Desk', 'price' => 100]
859904
860905<a name="method-array-prependkeyswith"></a>
 906  
861907#### `Arr::prependKeysWith()` {.collection-method}
862908
863 `Arr::prependKeysWith` 函数为关联数组中的所有键添加给定前缀:
864 
865    use Illuminate\Support\Arr;
866 
 909`Arr::prependKeysWith` 方法为关联数组中的所有键添加给定前缀:
 910
 911   use Illuminate\Support\Arr;
 912   
867913   $array = [
868914       'name' => 'Desk',
869915       'price' => 100,
870916   ];
871 
 917   
872918   $keyed = Arr::prependKeysWith($array, 'product.');
873 
 919   
874920   /*
875921       [
876922           'product.name' => 'Desk',
 
879925   */
880926
881927<a name="method-array-pull"></a>
 928  
882929#### `Arr::pull()` {.collection-method}
883930
884 `Arr::pull` 函数从数组中返回指定键的值并删除此键值对:
885 
886    use Illuminate\Support\Arr;
887 
 931`Arr::pull` 方法从数组中返回指定键的值并删除此键值对:
 932
 933   use Illuminate\Support\Arr;
 934   
888935   $array = ['name' => 'Desk', 'price' => 100];
889 
 936   
890937   $name = Arr::pull($array, 'name');
891 
 938   
892939   // $name: Desk
893 
 940   
894941   // $array: ['price' => 100]
895942
896943默认值可以作为第三个参数传递给该方法。如果指定键不存在,则返回该值:
897944
898945   use Illuminate\Support\Arr;
899 
 946   
900947   $value = Arr::pull($array, $key, $default);
901948
902949<a name="method-array-query"></a>
 950  
903951#### `Arr::query()` {.collection-method}
904952
905 `Arr::query` 函数将数组转换为查询字符串:
906 
907    use Illuminate\Support\Arr;
908 
 953`Arr::query` 方法将数组转换为查询字符串:
 954
 955   use Illuminate\Support\Arr;
 956   
909957   $array = [
910958       'name' => 'Taylor',
911959       'order' => [
 
913961           'direction' => 'desc'
914962       ]
915963   ];
916 
 964   
917965   Arr::query($array);
918 
 966   
919967   // name=Taylor&order[column]=created_at&order[direction]=desc
920968
921969<a name="method-array-random"></a>
 970  
922971#### `Arr::random()` {.collection-method}
923972
924 `Arr::random` 函数从数组中随机返回一个值:
925 
926    use Illuminate\Support\Arr;
927 
 973`Arr::random` 方法从数组中随机返回一个值:
 974
 975   use Illuminate\Support\Arr;
 976   
928977   $array = [1, 2, 3, 4, 5];
929 
 978   
930979   $random = Arr::random($array);
931 
 980   
932981   // 4 - (retrieved randomly)
933982
934983你还可以指定返回值的数量作为可选的第二个参数传递给该方法,请注意,提供这个参数会返回一个数组,即使是你只需要一项:
935984
936985   use Illuminate\Support\Arr;
937 
 986   
938987   $items = Arr::random($array, 2);
939 
 988   
940989   // [2, 5] - (retrieved randomly)
941990
942991
943992
944993<a name="method-array-set"></a>
 994  
945995#### `Arr::set()` {.collection-method}
946996
947 `Arr::set` 函数使用「.」符号在多维数组中设置指定键的值:
948 
949    use Illuminate\Support\Arr;
950 
 997`Arr::set` 方法使用「.」符号在多维数组中设置指定键的值:
 998
 999   use Illuminate\Support\Arr;
 1000   
9511001   $array = ['products' => ['desk' => ['price' => 100]]];
952 
 1002   
9531003   Arr::set($array, 'products.desk.price', 200);
954 
 1004   
9551005   // ['products' => ['desk' => ['price' => 200]]]
9561006
9571007<a name="method-array-shuffle"></a>
 1008  
9581009#### `Arr::shuffle()` {.collection-method}
9591010
960 `Arr::shuffle` 函数将数组中值进行随机排序:
961 
962    use Illuminate\Support\Arr;
963 
 1011`Arr::shuffle` 方法将数组中值进行随机排序:
 1012
 1013   use Illuminate\Support\Arr;
 1014   
9641015   $array = Arr::shuffle([1, 2, 3, 4, 5]);
965 
 1016   
9661017   // [3, 2, 5, 1, 4] - (generated randomly)
9671018
9681019<a name="method-array-sort"></a>
 1020  
9691021#### `Arr::sort()` {.collection-method}
9701022
971 `Arr::sort` 函数根据给定数组的值进行升序排序:
972 
973    use Illuminate\Support\Arr;
974 
 1023`Arr::sort` 方法根据给定数组的值进行升序排序:
 1024
 1025   use Illuminate\Support\Arr;
 1026   
9751027   $array = ['Desk', 'Table', 'Chair'];
976 
 1028   
9771029   $sorted = Arr::sort($array);
978 
 1030   
9791031   // ['Chair', 'Desk', 'Table']
9801032
981 你还可以根据给定回调函数的返回结果对数组进行排序:
982 
983    use Illuminate\Support\Arr;
984 
 1033你还可以根据给定回调方法的返回结果对数组进行排序:
 1034
 1035   use Illuminate\Support\Arr;
 1036   
9851037   $array = [
9861038       ['name' => 'Desk'],
9871039       ['name' => 'Table'],
9881040       ['name' => 'Chair'],
9891041   ];
990 
 1042   
9911043   $sorted = array_values(Arr::sort($array, function (array $value) {
9921044       return $value['name'];
9931045   }));
994 
 1046   
9951047   /*
9961048       [
9971049           ['name' => 'Chair'],
 
10011053   */
10021054
10031055<a name="method-array-sort-desc"></a>
 1056  
10041057#### `Arr::sortDesc()` {.collection-method}
10051058
1006 `Arr::sortDesc` 函数根据给定数组的值进行降序排序:
1007 
1008    use Illuminate\Support\Arr;
1009 
 1059`Arr::sortDesc` 方法根据给定数组的值进行降序排序:
 1060
 1061   use Illuminate\Support\Arr;
 1062   
10101063   $array = ['Desk', 'Table', 'Chair'];
1011 
 1064   
10121065   $sorted = Arr::sortDesc($array);
1013 
 1066   
10141067   // ['Table', 'Desk', 'Chair']
10151068
1016 你还可以根据给定回调函数的返回结果对数组进行排序:
1017 
1018    use Illuminate\Support\Arr;
1019 
 1069你还可以根据给定回调方法的返回结果对数组进行排序:
 1070
 1071   use Illuminate\Support\Arr;
 1072   
10201073   $array = [
10211074       ['name' => 'Desk'],
10221075       ['name' => 'Table'],
10231076       ['name' => 'Chair'],
10241077   ];
1025 
 1078   
10261079   $sorted = array_values(Arr::sortDesc($array, function (array $value) {
10271080       return $value['name'];
10281081   }));
1029 
 1082   
10301083   /*
10311084       [
10321085           ['name' => 'Table'],
 
10361089   */
10371090
10381091<a name="method-array-sort-recursive"></a>
 1092  
10391093#### `Arr::sortRecursive()` {.collection-method}
10401094
1041 `Arr::sortRecursive` 函数对给定数组进行递归排序,使用 `sort` 函数对数字索引子数组进行按值升序排序,使用 `ksort` 函数对关联子数组进行按键升序排序:
1042 
1043    use Illuminate\Support\Arr;
1044 
 1095`Arr::sortRecursive` 方法对给定数组进行递归排序,使用 `sort` 方法对数字索引子数组进行按值升序排序,使用 `ksort` 方法对关联子数组进行按键升序排序:
 1096
 1097   use Illuminate\Support\Arr;
 1098   
10451099   $array = [
10461100       ['Roman', 'Taylor', 'Li'],
10471101       ['PHP', 'Ruby', 'JavaScript'],
10481102       ['one' => 1, 'two' => 2, 'three' => 3],
10491103   ];
1050 
 1104   
10511105   $sorted = Arr::sortRecursive($array);
1052 
 1106   
10531107   /*
10541108       [
10551109           ['JavaScript', 'PHP', 'Ruby'],
 
10611115
10621116
10631117<a name="method-array-to-css-classes"></a>
 1118  
10641119#### `Arr::toCssClasses()` {.collection-method}
10651120
1066 `Arr::toCssClasses` 函数根据给定的条件编译并返回 CSS 类字符串。该方法接受一个类数组,其中数组键包含你希望添加的一个或多个 CSS Class,而值是一个布尔表达式。如果数组元素有一个数字键,它将始终包含在呈现的类列表中:
1067 
1068    use Illuminate\Support\Arr;
1069 
 1121`Arr::toCssClasses` 方法根据给定的条件编译并返回 CSS 类字符串。该方法接受一个类数组,其中数组键包含你希望添加的一个或多个 CSS Class,而值是一个布尔表达式。如果数组元素有一个数字键,它将始终包含在呈现的类列表中:
 1122
 1123   use Illuminate\Support\Arr;
 1124   
10701125   $isActive = false;
10711126   $hasError = true;
1072 
 1127   
10731128   $array = ['p-4', 'font-bold' => $isActive, 'bg-red' => $hasError];
1074 
 1129   
10751130   $classes = Arr::toCssClasses($array);
1076 
 1131   
10771132   /*
10781133       'p-4 bg-red'
10791134   */
10801135
1081 Laravel 基于该函数实现 [Blade组件里的条件合并类](/docs/laravel/10.x/blade#conditionally-merge-classes) 以及 `@class` [Blade指令](/docs/laravel/10.x/blade#conditional-classes):
 1136Laravel 基于该方法实现 [Blade组件里的条件合并类](/docs/laravel/10.x/blade#conditionally-merge-classes) 以及 `@class` [Blade指令](/docs/laravel/10.x/blade#conditional-classes):
10821137
10831138<a name="method-array-undot"></a>
 1139  
10841140#### `Arr::undot()` {.collection-method}
10851141
1086 `Arr::undot` 函数使用「.」符号将一维数组扩展为多维数组:
1087 
1088    use Illuminate\Support\Arr;
1089 
 1142`Arr::undot` 方法使用「.」符号将一维数组扩展为多维数组:
 1143
 1144   use Illuminate\Support\Arr;
 1145   
10901146   $array = [
10911147       'user.name' => 'Kevin Malone',
10921148       'user.occupation' => 'Accountant',
10931149   ];
1094 
 1150   
10951151   $array = Arr::undot($array);
1096 
 1152   
10971153   // ['user' => ['name' => 'Kevin Malone', 'occupation' => 'Accountant']]
10981154
10991155<a name="method-array-where"></a>
 1156  
11001157#### `Arr::where()` {.collection-method}
11011158
1102 `Arr::where` 函数使用给定的回调函数返回的结果过滤数组:
1103 
1104    use Illuminate\Support\Arr;
1105 
 1159`Arr::where` 方法使用给定的回调函数返回的结果过滤数组:
 1160
 1161   use Illuminate\Support\Arr;
 1162   
11061163   $array = [100, '200', 300, '400', 500];
1107 
 1164   
11081165   $filtered = Arr::where($array, function (string|int $value, int $key) {
11091166       return is_string($value);
11101167   });
1111 
 1168   
11121169   // [1 => '200', 3 => '400']
11131170
11141171<a name="method-array-where-not-null"></a>
 1172  
11151173#### `Arr::whereNotNull()` {.collection-method}
11161174
1117 `Arr::whereNotNull` 函数将从给定数组中删除所有 `null` 值:
1118 
1119    use Illuminate\Support\Arr;
1120 
 1175`Arr::whereNotNull` 方法将从给定数组中删除所有 `null` 值:
 1176
 1177   use Illuminate\Support\Arr;
 1178   
11211179   $array = [0, null];
1122 
 1180   
11231181   $filtered = Arr::whereNotNull($array);
1124 
 1182   
11251183   // [0 => 0]
11261184
11271185<a name="method-array-wrap"></a>
 1186  
11281187#### `Arr::wrap()` {.collection-method}
11291188
1130 `Arr::wrap` 函数可以将给定值转换为一个数组,如果给定的值已经是一个数组,它将原样返回:
1131 
1132    use Illuminate\Support\Arr;
1133 
 1189`Arr::wrap` 方法可以将给定值转换为一个数组,如果给定的值已经是一个数组,它将原样返回:
 1190
 1191   use Illuminate\Support\Arr;
 1192   
11341193   $string = 'Laravel';
1135 
 1194   
11361195   $array = Arr::wrap($string);
1137 
 1196   
11381197   // ['Laravel']
11391198
11401199
 
11421201如果给定值是 `null` ,将返回一个空数组:
11431202
11441203   use Illuminate\Support\Arr;
1145 
 1204   
11461205   $array = Arr::wrap(null);
1147 
 1206   
11481207   // []
11491208
11501209<a name="method-data-fill"></a>
 1210  
11511211#### `data_fill()` {#collection-method}
11521212
11531213`data_fill` 函数使用「.」符号给多维数组或对象设置缺少的值:
11541214
11551215   $data = ['products' => ['desk' => ['price' => 100]]];
1156 
 1216   
11571217   data_fill($data, 'products.desk.price', 200);
1158 
 1218   
11591219   // ['products' => ['desk' => ['price' => 100]]]
1160 
 1220   
11611221   data_fill($data, 'products.desk.discount', 10);
1162 
 1222   
11631223   // ['products' => ['desk' => ['price' => 100, 'discount' => 10]]]
11641224
11651225也可以接收 「\*」 作为通配符,设置相应缺少的值:
 
11701230           ['name' => 'Desk 2'],
11711231       ],
11721232   ];
1173 
 1233   
11741234   data_fill($data, 'products.*.price', 200);
1175 
 1235   
11761236   /*
11771237       [
11781238           'products' => [
 
11831243   */
11841244
11851245<a name="method-data-get"></a>
 1246  
11861247#### `data_get()` {#collection-method}
11871248
11881249`data_get` 函数使用 「.」 符号从多维数组或对象中根据指定键检索值
11891250
11901251   $data = ['products' => ['desk' => ['price' => 100]]];
1191 
 1252   
11921253   $price = data_get($data, 'products.desk.price');
1193 
 1254   
11941255   // 100
11951256
11961257`data_get` 函数也接受一个默认值,如果没有找到指定的键,将返回默认值:
11971258
11981259   $discount = data_get($data, 'products.desk.discount', 0);
1199 
 1260   
12001261   // 0
12011262
12021263该函数还接受「\*」作为通配符,来指向数组或对象的任何键:
 
12051266       'product-one' => ['name' => 'Desk 1', 'price' => 100],
12061267       'product-two' => ['name' => 'Desk 2', 'price' => 150],
12071268   ];
1208 
 1269   
12091270   data_get($data, '*.name');
1210 
 1271   
12111272   // ['Desk 1', 'Desk 2'];
12121273
12131274<a name="method-data-set"></a>
 1275  
12141276#### `data_set()` {#collection-method}
12151277
12161278`data_set` 函数使用「.」符号从多维数组或对象中根据指定键设置值:
12171279
12181280   $data = ['products' => ['desk' => ['price' => 100]]];
1219 
 1281   
12201282   data_set($data, 'products.desk.price', 200);
1221 
 1283   
12221284   // ['products' => ['desk' => ['price' => 200]]]
12231285
12241286同 `data_get` 一样,函数也支持使用「\*」 作为通配符给相应键名赋值:
 
12291291           ['name' => 'Desk 2', 'price' => 150],
12301292       ],
12311293   ];
1232 
 1294   
12331295   data_set($data, 'products.*.price', 200);
1234 
 1296   
12351297   /*
12361298       [
12371299           'products' => [
 
12461308通常情况下,已存在的值将会被覆盖。如果只是希望设置一个目前不存在的值,你可以增加一个 `false` 作为函数的第四个参数:
12471309
12481310   $data = ['products' => ['desk' => ['price' => 100]]];
1249 
 1311   
12501312   data_set($data, 'products.desk.price', 200, overwrite: false);
1251 
 1313   
12521314   // ['products' => ['desk' => ['price' => 100]]]
12531315
12541316<a name="method-head"></a>
 1317  
12551318#### `head()` {#collection-method}
12561319
12571320`head` 函数将返回数组中的第一个值:
12581321
12591322   $array = [100, 200, 300];
1260 
 1323   
12611324   $first = head($array);
1262 
 1325   
12631326   // 100
12641327
12651328<a name="method-last"></a>
 1329  
12661330#### `last()` {.collection-method}
12671331
12681332`last` 函数将返回数组中的最后一个值:
12691333
12701334   $array = [100, 200, 300];
1271 
 1335   
12721336   $last = last($array);
1273 
 1337   
12741338   // 300
12751339
12761340<a name="paths"></a>
 1341  
12771342## 路径
12781343
12791344<a name="method-app-path"></a>
 1345  
12801346#### `app_path()` {.collection-method}
12811347
12821348`app_path` 函数返回 `app` 目录的完整路径。你也可以使用 `app_path` 函数来生成应用目录下特定文件的完整路径:
12831349
12841350   $path = app_path();
1285 
 1351   
12861352   $path = app_path('Http/Controllers/Controller.php');
12871353
12881354<a name="method-base-path"></a>
 1355  
12891356#### `base_path()` {#collection-method}
12901357
12911358`base_path` 函数返回项目根目录的完整路径。你也可以使用 `base_path` 函数生成项目根目录下特定文件的完整路径:
12921359
12931360   $path = base_path();
1294 
 1361   
12951362   $path = base_path('vendor/bin');
12961363
12971364<a name="method-config-path"></a>
 1365  
12981366#### `config_path()` {#collection-method}
12991367
13001368`config_path` 函数返回项目配置目录 (config) 的完整路径。你也可以使用 `config_path` 函数来生成应用配置目录中的特定文件的完整路径:
13011369
13021370   $path = config_path();
1303 
 1371   
13041372   $path = config_path('app.php');
13051373
13061374<a name="method-database-path"></a>
 
13111379`database_path` 函数返回 `database` 目录的完整路径。你可以使用 `database_path` 函数来生成数据库目录下指定文件的完整路径:
13121380
13131381   $path = database_path();
1314 
 1382   
13151383   $path = database_path('factories/UserFactory.php');
13161384
13171385<a name="method-lang-path"></a>
 1386  
13181387#### `lang_path()` {.collection-method}
13191388
13201389The `lang_path` 函数返回 `lang` 目录的完整路径。你可以使用 `lang_path` 函数来生成自定义语言目录下指定文件的完整路径:
13211390
13221391   $path = lang_path();
1323 
 1392   
13241393   $path = lang_path('en/messages.php');
13251394
13261395> **注意**
13271396> 默认情况下,Laravel 框架不包含 `lang` 目录。如果你想自定义 Laravel 的语言文件,可以通过 Artisan 命令 `lang:publish` 来发布它们。
13281397
13291398<a name="method-mix"></a>
 1399  
13301400#### `mix()` {.collection-method}
13311401
13321402`mix` 函数返回 [编译前端资源(Mix)的路径](/docs/laravel/10.x/mix),便于加载 css,js 等静态文件:
 
13341404   $path = mix('css/app.css');
13351405
13361406<a name="method-public-path"></a>
 1407  
13371408#### `public_path()` {.collection-method}
13381409
13391410`public_path` 函数返回 `public` 目录的完整路径。你可以使用 `public_path` 函数来生成`public` 目录下指定文件的完整路径:
13401411
13411412   $path = public_path();
1342 
 1413   
13431414   $path = public_path('css/app.css');
13441415
13451416<a name="method-resource-path"></a>
 1417  
13461418#### `resource_path()` {.collection-method}
13471419
13481420`resource_path` 函数返回 `resource` 目录的完整路径。你可以使用 `resource_path` 函数来生成位于资源路径中指定文件的完整路径:
13491421
13501422   $path = resource_path();
1351 
 1423   
13521424   $path = resource_path('sass/app.scss');
13531425
13541426
13551427
13561428<a name="method-storage-path"></a>
 1429  
13571430#### `storage_path()`
13581431
1359 `storage_path` 函数返回 `storage` 目录的完整路径。 你也可以用 `storage_path` 函数来生成位于资源路径中的特定文件路径
 1432`storage_path` 函数返回 `storage` 目录的完整路径。 你也可以用 `storage_path` 函数来生成位于资源路径中的特定文件路径
13601433
13611434   $path = storage_path();
1362 
 1435   
13631436   $path = storage_path('app/file.txt');
13641437
13651438<a name="strings"></a>
 1439  
13661440## 字符串
13671441
13681442<a name="method-__"></a>
 1443  
13691444#### `__()`
13701445
13711446`__`函数可使用 [本地化文件](/docs/laravel/10.x/localization) 来翻译指定的字符串或特定的 key
13721447
13731448   echo __('Welcome to our application');
1374 
 1449   
13751450   echo __('messages.welcome');
13761451
1377 如果给定翻译的字符串或者 key 不存在, 则 `__` 会返回你指定的值。所以上述例子中, 如果给定翻译的字符串或者 key 不存在,则 `__` 函数会返回 `messages.welcome`。
 1452如果给定翻译的字符串或者 key 不存在, 则 `__` 会返回你指定的值。所以上述例子中, 如果给定翻译的字符串或者 key 不存在,则 `__` 函数会返回 `messages.welcome`。
13781453
13791454<a name="method-class-basename"></a>
 1455  
13801456#### `class_basename()`
13811457
13821458`class_basename` 函数返回不带命名空间的特定类的类名:
13831459
13841460   $class = class_basename('Foo\Bar\Baz');
1385 
 1461   
13861462   // Baz
13871463
13881464<a name="method-e"></a>
 1465  
13891466#### `e()`
13901467
1391 `e` 函数运行 PHP 的 `htmlspecialchars` 函数,且 `double_encode` 默认设定为 `true`:
 1468`e` 函数运行 PHP 的 `htmlspecialchars` 函数,且 `double_encode` 默认设定为 `true`:
13921469
13931470   echo e('<html>foo</html>');
1394 
 1471   
13951472   // &lt;html&gt;foo&lt;/html&gt;
13961473
13971474<a name="method-preg-replace-array"></a>
 1475  
13981476#### `preg_replace_array()` {.collection-method}
13991477
1400 `preg_replace_array` 函数按数组顺序替换字符串中符合给定模式的字符:
 1478`preg_replace_array` 函数按数组顺序替换字符串中符合给定模式的字符:
14011479
14021480   $string = 'The event will take place between :start and :end';
1403 
 1481   
14041482   $replaced = preg_replace_array('/:[a-z_]+/', ['8:30', '9:00'], $string);
1405 
 1483   
14061484   // The event will take place between 8:30 and 9:00
14071485
14081486<a name="method-str-after"></a>
 1487  
14091488#### `Str::after()`
14101489
1411 `Str::after` 方法返回字符串中指定值之后的所有内容。如果字符串中不存在这个值,它将返回整个字符串:
1412 
1413    use Illuminate\Support\Str;
1414 
 1490`Str::after` 方法返回字符串中指定值之后的所有内容。如果字符串中不存在这个值,它将返回整个字符串:
 1491
 1492   use Illuminate\Support\Str;
 1493   
14151494   $slice = Str::after('This is my name', 'This is');
1416 
 1495   
14171496   // ' my name'
14181497
14191498
14201499
14211500<a name="method-str-after-last"></a>
 1501  
14221502#### `Str::afterLast()`
14231503
1424 `Str::afterLast` 方法返回字符串中指定值最后一次出现后的所有内容。如果字符串中不存在这个值,它将返回整个字符串:
1425 
1426    use Illuminate\Support\Str;
1427 
 1504`Str::afterLast` 方法返回字符串中指定值最后一次出现后的所有内容。如果字符串中不存在这个值,它将返回整个字符串:
 1505
 1506   use Illuminate\Support\Str;
 1507   
14281508   $slice = Str::afterLast('App\Http\Controllers\Controller', '\\');
1429 
 1509   
14301510   // 'Controller'
14311511
14321512<a name="method-str-ascii"></a>
 1513  
14331514#### `Str::ascii()`
14341515
1435 `Str::ascii` 方法尝试将字符串转换为 ASCII 值:
1436 
1437    use Illuminate\Support\Str;
1438 
 1516`Str::ascii` 方法尝试将字符串转换为 ASCII 值:
 1517
 1518   use Illuminate\Support\Str;
 1519   
14391520   $slice = Str::ascii('û');
1440 
 1521   
14411522   // 'u'
14421523
14431524<a name="method-str-before"></a>
 1525  
14441526#### `Str::before()`
14451527
1446 `Str::before` 方法返回字符串中指定值之前的所有内容:
1447 
1448    use Illuminate\Support\Str;
1449 
 1528`Str::before` 方法返回字符串中指定值之前的所有内容:
 1529
 1530   use Illuminate\Support\Str;
 1531   
14501532   $slice = Str::before('This is my name', 'my name');
1451 
 1533   
14521534   // 'This is '
14531535
14541536<a name="method-str-before-last"></a>
 1537  
14551538#### `Str::beforeLast()`
14561539
1457 `Str::beforeLast` 方法返回字符串中指定值最后一次出现前的所有内容:
1458 
1459    use Illuminate\Support\Str;
1460 
 1540`Str::beforeLast` 方法返回字符串中指定值最后一次出现前的所有内容:
 1541
 1542   use Illuminate\Support\Str;
 1543   
14611544   $slice = Str::beforeLast('This is my name', 'is');
1462 
 1545   
14631546   // 'This '
14641547
14651548<a name="method-str-between"></a>
 1549  
14661550#### `Str::between()`
14671551
1468 `Str::between` 方法返回字符串在指定两个值之间的内容:
1469 
1470    use Illuminate\Support\Str;
1471 
 1552`Str::between` 方法返回字符串在指定两个值之间的内容:
 1553
 1554   use Illuminate\Support\Str;
 1555   
14721556   $slice = Str::between('This is my name', 'This', 'name');
1473 
 1557   
14741558   // ' is my '
14751559
14761560<a name="method-str-between-first"></a>
 1561  
14771562#### `Str::betweenFirst()`
14781563
14791564`Str::betweenFirst` 方法返回字符串在指定两个值之间的最小可能的部分:
14801565
14811566   use Illuminate\Support\Str;
1482 
 1567   
14831568   $slice = Str::betweenFirst('[a] bc [d]', '[', ']');
1484 
 1569   
14851570   // 'a'
14861571
14871572<a name="method-camel-case"></a>
 1573  
14881574#### `Str::camel()`
14891575
1490 `Str::camel` 方法将指定字符串转换为 `驼峰式` 表示方法:
1491 
1492    use Illuminate\Support\Str;
1493 
 1576`Str::camel` 方法将指定字符串转换为 `驼峰式` 表示方法:
 1577
 1578   use Illuminate\Support\Str;
 1579   
14941580   $converted = Str::camel('foo_bar');
1495 
 1581   
14961582   // fooBar
14971583
14981584
14991585
15001586<a name="method-str-contains"></a>
 1587  
15011588#### `Str::contains()`
15021589
1503 `Str::contains` 方法判断指定字符串中是否包含另一指定字符串(区分大小写):
1504 
1505    use Illuminate\Support\Str;
1506 
 1590`Str::contains` 方法判断指定字符串中是否包含另一指定字符串(区分大小写):
 1591
 1592   use Illuminate\Support\Str;
 1593   
15071594   $contains = Str::contains('This is my name', 'my');
1508 
 1595   
15091596   // true
15101597
15111598你也可以传递一个数组来判断指定字符串是否包含数组中的任一值:
15121599
15131600   use Illuminate\Support\Str;
1514 
 1601   
15151602   $contains = Str::contains('This is my name', ['my', 'foo']);
1516 
 1603   
15171604   // true
15181605
15191606<a name="method-str-contains-all"></a>
 1607  
15201608#### `Str::containsAll()`
15211609
1522 `Str::containsAll` 方法用于判断指定字符串是否包含指定数组中的所有值:
1523 
1524    use Illuminate\Support\Str;
1525 
 1610`Str::containsAll` 方法用于判断指定字符串是否包含指定数组中的所有值:
 1611
 1612   use Illuminate\Support\Str;
 1613   
15261614   $containsAll = Str::containsAll('This is my name', ['my', 'name']);
1527 
 1615   
15281616   // true
15291617
15301618<a name="method-ends-with"></a>
 1619  
15311620#### `Str::endsWith()`
15321621
1533 `Str::endsWith` 方法用于判断指定字符串是否以另一指定字符串结尾:
1534 
1535    use Illuminate\Support\Str;
1536 
 1622`Str::endsWith` 方法用于判断指定字符串是否以另一指定字符串结尾:
 1623
 1624   use Illuminate\Support\Str;
 1625   
15371626   $result = Str::endsWith('This is my name', 'name');
1538 
 1627   
15391628   // true
15401629
15411630你也可以传一个数组来判断指定字符串是否以指定数组中的任一值结尾:
15421631
15431632   use Illuminate\Support\Str;
1544 
 1633   
15451634   $result = Str::endsWith('This is my name', ['name', 'foo']);
1546 
 1635   
15471636   // true
1548 
 1637   
15491638   $result = Str::endsWith('This is my name', ['this', 'foo']);
1550 
 1639   
15511640   // false
15521641
15531642<a name="method-excerpt"></a>
 1643  
15541644#### `Str::excerpt()`
15551645
1556 `Str::excerpt` 方法提取字符串中给定短语匹配到的第一个片段:
1557 
1558    use Illuminate\Support\Str;
1559 
 1646`Str::excerpt` 方法提取字符串中给定短语匹配到的第一个片段:
 1647
 1648   use Illuminate\Support\Str;
 1649   
15601650   $excerpt = Str::excerpt('This is my name', 'my', [
15611651       'radius' => 3
15621652   ]);
1563 
 1653   
15641654   // '...is my na...'
15651655
1566 `radius` 选项默认为 `100`,允许你定义应出现在截断字符串前后的字符数。
 1656`radius` 选项默认为 `100`,允许你定义应出现在截断字符串前后的字符数。
15671657
15681658
15691659
15701660此外,你可以使用`omission`选项来定义将附加到截断字符串的字符串:
15711661
15721662   use Illuminate\Support\Str;
1573 
 1663   
15741664   $excerpt = Str::excerpt('This is my name', 'name', [
15751665       'radius' => 3,
15761666       'omission' => '(...) '
15771667   ]);
1578 
 1668   
15791669   // '(...) my name'
15801670
15811671<a name="method-str-finish"></a>
 1672  
15821673#### `Str::finish()` {.collection-method}
15831674
15841675`Str::finish`方法将指定的字符串修改为以指定的值结尾的形式:
15851676
15861677   use Illuminate\Support\Str;
1587 
 1678   
15881679   $adjusted = Str::finish('this/string', '/');
1589 
 1680   
15901681   // this/string/
1591 
 1682   
15921683   $adjusted = Str::finish('this/string/', '/');
1593 
 1684   
15941685   // this/string/
15951686
15961687<a name="method-str-headline"></a>
 1688  
15971689#### `Str::headline()` {.collection-method}
15981690
15991691`Str::headline`方法会将由大小写、连字符或下划线分隔的字符串转换为空格分隔的字符串,同时保证每个单词的首字母大写:
16001692
16011693   use Illuminate\Support\Str;
1602 
 1694   
16031695   $headline = Str::headline('steve_jobs');
1604 
 1696   
16051697   // Steve Jobs
1606 
 1698   
16071699   $headline = Str::headline('邮件通知发送');
1608 
 1700   
16091701   // 邮件通知发送
16101702
16111703<a name="method-str-inline-markdown"></a>
 1704  
16121705#### `Str::inlineMarkdown()` {.collection-method}
16131706
16141707`Str::inlineMarkdown`方法使用[通用标记](https://commonmark.thephpleague.com/)将 GitHub 风味 Markdown 转换为内联 HTML。然而,与`markdown`方法不同的是,它不会将所有生成的 HTML 都包装在块级元素中:
16151708
16161709   use Illuminate\Support\Str;
1617 
 1710   
16181711   $html = Str::inlineMarkdown('**Laravel**');
1619 
 1712   
16201713   // <strong>Laravel</strong>
16211714
16221715<a name="method-str-is"></a>
 1716  
16231717#### `Str::is()` {.collection-method}
16241718
16251719`Str::is`方法用来判断字符串是否与指定模式匹配。星号`*`可用于表示通配符:
16261720
16271721   use Illuminate\Support\Str;
1628 
 1722   
16291723   $matches = Str::is('foo*', 'foobar');
1630 
 1724   
16311725   // true
1632 
 1726   
16331727   $matches = Str::is('baz*', 'foobar');
1634 
 1728   
16351729   // false
16361730
16371731<a name="method-str-is-ascii"></a>
 1732  
16381733#### `Str::isAscii()` {.collection-method}
16391734
16401735`Str::isAscii`方法用于判断字符串是否是 7 位 ASCII:
16411736
16421737   use Illuminate\Support\Str;
1643 
 1738   
16441739   $isAscii = Str::isAscii('Taylor');
1645 
 1740   
16461741   // true
1647 
 1742   
16481743   $isAscii = Str::isAscii('ü');
1649 
 1744   
16501745   // false
16511746
16521747
16531748
16541749<a name="method-str-is-json"></a>
 1750  
16551751#### `Str::isJson()` {.collection-method}
16561752
16571753`Str::isJson`方法确定给定的字符串是否是有效的 JSON:
16581754
16591755   use Illuminate\Support\Str;
1660 
 1756   
16611757   $result = Str::isJson('[1,2,3]');
1662 
 1758   
16631759   // true
1664 
 1760   
16651761   $result = Str::isJson('{"first": "John", "last": "Doe"}');
1666 
 1762   
16671763   // true
1668 
 1764   
16691765   $result = Str::isJson('{first: "John", last: "Doe"}');
1670 
 1766   
16711767   // false
16721768
16731769<a name="method-str-is-ulid"></a>
 1770  
16741771#### `Str::isUlid()` {.collection-method}
16751772
16761773`Str::isUlid`方法用于判断指定字符串是否是有效的 ULID:
16771774
16781775   use Illuminate\Support\Str;
1679 
 1776   
16801777   $isUlid = Str::isUlid('01gd6r360bp37zj17nxb55yv40');
1681 
 1778   
16821779   // true
1683 
 1780   
16841781   $isUlid = Str::isUlid('laravel');
1685 
 1782   
16861783   // false
16871784
16881785<a name="method-str-is-uuid"></a>
 1786  
16891787#### `Str::isUuid()` {.collection-method}
16901788
16911789`Str::isUuid`方法用于判断指定字符串是否是有效的 UUID:
16921790
16931791   use Illuminate\Support\Str;
1694 
 1792   
16951793   $isUuid = Str::isUuid('a0a2a2d2-0b87-4a18-83f2-2529882be2de');
1696 
 1794   
16971795   // true
1698 
 1796   
16991797   $isUuid = Str::isUuid('laravel');
1700 
 1798   
17011799   // false
17021800
17031801<a name="method-kebab-case"></a>
 1802  
17041803#### `Str::kebab()` {.collection-method}
17051804
17061805`Str::kebab`方法将字符串转换为`烤串式( kebab-case )`表示方法:
17071806
17081807   use Illuminate\Support\Str;
1709 
 1808   
17101809   $converted = Str::kebab('fooBar');
1711 
 1810   
17121811   // foo-bar
17131812
17141813<a name="method-str-lcfirst"></a>
 1814  
17151815#### `Str::lcfirst()` {.collection-method}
17161816
17171817`Str::lcfirst`方法返回第一个小写字符的给定字符串:
17181818
17191819   use Illuminate\Support\Str;
1720 
 1820   
17211821   $string = Str::lcfirst('Foo Bar');
1722 
 1822   
17231823   // foo Bar
17241824
17251825<a name="method-str-length"></a>
 1826  
17261827#### `Str::length()` {.collection-method}
17271828
17281829`Str::length`方法返回指定字符串的长度:
17291830
17301831   use Illuminate\Support\Str;
1731 
 1832   
17321833   $length = Str::length('Laravel');
1733 
 1834   
17341835   // 7
17351836
17361837<a name="method-str-limit"></a>
 1838  
17371839#### `Str::limit()` {.collection-method}
17381840
17391841`Str::limit`方法将字符串以指定长度进行截断:
17401842
17411843   use Illuminate\Support\Str;
1742 
 1844   
17431845   $truncated = Str::limit('敏捷的棕色狐狸跳过懒惰的狗', 20);
1744 
 1846   
17451847   // 敏捷的棕色狐狸...
17461848
17471849
 
17491851你也可通过第三个参数来改变追加到末尾的字符串:
17501852
17511853   use Illuminate\Support\Str;
1752 
 1854   
17531855   $truncated = Str::limit('敏捷的棕色狐狸跳过懒惰的狗', 20, ' (...)');
1754 
 1856   
17551857   // 敏捷的棕色狐狸 (...)
17561858
17571859<a name="method-str-lower"></a>
 1860  
17581861#### `Str::lower()` {.collection-method}
17591862
17601863`Str::lower`方法用于将字符串转换为小写:
17611864
17621865   use Illuminate\Support\Str;
1763 
 1866   
17641867   $converted = Str::lower('LARAVEL');
1765 
 1868   
17661869   // laravel
17671870
17681871<a name="method-str-markdown"></a>
 1872  
17691873#### `Str::markdown()` {.collection-method}
17701874
17711875`Str::markdown`方法将 GitHub 风格的 Markdown 转换为 HTML 使用[通用标记](https://commonmark.thephpleague.com/):
17721876
17731877   use Illuminate\Support\Str;
1774 
 1878   
17751879   $html = Str::markdown('# Laravel');
1776 
 1880   
17771881   // <h1>Laravel</h1>
1778 
 1882   
17791883   $html = Str::markdown('# Taylor <b>Otwell</b>', [
17801884       'html_input' => 'strip',
17811885   ]);
1782 
 1886   
17831887   // <h1>Taylor Otwell</h1>
17841888
17851889<a name="method-str-mask"></a>
 1890  
17861891#### `Str::mask()` {.collection-method}
17871892
17881893`Str::mask`方法会使用重复的字符掩盖字符串的一部分,并可用于混淆字符串段,例如电子邮件地址和电话号码:
17891894
17901895   use Illuminate\Support\Str;
1791 
 1896   
17921897   $string = Str::mask('taylor@example.com', '*', 3);
1793 
 1898   
17941899   // tay***************
17951900
17961901你可以提供一个负数作为`mask`方法的第三个参数,这将指示该方法在距字符串末尾的给定距离处开始屏蔽:
17971902
17981903   $string = Str::mask('taylor@example.com', '*', -15, 3);
1799 
 1904   
18001905   // tay***@example.com
18011906
18021907<a name="method-str-ordered-uuid"></a>
 1908  
18031909#### `Str::orderedUuid()` {.collection-method}
18041910
18051911`Str::orderedUuid`方法用于生成一个「时间戳优先」的 UUID ,它可作为数据库索引列的有效值。使用此方法生成的每个 UUID 将排在之前使用该方法生成的 UUID 后面:
18061912
18071913   use Illuminate\Support\Str;
1808 
 1914   
18091915   return (string) Str::orderedUuid();
18101916
18111917
18121918
18131919<a name="method-str-padboth"></a>
 1920  
18141921#### `Str::padBoth()` {.collection-method}
18151922
1816 `Str::padBoth`方法包装了 PHP 的`str_pad 函数`,在指定字符串的两侧填充上另一字符串:
1817 
1818    use Illuminate\Support\Str;
1819 
 1923`Str::padBoth`方法包装了 PHP 的`str_pad 方法`,在指定字符串的两侧填充上另一字符串:
 1924
 1925   use Illuminate\Support\Str;
 1926   
18201927   $padded = Str::padBoth('James', 10, '_');
1821 
 1928   
18221929   // '__James___'
1823 
 1930   
18241931   $padded = Str::padBoth('James', 10);
1825 
 1932   
18261933   // ' James  '
18271934
18281935<a name="method-str-padleft"></a>
 1936  
18291937#### `Str::padLeft()` {.collection-method}
18301938
1831 `Str::padLeft`方法包装了 PHP 的`str_pad`函数,在指定字符串的左侧填充上另一字符串:
1832 
1833    use Illuminate\Support\Str;
1834 
 1939`Str::padLeft`方法包装了 PHP 的`str_pad`方法,在指定字符串的左侧填充上另一字符串:
 1940
 1941   use Illuminate\Support\Str;
 1942   
18351943   $padded = Str::padLeft('James', 10, '-=');
1836 
 1944   
18371945   // '-=-=-James'
1838 
 1946   
18391947   $padded = Str::padLeft('James', 10);
1840 
 1948   
18411949   // '    James'
18421950
18431951<a name="method-str-padright"></a>
 1952  
18441953#### `Str::padRight()` {.collection-method}
18451954
1846 `Str::padRight`方法包装了 PHP 的`str_pad`函数,在指定字符串的右侧填充上另一字符串:
1847 
1848    use Illuminate\Support\Str;
1849 
 1955`Str::padRight`方法包装了 PHP 的`str_pad`方法,在指定字符串的右侧填充上另一字符串:
 1956
 1957   use Illuminate\Support\Str;
 1958   
18501959   $padded = Str::padRight('James', 10, '-');
1851 
 1960   
18521961   // 'James-----'
1853 
 1962   
18541963   $padded = Str::padRight('James', 10);
1855 
 1964   
18561965   // 'James    '
18571966
18581967<a name="method-str-password"></a>
 1968  
18591969#### `Str::password()` {.collection-method}
18601970
18611971`Str::password`方法可用于生成给定长度的安全随机密码。密码由字母、数字、符号和空格组成。默认情况下,密码长度为32位:
18621972
18631973   use Illuminate\Support\Str;
1864 
 1974   
18651975   $password = Str::password();
1866 
 1976   
18671977   // 'EbJo2vE-AS:U,$%_gkrV4n,q~1xy/-_4'
1868 
 1978   
18691979   $password = Str::password(12);
1870 
 1980   
18711981   // 'qwuar>#V|i]N'
18721982
18731983<a name="method-str-plural"></a>
 1984  
18741985#### `Str::plural()` {.collection-method}
18751986
1876 `Str::plural`方法将单数形式的字符串转换为复数形式。此函数支持 [Laravel 复数形式所支持的任何语言](/docs/laravel/10.x/localization#pluralization-language):
1877 
1878    use Illuminate\Support\Str;
1879 
 1987`Str::plural`方法将单数形式的字符串转换为复数形式。此方法支持 [Laravel 复数形式所支持的任何语言](/docs/laravel/10.x/localization#pluralization-language):
 1988
 1989   use Illuminate\Support\Str;
 1990   
18801991   $plural = Str::plural('car');
1881 
 1992   
18821993   // cars
1883 
 1994   
18841995   $plural = Str::plural('child');
1885 
 1996   
18861997   // children
18871998
18881999
18892000
1890 你可以提供一个整数作为函数的第二个参数来检索字符串的单数或复数形式:
1891 
1892    use Illuminate\Support\Str;
1893 
 2001你可以提供一个整数作为方法的第二个参数来检索字符串的单数或复数形式:
 2002
 2003   use Illuminate\Support\Str;
 2004   
18942005   $plural = Str::plural('child', 2);
1895 
 2006   
18962007   // children
1897 
 2008   
18982009   $singular = Str::plural('child', 1);
1899 
 2010   
19002011   // child
19012012
19022013<a name="method-str-plural-studly"></a>
 2014  
19032015#### `Str::pluralStudly()` {.collection-method}
19042016
1905 `Str::pluralStudly`方法将以驼峰格式的单数字符串转化为其复数形式。此函数支持 [Laravel 复数形式所支持的任何语言](/docs/laravel/10.x/localization#pluralization-language):
1906 
1907    use Illuminate\Support\Str;
1908 
 2017`Str::pluralStudly`方法将以驼峰格式的单数字符串转化为其复数形式。此方法支持 [Laravel 复数形式所支持的任何语言](/docs/laravel/10.x/localization#pluralization-language):
 2018
 2019   use Illuminate\Support\Str;
 2020   
19092021   $plural = Str::pluralStudly('VerifiedHuman');
1910 
 2022   
19112023   // VerifiedHumans
1912 
 2024   
19132025   $plural = Str::pluralStudly('UserFeedback');
1914 
 2026   
19152027   // UserFeedback
19162028
1917 你可以提供一个整数作为函数的第二个参数来检索字符串的单数或复数形式:
1918 
1919    use Illuminate\Support\Str;
1920 
 2029你可以提供一个整数作为方法的第二个参数来检索字符串的单数或复数形式:
 2030
 2031   use Illuminate\Support\Str;
 2032   
19212033   $plural = Str::pluralStudly('VerifiedHuman', 2);
1922 
 2034   
19232035   // VerifiedHumans
1924 
 2036   
19252037   $singular = Str::pluralStudly('VerifiedHuman', 1);
1926 
 2038   
19272039   // VerifiedHuman
19282040
19292041<a name="method-str-random"></a>
 2042  
19302043#### `Str::random()` {.collection-method}
19312044
1932 `Str::random` 方法用于生成指定长度的随机字符串。这个函数使用了PHP的 `random_bytes` 函数:
1933 
1934    use Illuminate\Support\Str;
1935 
 2045`Str::random` 方法用于生成指定长度的随机字符串。这个方法使用了PHP的 `random_bytes` 函数:
 2046
 2047   use Illuminate\Support\Str;
 2048   
19362049   $random = Str::random(40);
19372050
19382051<a name="method-str-remove"></a>
 2052  
19392053#### `Str::remove()` {.collection-method}
19402054
19412055`Str::remove` 方法从字符串中删除给定值或给定数组内的所有值:
19422056
19432057   use Illuminate\Support\Str;
1944 
 2058   
19452059   $string = 'Peter Piper picked a peck of pickled peppers.';
1946 
 2060   
19472061   $removed = Str::remove('e', $string);
1948 
 2062   
19492063   // Ptr Pipr pickd a pck of pickld ppprs.
19502064
19512065你还可以将`false`作为第三个参数传递给`remove`方法以在删除字符串时忽略大小写。
19522066
19532067<a name="method-str-replace"></a>
 2068  
19542069#### `Str::replace()` {.collection-method}
19552070
19562071`Str::replace` 方法用于替换字符串中的给定字符串:
19572072
19582073   use Illuminate\Support\Str;
1959 
 2074   
19602075   $string = 'Laravel 8.x';
1961 
 2076   
19622077   $replaced = Str::replace('8.x', '9.x', $string);
1963 
 2078   
19642079   // Laravel 9.x
19652080
19662081
19672082
19682083<a name="method-str-replace-array"></a>
 2084  
19692085#### `Str::replaceArray()` {.collection-method}
19702086
1971 `Str::replaceArray`方法使用数组有序的替换字符串中的特定字符:
1972 
1973    use Illuminate\Support\Str;
1974 
 2087`Str::replaceArray` 方法使用数组有序的替换字符串中的特定字符:
 2088
 2089   use Illuminate\Support\Str;
 2090   
19752091   $string = '该活动将在 ? 至 ? 举行';
1976 
 2092   
19772093   $replaced = Str::replaceArray('?', ['8:30', '9:00'], $string);
1978 
 2094   
19792095   // 该活动将在 8:30 至 9:00 举行
19802096
19812097<a name="method-str-replace-first"></a>
 2098  
19822099#### `Str::replaceFirst()` {.collection-method}
19832100
1984 `Str::replaceFirst`函数替换字符串中给定值的第一个匹配项:
1985 
1986    use Illuminate\Support\Str;
1987 
 2101`Str::replaceFirst` 方法替换字符串中给定值的第一个匹配项:
 2102
 2103   use Illuminate\Support\Str;
 2104   
19882105   $replaced = Str::replaceFirst('the', 'a', 'the quick brown fox jumps over the lazy dog');
1989 
 2106   
19902107   // a quick brown fox jumps over the lazy dog
19912108
19922109<a name="method-str-replace-last"></a>
 2110  
19932111#### `Str::replaceLast()` {.collection-method}
19942112
1995 `Str::replaceLast`函数替换字符串中最后一次出现的给定值:
1996 
1997    use Illuminate\Support\Str;
1998 
 2113`Str::replaceLast` 方法替换字符串中最后一次出现的给定值:
 2114
 2115   use Illuminate\Support\Str;
 2116   
19992117   $replaced = Str::replaceLast('the', 'a', 'the quick brown fox jumps over the lazy dog');
2000 
 2118   
20012119   // the quick brown fox jumps over a lazy dog
20022120
20032121
20042122<a name="method-str-reverse"></a>
 2123  
20052124#### `Str::reverse()` {.collection-method}
20062125
2007 `Str::reverse`方法用于反转给定的字符串:
2008 
2009    use Illuminate\Support\Str;
2010 
 2126`Str::reverse` 方法用于反转给定的字符串:
 2127
 2128   use Illuminate\Support\Str;
 2129   
20112130   $reversed = Str::reverse('Hello World');
2012 
 2131   
20132132   // dlroW olleH
20142133
20152134<a name="method-str-singular"></a>
 2135  
20162136#### `Str::singular()` {.collection-method}
20172137
2018 `Str::singular`方法将字符串转换为单数形式。此函数支持 [Laravel 复数形式所支持的任何语言](/docs/laravel/10.x/localization#pluralization-language):
2019 
2020    use Illuminate\Support\Str;
2021 
 2138`Str::singular` 方法将字符串转换为单数形式。此方法支持 [Laravel 复数形式所支持的任何语言](/docs/laravel/10.x/localization#pluralization-language):
 2139
 2140   use Illuminate\Support\Str;
 2141   
20222142   $singular = Str::singular('cars');
2023 
 2143   
20242144   // car
2025 
 2145   
20262146   $singular = Str::singular('children');
2027 
 2147   
20282148   // child
20292149
20302150<a name="method-str-slug"></a>
 2151  
20312152#### `Str::slug()` {.collection-method}
20322153
2033 `Str::slug`方法将给定的字符串生成一个 URL 友好的「slug」:
2034 
2035    use Illuminate\Support\Str;
2036 
 2154`Str::slug` 方法将给定的字符串生成一个 URL 友好的「slug」:
 2155
 2156   use Illuminate\Support\Str;
 2157   
20372158   $slug = Str::slug('Laravel 5 Framework', '-');
2038 
 2159   
20392160   // laravel-5-framework
20402161
20412162
20422163
20432164<a name="method-snake-case"></a>
 2165  
20442166#### `Str::snake()` {.collection-method}
20452167
2046 `Str::snake`方法是将驼峰的函数名或者字符串转换成`_`命名的函数或者字符串,例如`snakeCase` 转换成 `snake_case`
2047 
2048    use Illuminate\Support\Str;
2049 
 2168`Str::snake` 方法是将驼峰的函数名或者字符串转换成 `_` 命名的函数或者字符串,例如 `snakeCase` 转换成 `snake_case`:
 2169
 2170   use Illuminate\Support\Str;
 2171   
20502172   $converted = Str::snake('fooBar');
2051 
 2173   
20522174   // foo_bar
2053 
 2175   
20542176   $converted = Str::snake('fooBar', '-');
2055 
 2177   
20562178   // foo-bar
20572179
20582180<a name="method-str-squish"></a>
 2181  
20592182#### `Str::squish()` {.collection-method}
20602183
20612184`Str::squish`方法删除字符串中所有多余的空白,包括单词之间多余的空白:
20622185
20632186   use Illuminate\Support\Str;
2064 
 2187   
20652188   $string = Str::squish('   laravel   framework   ');
2066 
 2189   
20672190   // laravel framework
20682191
20692192<a name="method-str-start"></a>
 2193  
20702194#### `Str::start()` {.collection-method}
20712195
20722196`Str::start`方法是将给定的值添加到字符串的开始位置,例如:
20732197
20742198   use Illuminate\Support\Str;
2075 
 2199   
20762200   $adjusted = Str::start('this/string', '/');
2077 
 2201   
20782202   // /this/string
2079 
 2203   
20802204   $adjusted = Str::start('/this/string', '/');
2081 
 2205   
20822206   // /this/string
20832207
20842208<a name="method-starts-with"></a>
 2209  
20852210#### `Str::startsWith()` {.collection-method}
20862211
20872212`Str::startsWith`方法用来判断给定的字符串是否为给定值的开头:
20882213
20892214   use Illuminate\Support\Str;
2090 
 2215   
20912216   $result = Str::startsWith('This is my name', 'This');
2092 
 2217   
20932218   // true
20942219
20952220如果传递了一个可能值的数组且字符串以任何给定值开头,则`startsWith`方法将返回`true`:
20962221
20972222   $result = Str::startsWith('This is my name', ['This', 'That', 'There']);
2098 
 2223   
20992224   // true
21002225
21012226<a name="method-studly-case"></a>
 2227  
21022228#### `Str::studly()` {.collection-method}
21032229
21042230`Str::studly`方法将给定的字符串转换为`驼峰命名`的字符串:
21052231
21062232   use Illuminate\Support\Str;
2107 
 2233   
21082234   $converted = Str::studly('foo_bar');
2109 
 2235   
21102236   // FooBar
21112237
21122238<a name="method-str-substr"></a>
 2239  
21132240#### `Str::substr()` {.collection-method}
21142241
21152242`Str::substr`方法返回由 start 和 length 参数指定的字符串部分:
21162243
21172244   use Illuminate\Support\Str;
2118 
 2245   
21192246   $converted = Str::substr('The Laravel Framework', 4, 7);
2120 
 2247   
21212248   // Laravel
21222249
21232250
21242251
21252252<a name="method-str-substrcount"></a>
 2253  
21262254#### `Str::substrCount()` {.collection-method}
21272255
21282256`Str::substrCount` 方法返回给定字符串中给定值的出现次数:
21292257
21302258   use Illuminate\Support\Str;
2131 
 2259   
21322260   $count = Str::substrCount('If you like ice cream, you will like snow cones.', 'like');
2133 
 2261   
21342262   // 2
21352263
21362264<a name="method-str-substrreplace"></a>
 2265  
21372266#### `Str::substrReplace()` {.collection-method}
21382267
21392268`Str::substrReplace` 方法替换字符串一部分中的文本,从第三个参数指定的位置开始,替换第四个参数指定的字符数。 当「0」传递给方法的第四个参数将在指定位置插入字符串,而不是替换字符串中的任何现有字符:
21402269
21412270   use Illuminate\Support\Str;
2142 
 2271   
21432272   $result = Str::substrReplace('1300', ':', 2);
21442273   // 13:
2145 
 2274   
21462275   $result = Str::substrReplace('1300', ':', 2, 0);
21472276   // 13:00
21482277
21492278<a name="method-str-swap"></a>
 2279  
21502280#### `Str::swap()` {.collection-method}
21512281
21522282`Str::swap` 方法使用 PHP 的 `strtr` 函数替换给定字符串中的多个值:
21532283
21542284   use Illuminate\Support\Str;
2155 
 2285   
21562286   $string = Str::swap([
21572287       'Tacos' => 'Burritos',
21582288       'great' => 'fantastic',
21592289   ], 'Tacos are great!');
2160 
 2290   
21612291   // Burritos are fantastic!
21622292
21632293<a name="method-title-case"></a>
 2294  
21642295#### `Str::title()` {.collection-method}
21652296
21662297`Str::title` 方法将给定的字符串转换为 `Title Case`:
21672298
21682299   use Illuminate\Support\Str;
2169 
 2300   
21702301   $converted = Str::title('a nice title uses the correct case');
2171 
 2302   
21722303   // A Nice Title Uses The Correct Case
21732304
21742305<a name="method-str-to-html-string"></a>
 2306  
21752307#### `Str::toHtmlString()` {.collection-method}
21762308
21772309`Str::toHtmlString` 方法将字符串实例转换为 `Illuminate\Support\HtmlString` 的实例,它可以显示在 Blade 模板中:
21782310
21792311   use Illuminate\Support\Str;
2180 
 2312   
21812313   $htmlString = Str::of('Nuno Maduro')->toHtmlString();
21822314
21832315<a name="method-str-ucfirst"></a>
 2316  
21842317#### `Str::ucfirst()` {.collection-method}
21852318
21862319`Str::ucfirst` 方法返回第一个字符大写的给定字符串:
21872320
21882321   use Illuminate\Support\Str;
2189 
 2322   
21902323   $string = Str::ucfirst('foo bar');
2191 
 2324   
21922325   // Foo bar
21932326
21942327
21952328
21962329<a name="method-str-ucsplit"></a>
 2330  
21972331#### `Str::ucsplit()` {.collection-method}
21982332
21992333`Str::ucsplit` 方法将给定的字符串按大写字符拆分为数组:
22002334
22012335   use Illuminate\Support\Str;
2202 
 2336   
22032337   $segments = Str::ucsplit('FooBar');
2204 
 2338   
22052339   // [0 => 'Foo', 1 => 'Bar']
22062340
22072341<a name="method-str-upper"></a>
 2342  
22082343#### `Str::upper()` {.collection-method}
22092344
22102345`Str::upper` 方法将给定的字符串转换为大写:
22112346
22122347   use Illuminate\Support\Str;
2213 
 2348   
22142349   $string = Str::upper('laravel');
2215 
 2350   
22162351   // LARAVEL
22172352
22182353<a name="method-str-ulid"></a>
 2354  
22192355#### `Str::ulid()` {.collection-method}
22202356
22212357`Str::ulid` 方法生成一个 ULID:
22222358
22232359   use Illuminate\Support\Str;
2224 
 2360   
22252361   return (string) Str::ulid();
22262362   
22272363   // 01gd6r360bp37zj17nxb55yv40
22282364
22292365<a name="method-str-uuid"></a>
 2366  
22302367#### `Str::uuid()` {.collection-method}
22312368
22322369`Str::uuid` 方法生成一个 UUID(版本 4):
22332370
22342371   use Illuminate\Support\Str;
2235 
 2372   
22362373   return (string) Str::uuid();
22372374
22382375<a name="method-str-word-count"></a>
 2376  
22392377#### `Str::wordCount()` {.collection-method}
22402378
22412379`Str::wordCount` 方法返回字符串包含的单词数
 
22472385```
22482386
22492387<a name="method-str-words"></a>
 2388  
22502389#### `Str::words()` {.collection-method}
22512390
22522391`Str::words` 方法限制字符串中的单词数。 可以通过其第三个参数将附加字符串传递给此方法,以指定应将这个字符串附加到截断后的字符串末尾:
22532392
22542393   use Illuminate\Support\Str;
2255 
 2394   
22562395   return Str::words('Perfectly balanced, as all things should be.', 3, ' >>>');
2257 
 2396   
22582397   // Perfectly balanced, as >>>
22592398
22602399<a name="method-str"></a>
 2400  
22612401#### `str()` {.collection-method}
22622402
22632403`str` 函数返回给定字符串的新 `Illuminate\Support\Stringable` 实例。 此函数等效于 `Str::of` 方法:
22642404
22652405   $string = str('Taylor')->append(' Otwell');
2266 
 2406   
22672407   // 'Taylor Otwell'
22682408
22692409如果没有为 `str` 函数提供参数,该函数将返回 `Illuminate\Support\Str` 的实例:
22702410
22712411   $snake = str()->snake('FooBar');
2272 
 2412   
22732413   // 'foo_bar'
22742414
22752415
22762416
22772417<a name="method-trans"></a>
 2418  
22782419#### `trans()` {.collection-method}
22792420
22802421`trans` 函数使用你的 [语言文件](/docs/laravel/10.x/localization) 翻译给定的翻译键:
 2422  
22812423```
22822424    echo trans('messages.welcome');
22832425```
 2426  
22842427如果指定的翻译键不存在,`trans` 函数将返回给定的键。 因此,使用上面的示例,如果翻译键不存在,`trans` 函数将返回 `messages.welcome`。
22852428
22862429<a name="method-trans-choice"></a>
 2430  
22872431#### `trans_choice()` {.collection-method}
22882432
22892433`trans_choice` 函数用词形变化翻译给定的翻译键:
 2434  
22902435```
22912436    echo trans_choice('messages.notifications', $unreadCount);
22922437```
 2438  
22932439如果指定的翻译键不存在,`trans_choice` 函数将返回给定的键。 因此,使用上面的示例,如果翻译键不存在,`trans_choice` 函数将返回 `messages.notifications`。
22942440
22952441<a name="fluent-strings"></a>
 2442  
22962443## 字符流处理
22972444
22982445Fluent strings 提供了一个更流畅的、面向对象的接口来处理字符串值,与传统的字符串操作相比,允许你使用更易读的语法将多个字符串操作链接在一起。
22992446
23002447<a name="method-fluent-str-after"></a>
 2448  
23012449#### `after` {.collection-method}
23022450
23032451`after` 方法返回字符串中给定值之后的所有内容。 如果字符串中不存在该值,则将返回整个字符串:
23042452
23052453   use Illuminate\Support\Str;
2306 
 2454   
23072455   $slice = Str::of('This is my name')->after('This is');
2308 
 2456   
23092457   // ' my name'
23102458
23112459<a name="method-fluent-str-after-last"></a>
 2460  
23122461#### `afterLast` {.collection-method}
23132462
23142463`afterLast` 方法返回字符串中最后一次出现给定值之后的所有内容。 如果字符串中不存在该值,则将返回整个字符串
23152464
23162465   use Illuminate\Support\Str;
2317 
 2466   
23182467   $slice = Str::of('App\Http\Controllers\Controller')->afterLast('\\');
2319 
 2468   
23202469   // 'Controller'
23212470
23222471
23232472
23242473<a name="method-fluent-str-append"></a>
 2474  
23252475#### `append` {.collection-method}
23262476
23272477`append` 方法将给定的值附加到字符串:
23282478
23292479   use Illuminate\Support\Str;
2330 
 2480   
23312481   $string = Str::of('Taylor')->append(' Otwell');
2332 
 2482   
23332483   // 'Taylor Otwell'
23342484
23352485<a name="method-fluent-str-ascii"></a>
 2486  
23362487#### `ascii` {.collection-method}
23372488
23382489`ascii` 方法将尝试将字符串音译为 ASCII 值:
23392490
23402491   use Illuminate\Support\Str;
2341 
 2492   
23422493   $string = Str::of('ü')->ascii();
2343 
 2494   
23442495   // 'u'
23452496
23462497<a name="method-fluent-str-basename"></a>
 2498  
23472499#### `basename` {.collection-method}
23482500
23492501`basename` 方法将返回给定字符串的结尾名称部分:
23502502
23512503   use Illuminate\Support\Str;
2352 
 2504   
23532505   $string = Str::of('/foo/bar/baz')->basename();
2354 
 2506   
23552507   // 'baz'
23562508
23572509如果需要,你可以提供将从尾随组件中删除的「扩展名」:
23582510
23592511   use Illuminate\Support\Str;
2360 
 2512   
23612513   $string = Str::of('/foo/bar/baz.jpg')->basename('.jpg');
2362 
 2514   
23632515   // 'baz'
23642516
23652517<a name="method-fluent-str-before"></a>
 2518  
23662519#### `before` {.collection-method}
23672520
23682521`before` 方法返回字符串中给定值之前的所有内容:
23692522
23702523   use Illuminate\Support\Str;
2371 
 2524   
23722525   $slice = Str::of('This is my name')->before('my name');
2373 
 2526   
23742527   // 'This is '
23752528
23762529<a name="method-fluent-str-before-last"></a>
 2530  
23772531#### `beforeLast` {.collection-method}
23782532
23792533`beforeLast` 方法返回字符串中最后一次出现给定值之前的所有内容:
23802534
23812535   use Illuminate\Support\Str;
2382 
 2536   
23832537   $slice = Str::of('This is my name')->beforeLast('is');
2384 
 2538   
23852539   // 'This '
23862540
23872541<a name="method-fluent-str-between"></a>
 2542  
23882543#### `between` {.collection-method}
23892544
23902545`between` 方法返回两个值之间的字符串部分:
23912546
23922547   use Illuminate\Support\Str;
2393 
 2548   
23942549   $converted = Str::of('This is my name')->between('This', 'name');
2395 
 2550   
23962551   // ' is my '
23972552
23982553<a name="method-fluent-str-between-first"></a>
 2554  
23992555#### `betweenFirst` {.collection-method}
24002556
24012557`betweenFirst` 方法返回两个值之间字符串的最小可能部分:
24022558
24032559   use Illuminate\Support\Str;
2404 
 2560   
24052561   $converted = Str::of('[a] bc [d]')->betweenFirst('[', ']');
2406 
 2562   
24072563   // 'a'
24082564
24092565
24102566
24112567<a name="method-fluent-str-camel"></a>
 2568  
24122569#### `camel` {.collection-method}
24132570
24142571`camel` 方法将给定的字符串转换为 `camelCase`:
24152572
24162573   use Illuminate\Support\Str;
2417 
 2574   
24182575   $converted = Str::of('foo_bar')->camel();
2419 
 2576   
24202577   // fooBar
24212578
24222579<a name="method-fluent-str-class-basename"></a>
 2580  
24232581#### `classBasename` {.collection-method}
24242582
24252583`classBasename` 方法返回给定类的类名,删除了类的命名空间:
24262584
24272585   use Illuminate\Support\Str;
2428 
 2586   
24292587   $class = Str::of('Foo\Bar\Baz')->classBasename();
2430 
 2588   
24312589   // Baz
24322590
24332591<a name="method-fluent-str-contains"></a>
 2592  
24342593#### `contains` {.collection-method}
24352594
24362595`contains` 方法确定给定的字符串是否包含给定的值。 此方法区分大小写:
24372596
24382597   use Illuminate\Support\Str;
2439 
 2598   
24402599   $contains = Str::of('This is my name')->contains('my');
2441 
 2600   
24422601   // true
24432602
24442603你还可以传递一个值数组来确定给定字符串是否包含数组中的任意值:
24452604
24462605   use Illuminate\Support\Str;
2447 
 2606   
24482607   $contains = Str::of('This is my name')->contains(['my', 'foo']);
2449 
 2608   
24502609   // true
24512610
24522611<a name="method-fluent-str-contains-all"></a>
 2612  
24532613#### `containsAll` {.collection-method}
24542614
24552615`containsAll` 方法确定给定字符串是否包含给定数组中的所有值:
24562616
24572617   use Illuminate\Support\Str;
2458 
 2618   
24592619   $containsAll = Str::of('This is my name')->containsAll(['my', 'name']);
2460 
 2620   
24612621   // true
24622622
24632623<a name="method-fluent-str-dirname"></a>
 2624  
24642625#### `dirname` {.collection-method}
24652626
24662627`dirname` 方法返回给定字符串的父目录部分:
24672628
24682629   use Illuminate\Support\Str;
2469 
 2630   
24702631   $string = Str::of('/foo/bar/baz')->dirname();
2471 
 2632   
24722633   // '/foo/bar'
24732634
24742635如有必要,你还可以指定要从字符串中删除多少目录级别:
24752636
24762637   use Illuminate\Support\Str;
2477 
 2638   
24782639   $string = Str::of('/foo/bar/baz')->dirname(2);
2479 
 2640   
24802641   // '/foo'
24812642
24822643<a name="method-fluent-str-excerpt"></a>
 2644  
24832645#### `excerpt` {.collection-method}
24842646
24852647`excerpt` 方法从字符串中提取与该字符串中短语的第一个实例匹配的摘录:
24862648
24872649   use Illuminate\Support\Str;
2488 
 2650   
24892651   $excerpt = Str::of('This is my name')->excerpt('my', [
24902652       'radius' => 3
24912653   ]);
2492 
 2654   
24932655   // '...is my na...'
24942656
24952657
 
24992661此外,还可以使用 `omission` 选项更改将添加到截断字符串之前和附加的字符串
25002662
25012663   use Illuminate\Support\Str;
2502 
 2664   
25032665   $excerpt = Str::of('This is my name')->excerpt('name', [
25042666       'radius' => 3,
25052667       'omission' => '(...) '
25062668   ]);
2507 
 2669   
25082670   // '(...) my name'
25092671
25102672<a name="method-fluent-str-ends-with"></a>
 2673  
25112674#### `endsWith` {.collection-method}
25122675
25132676`endsWith` 方法确定给定字符串是否以给定值结尾:
25142677
25152678   use Illuminate\Support\Str;
2516 
 2679   
25172680   $result = Str::of('This is my name')->endsWith('name');
2518 
 2681   
25192682   // true
25202683
25212684你还可以传递一个值数组来确定给定字符串是否以数组中的任何值结尾:
25222685
25232686   use Illuminate\Support\Str;
2524 
 2687   
25252688   $result = Str::of('This is my name')->endsWith(['name', 'foo']);
2526 
 2689   
25272690   // true
2528 
 2691   
25292692   $result = Str::of('This is my name')->endsWith(['this', 'foo']);
2530 
 2693   
25312694   // false
25322695
25332696<a name="method-fluent-str-exactly"></a>
 2697  
25342698#### `exactly` {.collection-method}
25352699
25362700`exactly` 方法确定给定的字符串是否与另一个字符串完全匹配:
25372701
25382702   use Illuminate\Support\Str;
2539 
 2703   
25402704   $result = Str::of('Laravel')->exactly('Laravel');
2541 
 2705   
25422706   // true
25432707
25442708<a name="method-fluent-str-explode"></a>
 2709  
25452710#### `explode` {.collection-method}
25462711
25472712`explode` 方法按给定的分隔符拆分字符串并返回包含拆分字符串的每个部分的集合:
25482713
25492714   use Illuminate\Support\Str;
2550 
 2715   
25512716   $collection = Str::of('foo bar baz')->explode(' ');
2552 
 2717   
25532718   // collect(['foo', 'bar', 'baz'])
25542719
25552720<a name="method-fluent-str-finish"></a>
 2721  
25562722#### `finish` {.collection-method}
25572723
25582724`finish` 方法将给定值的单个实例添加到字符串中(如果它尚未以该值结尾):
25592725   use Illuminate\Support\Str;
25602726
25612727   $adjusted = Str::of('this/string')->finish('/');
2562 
 2728   
25632729   // this/string/
2564 
 2730   
25652731   $adjusted = Str::of('this/string/')->finish('/');
2566 
 2732   
25672733   // this/string/
25682734
25692735
25702736
25712737<a name="method-fluent-str-headline"></a>
 2738  
25722739#### `headline` {.collection-method}
25732740
25742741`headline` 方法会将由大小写、连字符或下划线分隔的字符串转换为空格分隔的字符串,每个单词的首字母大写:
25752742
25762743   use Illuminate\Support\Str;
2577 
 2744   
25782745   $headline = Str::of('taylor_otwell')->headline();
2579 
 2746   
25802747   // Taylor Otwell
2581 
 2748   
25822749   $headline = Str::of('EmailNotificationSent')->headline();
2583 
 2750   
25842751   // Email Notification Sent
25852752
25862753<a name="method-fluent-str-inline-markdown"></a>
 2754  
25872755#### `inlineMarkdown` {.collection-method}
25882756
25892757`inlineMarkdown` 方法使用 [CommonMark](https://commonmark.thephpleague.com/) 将 GitHub 风格的 Markdown 转换为内联 HTML。 但是,与 `markdown` 方法不同,它不会将所有生成的 HTML 包装在块级元素中:
25902758
25912759   use Illuminate\Support\Str;
2592 
 2760   
25932761   $html = Str::of('**Laravel**')->inlineMarkdown();
2594 
 2762   
25952763   // <strong>Laravel</strong>
25962764
25972765<a name="method-fluent-str-is"></a>
 2766  
25982767#### `is` {.collection-method}
25992768
26002769`is` 方法确定给定字符串是否与给定模式匹配。 星号可用作通配符值
26012770
26022771   use Illuminate\Support\Str;
2603 
 2772   
26042773   $matches = Str::of('foobar')->is('foo*');
2605 
 2774   
26062775   // true
2607 
 2776   
26082777   $matches = Str::of('foobar')->is('baz*');
2609 
 2778   
26102779   // false
26112780
26122781<a name="method-fluent-str-is-ascii"></a>
 2782  
26132783#### `isAscii` {.collection-method}
26142784
26152785`isAscii` 方法确定给定字符串是否为 ASCII 字符串:
26162786
26172787   use Illuminate\Support\Str;
2618 
 2788   
26192789   $result = Str::of('Taylor')->isAscii();
2620 
 2790   
26212791   // true
2622 
 2792   
26232793   $result = Str::of('ü')->isAscii();
2624 
 2794   
26252795   // false
26262796
26272797<a name="method-fluent-str-is-empty"></a>
 2798  
26282799#### `isEmpty` {.collection-method}
26292800
26302801`isEmpty` 方法确定给定的字符串是否为空:
26312802
26322803   use Illuminate\Support\Str;
2633 
 2804   
26342805   $result = Str::of(' ')->trim()->isEmpty();
2635 
 2806   
26362807   // true
2637 
 2808   
26382809   $result = Str::of('Laravel')->trim()->isEmpty();
2639 
 2810   
26402811   // false
26412812
26422813<a name="method-fluent-str-is-not-empty"></a>
 2814  
26432815#### `isNotEmpty` {.collection-method}
26442816
26452817`isNotEmpty` 方法确定给定的字符串是否不为空:
26462818
26472819   use Illuminate\Support\Str;
2648 
 2820   
26492821   $result = Str::of(' ')->trim()->isNotEmpty();
2650 
 2822   
26512823   // false
2652 
 2824   
26532825   $result = Str::of('Laravel')->trim()->isNotEmpty();
2654 
 2826   
26552827   // true
26562828
26572829
26582830
26592831<a name="method-fluent-str-is-json"></a>
 2832  
26602833#### `isJson` {.collection-method}
26612834
26622835`isJson` 方法确定给定的字符串是否是有效的 JSON:
26632836
26642837   use Illuminate\Support\Str;
2665 
 2838   
26662839   $result = Str::of('[1,2,3]')->isJson();
2667 
 2840   
26682841   // true
2669 
 2842   
26702843   $result = Str::of('{"first": "John", "last": "Doe"}')->isJson();
2671 
 2844   
26722845   // true
2673 
 2846   
26742847   $result = Str::of('{first: "John", last: "Doe"}')->isJson();
2675 
 2848   
26762849   // false
26772850
26782851<a name="method-fluent-str-is-ulid"></a>
 2852  
26792853#### `isUlid` {.collection-method}
26802854
26812855`isUlid` 方法确定给定的字符串是否一个 ULID:
26822856
26832857   use Illuminate\Support\Str;
2684 
 2858   
26852859   $result = Str::of('01gd6r360bp37zj17nxb55yv40')->isUlid();
2686 
 2860   
26872861   // true
2688 
 2862   
26892863   $result = Str::of('Taylor')->isUlid();
2690 
 2864   
26912865   // false
26922866
26932867<a name="method-fluent-str-is-uuid"></a>
 2868  
26942869#### `isUuid` {.collection-method}
26952870
26962871`isUuid` 方法确定给定的字符串是否是一个 UUID:
26972872
26982873   use Illuminate\Support\Str;
2699 
 2874   
27002875   $result = Str::of('5ace9ab9-e9cf-4ec6-a19d-5881212a452c')->isUuid();
2701 
 2876   
27022877   // true
2703 
 2878   
27042879   $result = Str::of('Taylor')->isUuid();
2705 
 2880   
27062881   // false
27072882
27082883<a name="method-fluent-str-kebab"></a>
 2884  
27092885#### `kebab` {.collection-method}
27102886
27112887`kebab` 方法转变给定的字符串为 `kebab-case`:
27122888
27132889   use Illuminate\Support\Str;
2714 
 2890   
27152891   $converted = Str::of('fooBar')->kebab();
2716 
 2892   
27172893   // foo-bar
27182894
27192895<a name="method-fluent-str-lcfirst"></a>
 2896  
27202897#### `lcfirst` {.collection-method}
27212898
27222899`lcfirst` 方法返回给定的字符串的第一个字符为小写字母:
27232900
27242901   use Illuminate\Support\Str;
2725 
 2902   
27262903   $string = Str::of('Foo Bar')->lcfirst();
2727 
 2904   
27282905   // foo Bar
27292906
27302907
27312908<a name="method-fluent-str-length"></a>
 2909  
27322910#### `length` {.collection-method}
27332911
27342912`length` 方法返回给定字符串的长度:
27352913
27362914   use Illuminate\Support\Str;
2737 
 2915   
27382916   $length = Str::of('Laravel')->length();
2739 
 2917   
27402918   // 7
27412919
27422920<a name="method-fluent-str-limit"></a>
 2921  
27432922#### `limit` {.collection-method}
27442923
27452924`limit` 方法将给定的字符串截断为指定的长度:
27462925
27472926   use Illuminate\Support\Str;
2748 
 2927   
27492928   $truncated = Str::of('The quick brown fox jumps over the lazy dog')->limit(20);
2750 
 2929   
27512930   // The quick brown fox...
27522931
27532932
 
27552934你也可以通过第二个参数来改变追加到末尾的字符串:
27562935
27572936   use Illuminate\Support\Str;
2758 
 2937   
27592938   $truncated = Str::of('The quick brown fox jumps over the lazy dog')->limit(20, ' (...)');
2760 
 2939   
27612940   // The quick brown fox (...)
27622941
27632942<a name="method-fluent-str-lower"></a>
 2943  
27642944#### `lower`
27652945
2766 `lower` 方法将指定字符串转换为小写:
2767 
2768    use Illuminate\Support\Str;
2769 
 2946`lower` 方法将指定字符串转换为小写:
 2947
 2948   use Illuminate\Support\Str;
 2949   
27702950   $result = Str::of('LARAVEL')->lower();
2771 
 2951   
27722952   // 'laravel'
27732953
27742954<a name="method-fluent-str-ltrim"></a>
 2955  
27752956#### `ltrim`
27762957
2777 `ltrim` 方法移除字符串左端指定的字符:
2778 
2779    use Illuminate\Support\Str;
2780 
 2958`ltrim` 方法移除字符串左端指定的字符:
 2959
 2960   use Illuminate\Support\Str;
 2961   
27812962   $string = Str::of(' Laravel ')->ltrim();
2782 
 2963   
27832964   // 'Laravel '
2784 
 2965   
27852966   $string = Str::of('/Laravel/')->ltrim('/');
2786 
 2967   
27872968   // 'Laravel/'
27882969
27892970<a name="method-fluent-str-markdown"></a>
 2971  
27902972#### `markdown` {.collection-method}
27912973
2792 `markdown` 方法将 Github 风格的 Markdown 转换为 HTML:
2793 
2794    use Illuminate\Support\Str;
2795 
 2974`markdown` 方法将 Github 风格的 Markdown 转换为 HTML:
 2975
 2976   use Illuminate\Support\Str;
 2977   
27962978   $html = Str::of('# Laravel')->markdown();
2797 
 2979   
27982980   // <h1>Laravel</h1>
2799 
 2981   
28002982   $html = Str::of('# Taylor <b>Otwell</b>')->markdown([
28012983       'html_input' => 'strip',
28022984   ]);
2803 
 2985   
28042986   // <h1>Taylor Otwell</h1>
28052987
28062988<a name="method-fluent-str-mask"></a>
 2989  
28072990#### `mask`
28082991
2809 `mask` 方法用重复字符掩盖字符串的一部分,可用于模糊处理字符串的某些段,例如电子邮件地址和电话号码:
2810 
2811    use Illuminate\Support\Str;
2812 
 2992`mask` 方法用重复字符掩盖字符串的一部分,可用于模糊处理字符串的某些段,例如电子邮件地址和电话号码:
 2993
 2994   use Illuminate\Support\Str;
 2995   
28132996   $string = Str::of('taylor@example.com')->mask('*', 3);
2814 
 2997   
28152998   // tay***************
28162999
2817 需要的话,你可以提供一个负数作为 `mask` 方法的第三或第四个参数,这将指示该方法在距字符串末尾的给定距离处开始屏蔽:
 3000需要的话,你可以提供一个负数作为 `mask` 方法的第三或第四个参数,这将指示该方法在距字符串末尾的给定距离处开始屏蔽:
28183001
28193002   $string = Str::of('taylor@example.com')->mask('*', -15, 3);
2820 
 3003   
28213004   // tay***@example.com
2822 
 3005   
28233006   $string = Str::of('taylor@example.com')->mask('*', 4, -4);
2824 
 3007   
28253008   // tayl**********.com
28263009
28273010
28283011
28293012<a name="method-fluent-str-match"></a>
 3013  
28303014#### `match`
28313015
2832 `match` 方法将会返回字符串中和指定正则表达式匹配的部分:
2833 
2834    use Illuminate\Support\Str;
2835 
 3016`match` 方法将会返回字符串中和指定正则表达式匹配的部分:
 3017
 3018   use Illuminate\Support\Str;
 3019   
28363020   $result = Str::of('foo bar')->match('/bar/');
2837 
 3021   
28383022   // 'bar'
2839 
 3023   
28403024   $result = Str::of('foo bar')->match('/foo (.*)/');
2841 
 3025   
28423026   // 'bar'
28433027
28443028<a name="method-fluent-str-match-all"></a>
 3029  
28453030#### `matchAll`
28463031
2847 `matchAll` 方法将会返回一个集合,该集合包含了字符串中与指定正则表达式匹配的部分
2848 
2849    use Illuminate\Support\Str;
2850 
 3032`matchAll` 方法将会返回一个集合,该集合包含了字符串中与指定正则表达式匹配的部分
 3033
 3034   use Illuminate\Support\Str;
 3035   
28513036   $result = Str::of('bar foo bar')->matchAll('/bar/');
2852 
 3037   
28533038   // collect(['bar', 'bar'])
28543039
28553040如果你在正则表达式中指定了一个匹配组, Laravel 将会返回与该组匹配的集合:
28563041
28573042   use Illuminate\Support\Str;
2858 
 3043   
28593044   $result = Str::of('bar fun bar fly')->matchAll('/f(\w*)/');
2860 
 3045   
28613046   // collect(['un', 'ly']);
28623047
28633048如果没有找到任何匹配项,则返回空集合。
28643049
28653050<a name="method-fluent-str-is-match"></a>
 3051  
28663052#### `isMatch`
28673053
28683054`isMatch` 方法用于判断给定的字符串是否与正则表达式匹配:
28693055
28703056   use Illuminate\Support\Str;
2871 
 3057   
28723058   $result = Str::of('foo bar')->isMatch('/foo (.*)/');
2873 
 3059   
28743060   // true
2875 
 3061   
28763062   $result = Str::of('laravel')->match('/foo (.*)/');
2877 
 3063   
28783064   // false
28793065
28803066<a name="method-fluent-str-new-line"></a>
 3067  
28813068#### `newLine`
28823069
28833070`newLine` 方法将给字符串追加换行的字符:
28843071
28853072   use Illuminate\Support\Str;
2886 
 3073   
28873074   $padded = Str::of('Laravel')->newLine()->append('Framework');
2888 
 3075   
28893076   // 'Laravel
28903077   // Framework'
28913078
28923079<a name="method-fluent-str-padboth"></a>
 3080  
28933081#### `padBoth`
28943082
2895 `padBoth` 方法包装了 PHP 的 `str_pad` 函数,在指定字符串的两侧填充上另一字符串,直至该字符串到达指定的长度:
2896 
2897    use Illuminate\Support\Str;
2898 
 3083`padBoth` 方法包装了 PHP 的 `str_pad` 函数,在指定字符串的两侧填充上另一字符串,直至该字符串到达指定的长度:
 3084
 3085   use Illuminate\Support\Str;
 3086   
28993087   $padded = Str::of('James')->padBoth(10, '_');
2900 
 3088   
29013089   // '__James___'
2902 
 3090   
29033091   $padded = Str::of('James')->padBoth(10);
2904 
 3092   
29053093   // ' James  '
29063094
29073095
29083096
29093097<a name="method-fluent-str-padleft"></a>
 3098  
29103099#### `padLeft`
29113100
2912 The `padLeft` 方法包装了 PHP 的 `str_pad` 函数,在指定字符串的左侧填充上另一字符串,直至该字符串到达指定的长度:
2913 
2914    use Illuminate\Support\Str;
2915 
 3101The `padLeft` 方法包装了 PHP 的 `str_pad` 函数,在指定字符串的左侧填充上另一字符串,直至该字符串到达指定的长度:
 3102
 3103   use Illuminate\Support\Str;
 3104   
29163105   $padded = Str::of('James')->padLeft(10, '-=');
2917 
 3106   
29183107   // '-=-=-James'
2919 
 3108   
29203109   $padded = Str::of('James')->padLeft(10);
2921 
 3110   
29223111   // '    James'
29233112
29243113<a name="method-fluent-str-padright"></a>
 3114  
29253115#### `padRight`
29263116
2927 `padRight` 方法包装了 PHP 的 `str_pad` 函数,在指定字符串的右侧填充上另一字符串,直至该字符串到达指定的长度:
2928 
2929    use Illuminate\Support\Str;
2930 
 3117`padRight` 方法包装了 PHP 的 `str_pad` 函数,在指定字符串的右侧填充上另一字符串,直至该字符串到达指定的长度:
 3118
 3119   use Illuminate\Support\Str;
 3120   
29313121   $padded = Str::of('James')->padRight(10, '-');
2932 
 3122   
29333123   // 'James-----'
2934 
 3124   
29353125   $padded = Str::of('James')->padRight(10);
2936 
 3126   
29373127   // 'James    '
29383128
29393129<a name="method-fluent-str-pipe"></a>
 3130  
29403131#### `pipe`
29413132
29423133
2943 `pipe` 方法将把字符串的当前值传递给指定的函数来转换字符串:
 3134`pipe` 方法将把字符串的当前值传递给指定的函数来转换字符串:
29443135
29453136   use Illuminate\Support\Str;
29463137   use Illuminate\Support\Stringable;
2947 
 3138   
29483139   $hash = Str::of('Laravel')->pipe('md5')->prepend('Checksum: ');
2949 
 3140   
29503141   // 'Checksum: a5c95b86291ea299fcbe64458ed12702'
2951 
 3142   
29523143   $closure = Str::of('foo')->pipe(function (Stringable $str) {
29533144       return 'bar';
29543145   });
2955 
 3146   
29563147   // 'bar'
29573148
29583149<a name="method-fluent-str-plural"></a>
 3150  
29593151#### `plural`
29603152
2961 `plural` 方法将单数形式的字符串转换为复数形式。该此函数支持 [Laravel的复数化器支持的任何语言](/docs/laravel/10.x/localization#pluralization-language)
2962 
2963    use Illuminate\Support\Str;
2964 
 3153`plural` 方法将单数形式的字符串转换为复数形式。该此函数支持 [Laravel的复数化器支持的任何语言](/docs/laravel/10.x/localization#pluralization-language)
 3154
 3155   use Illuminate\Support\Str;
 3156   
29653157   $plural = Str::of('car')->plural();
2966 
 3158   
29673159   // cars
2968 
 3160   
29693161   $plural = Str::of('child')->plural();
2970 
 3162   
29713163   // children
29723164
29733165你也可以给该函数提供一个整数作为第二个参数,用于检索字符串的单数或复数形式:
29743166
29753167   use Illuminate\Support\Str;
2976 
 3168   
29773169   $plural = Str::of('child')->plural(2);
2978 
 3170   
29793171   // children
2980 
 3172   
29813173   $plural = Str::of('child')->plural(1);
2982 
 3174   
29833175   // child
29843176
29853177
29863178
29873179<a name="method-fluent-str-prepend"></a>
 3180  
29883181#### `prepend`
29893182
2990 `prepend` 方法用于在指定字符串的开头插入另一指定字符串:
2991 
2992    use Illuminate\Support\Str;
2993 
 3183`prepend` 方法用于在指定字符串的开头插入另一指定字符串:
 3184
 3185   use Illuminate\Support\Str;
 3186   
29943187   $string = Str::of('Framework')->prepend('Laravel ');
2995 
 3188   
29963189   // Laravel Framework
29973190
29983191<a name="method-fluent-str-remove"></a>
 3192  
29993193#### `remove`
30003194
3001 `remove` 方法用于从字符串中删除给定的值或值数组:
3002 
3003    use Illuminate\Support\Str;
3004 
 3195`remove` 方法用于从字符串中删除给定的值或值数组:
 3196
 3197   use Illuminate\Support\Str;
 3198   
30053199   $string = Str::of('Arkansas is quite beautiful!')->remove('quite');
3006 
 3200   
30073201   // Arkansas is beautiful!
30083202
3009 你也可以传递 `false` 作为第二个参数以在删除字符串时忽略大小写。
 3203你也可以传递 `false` 作为第二个参数以在删除字符串时忽略大小写。
30103204
30113205<a name="method-fluent-str-replace"></a>
 3206  
30123207#### `replace`
30133208
3014 `replace` 方法用于将字符串中的指定字符串替换为另一指定字符串:
3015 
3016    use Illuminate\Support\Str;
3017 
 3209`replace` 方法用于将字符串中的指定字符串替换为另一指定字符串:
 3210
 3211   use Illuminate\Support\Str;
 3212   
30183213   $replaced = Str::of('Laravel 6.x')->replace('6.x', '7.x');
3019 
 3214   
30203215   // Laravel 7.x
30213216
30223217<a name="method-fluent-str-replace-array"></a>
 3218  
30233219#### `replaceArray`
30243220
3025 `replaceArray` 方法使用数组顺序替换字符串中的给定值:
3026 
3027    use Illuminate\Support\Str;
3028 
 3221`replaceArray` 方法使用数组顺序替换字符串中的给定值:
 3222
 3223   use Illuminate\Support\Str;
 3224   
30293225   $string = 'The event will take place between ? and ?';
3030 
 3226   
30313227   $replaced = Str::of($string)->replaceArray('?', ['8:30', '9:00']);
3032 
 3228   
30333229   // The event will take place between 8:30 and 9:00
30343230
30353231<a name="method-fluent-str-replace-first"></a>
 3232  
30363233#### `replaceFirst`
30373234
3038 `replaceFirst` 方法替换字符串中给定值的第一个匹配项:
3039 
3040    use Illuminate\Support\Str;
3041 
 3235`replaceFirst` 方法替换字符串中给定值的第一个匹配项:
 3236
 3237   use Illuminate\Support\Str;
 3238   
30423239   $replaced = Str::of('the quick brown fox jumps over the lazy dog')->replaceFirst('the', 'a');
3043 
 3240   
30443241   // a quick brown fox jumps over the lazy dog
30453242
30463243<a name="method-fluent-str-replace-last"></a>
 3244  
30473245#### `replaceLast`
30483246
3049 `replaceLast` 方法替换字符串中给定值的最后一个匹配项:
3050 
3051    use Illuminate\Support\Str;
3052 
 3247`replaceLast` 方法替换字符串中给定值的最后一个匹配项:
 3248
 3249   use Illuminate\Support\Str;
 3250   
30533251   $replaced = Str::of('the quick brown fox jumps over the lazy dog')->replaceLast('the', 'a');
3054 
 3252   
30553253   // the quick brown fox jumps over a lazy dog
30563254
30573255
30583256
30593257<a name="method-fluent-str-replace-matches"></a>
 3258  
30603259#### `replaceMatches`
30613260
3062 `replaceMatches` 方法用给定的替换字符串替换与模式匹配的字符串的所有部分
3063 
3064    use Illuminate\Support\Str;
3065 
 3261`replaceMatches` 方法用给定的替换字符串替换与模式匹配的字符串的所有部分
 3262
 3263   use Illuminate\Support\Str;
 3264   
30663265   $replaced = Str::of('(+1) 501-555-1000')->replaceMatches('/[^A-Za-z0-9]++/', '')
3067 
 3266   
30683267   // '15015551000'
30693268
3070 `replaceMatches` 方法还接受一个闭包,该闭包将在字符串的每个部分与给定模式匹配时调用,从而允许你在闭包中执行替换逻辑并返回替换的值:
 3269`replaceMatches` 方法还接受一个闭包,该闭包将在字符串的每个部分与给定模式匹配时调用,从而允许你在闭包中执行替换逻辑并返回替换的值:
30713270
30723271   use Illuminate\Support\Str;
30733272   use Illuminate\Support\Stringable;
3074 
 3273   
30753274   $replaced = Str::of('123')->replaceMatches('/\d/', function (Stringable $match) {
30763275       return '['.$match[0].']';
30773276   });
3078 
 3277   
30793278   // '[1][2][3]'
30803279
30813280<a name="method-fluent-str-rtrim"></a>
 3281  
30823282#### `rtrim`
30833283
30843284`rtrim` 方法修剪给定字符串的右侧:
30853285
30863286   use Illuminate\Support\Str;
3087 
 3287   
30883288   $string = Str::of(' Laravel ')->rtrim();
3089 
 3289   
30903290   // ' Laravel'
3091 
 3291   
30923292   $string = Str::of('/Laravel/')->rtrim('/');
3093 
 3293   
30943294   // '/Laravel'
30953295
30963296<a name="method-fluent-str-scan"></a>
 3297  
30973298#### `scan`
30983299
3099 `scan` 方法根据 [PHP 函数 sscanf ](https://www.php.net/manual/en/function.sscanf.php) 支持的格式把字符串中的输入解析为集合:
3100 
3101    use Illuminate\Support\Str;
3102 
 3300`scan` 方法根据 [PHP 函数 sscanf ](https://www.php.net/manual/en/function.sscanf.php) 支持的格式把字符串中的输入解析为集合:
 3301
 3302   use Illuminate\Support\Str;
 3303   
31033304   $collection = Str::of('filename.jpg')->scan('%[^.].%s');
3104 
 3305   
31053306   // collect(['filename', 'jpg'])
31063307
31073308<a name="method-fluent-str-singular"></a>
 3309  
31083310#### `singular`
31093311
3110 `singular` 方法将字符串转换为其单数形式。此函数支持 [Laravel的复数化器支持的任何语言](/docs/laravel/10.x/localization#pluralization-language) :
3111 
3112    use Illuminate\Support\Str;
3113 
 3312`singular` 方法将字符串转换为其单数形式。此函数支持 [Laravel的复数化器支持的任何语言](/docs/laravel/10.x/localization#pluralization-language) :
 3313
 3314   use Illuminate\Support\Str;
 3315   
31143316   $singular = Str::of('cars')->singular();
3115 
 3317   
31163318   // car
3117 
 3319   
31183320   $singular = Str::of('children')->singular();
3119 
 3321   
31203322   // child
31213323
31223324
31233325
31243326<a name="method-fluent-str-slug"></a>
 3327  
31253328#### `slug` {.collection-method}
31263329
3127 `slug` 方法从给定字符串生成 URL 友好的 "slug":
3128 
3129    use Illuminate\Support\Str;
3130 
 3330`slug` 方法从给定字符串生成 URL 友好的 "slug":
 3331
 3332   use Illuminate\Support\Str;
 3333   
31313334   $slug = Str::of('Laravel Framework')->slug('-');
3132 
 3335   
31333336   // laravel-framework
31343337
31353338<a name="method-fluent-str-snake"></a>
 3339  
31363340#### `snake` {.collection-method}
31373341
3138 `snake` 方法将给定字符串转换为 `snake_case`
3139 
3140    use Illuminate\Support\Str;
3141 
 3342`snake` 方法将给定字符串转换为 `snake_case`
 3343
 3344   use Illuminate\Support\Str;
 3345   
31423346   $converted = Str::of('fooBar')->snake();
3143 
 3347   
31443348   // foo_bar
31453349
31463350<a name="method-fluent-str-split"></a>
 3351  
31473352#### `split` {.collection-method}
31483353
31493354split 方法使用正则表达式将字符串拆分为集合:
31503355
31513356   use Illuminate\Support\Str;
3152 
 3357   
31533358   $segments = Str::of('one, two, three')->split('/[\s,]+/');
3154 
 3359   
31553360   // collect(["one", "two", "three"])
31563361
31573362<a name="method-fluent-str-squish"></a>
 3363  
31583364#### `squish` {.collection-method}
31593365
31603366`squish` 方法删除字符串中所有无关紧要的空白,包括字符串之间的空白:
31613367
31623368   use Illuminate\Support\Str;
3163 
 3369   
31643370   $string = Str::of('   laravel   framework   ')->squish();
3165 
 3371   
31663372   // laravel framework
31673373
31683374<a name="method-fluent-str-start"></a>
 3375  
31693376#### `start` {.collection-method}
31703377
31713378`start` 方法将给定值的单个实例添加到字符串中,前提是该字符串尚未以该值开头:
31723379
31733380   use Illuminate\Support\Str;
3174 
 3381   
31753382   $adjusted = Str::of('this/string')->start('/');
3176 
 3383   
31773384   // /this/string
3178 
 3385   
31793386   $adjusted = Str::of('/this/string')->start('/');
3180 
 3387   
31813388   // /this/string
31823389
31833390<a name="method-fluent-str-starts-with"></a>
 3391  
31843392#### `startsWith` {.collection-method}
31853393
31863394`startsWith` 方法确定给定字符串是否以给定值开头:
31873395
31883396   use Illuminate\Support\Str;
3189 
 3397   
31903398   $result = Str::of('This is my name')->startsWith('This');
3191 
 3399   
31923400   // true
31933401
31943402<a name="method-fluent-str-studly"></a>
 3403  
31953404#### `studly` {.collection-method}
31963405
31973406`studly` 方法将给定字符串转换为 `StudlyCase`:
31983407
31993408   use Illuminate\Support\Str;
3200 
 3409   
32013410   $converted = Str::of('foo_bar')->studly();
3202 
 3411   
32033412   // FooBar
32043413
32053414
32063415
32073416<a name="method-fluent-str-substr"></a>
 3417  
32083418#### `substr` {.collection-method}
32093419
32103420`substr` 方法返回由给定的起始参数和长度参数指定的字符串部分:
32113421
32123422   use Illuminate\Support\Str;
3213 
 3423   
32143424   $string = Str::of('Laravel Framework')->substr(8);
3215 
 3425   
32163426   // Framework
3217 
 3427   
32183428   $string = Str::of('Laravel Framework')->substr(8, 5);
3219 
 3429   
32203430   // Frame
32213431
32223432<a name="method-fluent-str-substrreplace"></a>
 3433  
32233434#### `substrReplace` {.collection-method}
32243435
3225 `substrReplace` 方法在字符串的一部分中替换文本,从第二个参数指定的位置开始替换第三个参数指定的字符数。将 `0` 传递给方法的第三个参数将在指定位置插入字符串,而不替换字符串中的任何现有字符:
3226 
3227    use Illuminate\Support\Str;
3228 
 3436`substrReplace` 方法在字符串的一部分中替换文本,从第二个参数指定的位置开始替换第三个参数指定的字符数。将 `0` 传递给方法的第三个参数将在指定位置插入字符串,而不替换字符串中的任何现有字符:
 3437
 3438   use Illuminate\Support\Str;
 3439   
32293440   $string = Str::of('1300')->substrReplace(':', 2);
3230 
 3441   
32313442   // 13:
3232 
 3443   
32333444   $string = Str::of('The Framework')->substrReplace(' Laravel', 3, 0);
3234 
 3445   
32353446   // The Laravel Framework
32363447
32373448<a name="method-fluent-str-swap"></a>
 3449  
32383450#### `swap` {.collection-method}
32393451
3240 `swap` 方法使用 PHP 的 `strtr` 函数替换字符串中的多个值:
3241 
3242    use Illuminate\Support\Str;
3243 
 3452`swap` 方法使用 PHP 的 `strtr` 函数替换字符串中的多个值:
 3453
 3454   use Illuminate\Support\Str;
 3455   
32443456   $string = Str::of('Tacos are great!')
32453457       ->swap([
32463458           'Tacos' => 'Burritos',
32473459           'great' => 'fantastic',
32483460       ]);
3249 
 3461   
32503462   // Burritos are fantastic!
32513463
32523464<a name="method-fluent-str-tap"></a>
 3465  
32533466#### `tap` {.collection-method}
32543467
3255 `tap` 方法将字符串传递给给定的闭包,允许你在不影响字符串本身的情况下检查字符串并与之交互。`tap` 方法返回原始字符串,而不管闭包返回什么:
 3468`tap` 方法将字符串传递给给定的闭包,允许你在不影响字符串本身的情况下检查字符串并与之交互。`tap` 方法返回原始字符串,而不管闭包返回什么:
32563469
32573470   use Illuminate\Support\Str;
32583471   use Illuminate\Support\Stringable;
3259 
 3472   
32603473   $string = Str::of('Laravel')
32613474       ->append(' Framework')
32623475       ->tap(function (Stringable $string) {
32633476           dump('String after append: '.$string);
32643477       })
32653478       ->upper();
3266 
 3479   
32673480   // LARAVEL FRAMEWORK
32683481
32693482<a name="method-fluent-str-test"></a>
 
32713484
32723485#### `test` {.collection-method}
32733486
3274 `test` 方法确定字符串是否与给定的正则表达式模式匹配:
3275 
3276    use Illuminate\Support\Str;
3277 
 3487`test` 方法确定字符串是否与给定的正则表达式模式匹配:
 3488
 3489   use Illuminate\Support\Str;
 3490   
32783491   $result = Str::of('Laravel Framework')->test('/Laravel/');
3279 
 3492   
32803493   // true
32813494
32823495<a name="method-fluent-str-title"></a>
 3496  
32833497#### `title` {.collection-method}
32843498
3285 `title` 方法将给定字符串转换为 `title Case`:
3286 
3287    use Illuminate\Support\Str;
3288 
 3499`title` 方法将给定字符串转换为 `title Case`:
 3500
 3501   use Illuminate\Support\Str;
 3502   
32893503   $converted = Str::of('a nice title uses the correct case')->title();
3290 
 3504   
32913505   // A Nice Title Uses The Correct Case
32923506
32933507<a name="method-fluent-str-trim"></a>
 3508  
32943509#### `trim` {.collection-method}
32953510
3296 `trim` 方法修剪给定字符串:
3297 
3298    use Illuminate\Support\Str;
3299 
 3511`trim` 方法修剪给定字符串:
 3512
 3513   use Illuminate\Support\Str;
 3514   
33003515   $string = Str::of(' Laravel ')->trim();
3301 
 3516   
33023517   // 'Laravel'
3303 
 3518   
33043519   $string = Str::of('/Laravel/')->trim('/');
3305 
 3520   
33063521   // 'Laravel'
33073522
33083523<a name="method-fluent-str-ucfirst"></a>
 3524  
33093525#### `ucfirst` {.collection-method}
33103526
3311 `ucfirst` 方法返回第一个字符大写的给定字符串
3312 
3313    use Illuminate\Support\Str;
3314 
 3527`ucfirst` 方法返回第一个字符大写的给定字符串
 3528
 3529   use Illuminate\Support\Str;
 3530   
33153531   $string = Str::of('foo bar')->ucfirst();
3316 
 3532   
33173533   // Foo bar
33183534
33193535<a name="method-fluent-str-ucsplit"></a>
 3536  
33203537#### `ucsplit` {.collection-method}
33213538
33223539`ucsplit` 方法将给定的字符串按大写字符分割为一个集合:
33233540
33243541   use Illuminate\Support\Str;
3325 
 3542   
33263543   $string = Str::of('Foo Bar')->ucsplit();
3327 
 3544   
33283545   // collect(['Foo', 'Bar'])
33293546
33303547<a name="method-fluent-str-upper"></a>
 3548  
33313549#### `upper` {.collection-method}
33323550
3333 `upper` 方法将给定字符串转换为大写:
3334 
3335    use Illuminate\Support\Str;
3336 
 3551`upper` 方法将给定字符串转换为大写:
 3552
 3553   use Illuminate\Support\Str;
 3554   
33373555   $adjusted = Str::of('laravel')->upper();
3338 
 3556   
33393557   // LARAVEL
33403558
33413559<a name="method-fluent-str-when"></a>
 3560  
33423561#### `when` {.collection-method}
33433562
3344 如果给定的条件为 `true`,则 `when` 方法调用给定的闭包。闭包将接收一个流畅字符串实例:
 3563如果给定的条件为 `true`,则 `when` 方法调用给定的闭包。闭包将接收一个流畅字符串实例:
33453564
33463565   use Illuminate\Support\Str;
33473566   use Illuminate\Support\Stringable;
3348 
 3567   
33493568   $string = Str::of('Taylor')
33503569                   ->when(true, function (Stringable $string) {
33513570                       return $string->append(' Otwell');
33523571                   });
3353 
 3572   
33543573   // 'Taylor Otwell'
3355 如果需要,可以将另一个闭包作为第三个参数传递给 `when` 方法。如果条件参数的计算结果为 `false`,则将执行此闭包。
 3574
 3575如果需要,可以将另一个闭包作为第三个参数传递给 `when` 方法。如果条件参数的计算结果为 `false`,则将执行此闭包。
33563576
33573577
33583578
33593579<a name="method-fluent-str-when-contains"></a>
 3580  
33603581#### `whenContains` {.collection-method}
33613582
3362 `whenContains` 方法会在字符串包含给定的值的前提下,调用给定的闭包。闭包将接收字符流处理实例:
 3583`whenContains` 方法会在字符串包含给定的值的前提下,调用给定的闭包。闭包将接收字符流处理实例:
33633584
33643585   use Illuminate\Support\Str;
33653586   use Illuminate\Support\Stringable;
3366 
 3587   
33673588   $string = Str::of('tony stark')
33683589               ->whenContains('tony', function (Stringable $string) {
33693590                   return $string->title();
33703591               });
3371 
 3592   
33723593   // 'Tony Stark'
3373 如有必要,你可以将另一个闭包作为第三个参数传递给 `whenContains` 方法。如果字符串不包含给定值,则此闭包将执行。
 3594
 3595如有必要,你可以将另一个闭包作为第三个参数传递给 `whenContains` 方法。如果字符串不包含给定值,则此闭包将执行。
33743596
33753597你还可以传递一个值数组来确定给定的字符串是否包含数组中的任何值:
33763598
33773599   use Illuminate\Support\Str;
33783600   use Illuminate\Support\Stringable;
3379 
 3601   
33803602   $string = Str::of('tony stark')
33813603               ->whenContains(['tony', 'hulk'], function (Stringable $string) {
33823604                   return $string->title();
33833605               });
3384 
 3606   
33853607   // Tony Stark
33863608
33873609<a name="method-fluent-str-when-contains-all"></a>
 3610  
33883611#### `whenContainsAll` {.collection-method}
33893612
3390 `whenContainsAll` 方法会在字符串包含所有给定的子字符串时,调用给定的闭包。闭包将接收字符流处理实例:
 3613`whenContainsAll` 方法会在字符串包含所有给定的子字符串时,调用给定的闭包。闭包将接收字符流处理实例:
33913614
33923615   use Illuminate\Support\Str;
33933616   use Illuminate\Support\Stringable;
3394 
 3617   
33953618   $string = Str::of('tony stark')
33963619                   ->whenContainsAll(['tony', 'stark'], function (Stringable $string) {
33973620                       return $string->title();
33983621                   });
3399 
 3622   
34003623   // 'Tony Stark'
34013624
3402 如有必要,你可以将另一个闭包作为第三个参数传递给 `whenContainsAll` 方法。如果条件参数评估为 `false`,则此闭包将执行。
 3625如有必要,你可以将另一个闭包作为第三个参数传递给 `whenContainsAll` 方法。如果条件参数评估为 `false`,则此闭包将执行。
34033626
34043627<a name="method-fluent-str-when-empty"></a>
 3628  
34053629#### `whenEmpty` {.collection-method}
34063630
3407 如果字符串为空,`whenEmpty` 方法将调用给定的闭包。如果闭包返回一个值,`whenEmpty` 方法也将返回该值。如果闭包不返回值,则将返回字符流处理实例:
 3631如果字符串为空,`whenEmpty` 方法将调用给定的闭包。如果闭包返回一个值,`whenEmpty` 方法也将返回该值。如果闭包不返回值,则将返回字符流处理实例:
34083632
34093633   use Illuminate\Support\Str;
34103634   use Illuminate\Support\Stringable;
3411 
 3635   
34123636   $string = Str::of(' ')->whenEmpty(function (Stringable $string) {
34133637       return $string->trim()->prepend('Laravel');
34143638   });
3415 
 3639   
34163640   // 'Laravel'
34173641
34183642
34193643
34203644<a name="method-fluent-str-when-not-empty"></a>
 3645  
34213646#### `whenNotEmpty` {.collection-method}
34223647
3423 如果字符串不为空,`whenNotEmpty` 方法会调用给定的闭包。如果闭包返回一个值,那么 `whenNotEmpty` 方法也将返回该值。如果闭包没有返回值,则返回字符流处理实例:
 3648如果字符串不为空,`whenNotEmpty` 方法会调用给定的闭包。如果闭包返回一个值,那么 `whenNotEmpty` 方法也将返回该值。如果闭包没有返回值,则返回字符流处理实例:
34243649
34253650   use Illuminate\Support\Str;
34263651   use Illuminate\Support\Stringable;
3427 
 3652   
34283653   $string = Str::of('Framework')->whenNotEmpty(function (Stringable $string) {
34293654       return $string->prepend('Laravel ');
34303655   });
3431 
 3656   
34323657   // 'Laravel Framework'
34333658
34343659<a name="method-fluent-str-when-starts-with"></a>
 3660  
34353661#### `whenStartsWith` {.collection-method}
34363662
3437 如果字符串以给定的子字符串开头,`whenStartsWith` 方法会调用给定的闭包。闭包将接收字符流处理实例:
 3663如果字符串以给定的子字符串开头,`whenStartsWith` 方法会调用给定的闭包。闭包将接收字符流处理实例:
34383664
34393665   use Illuminate\Support\Str;
34403666   use Illuminate\Support\Stringable;
3441 
 3667   
34423668   $string = Str::of('disney world')->whenStartsWith('disney', function (Stringable $string) {
34433669       return $string->title();
34443670   });
3445 
 3671   
34463672   // 'Disney World'
34473673
34483674<a name="method-fluent-str-when-ends-with"></a>
 3675  
34493676#### `whenEndsWith` {.collection-method}
34503677
3451 如果字符串以给定的子字符串结尾,`whenEndsWith` 方法会调用给定的闭包。闭包将接收字符流处理实例:
 3678如果字符串以给定的子字符串结尾,`whenEndsWith` 方法会调用给定的闭包。闭包将接收字符流处理实例:
34523679
34533680   use Illuminate\Support\Str;
34543681   use Illuminate\Support\Stringable;
3455 
 3682   
34563683   $string = Str::of('disney world')->whenEndsWith('world', function (Stringable $string) {
34573684       return $string->title();
34583685   });
3459 
 3686   
34603687   // 'Disney World'
34613688
34623689<a name="method-fluent-str-when-exactly"></a>
 3690  
34633691#### `whenExactly` {.collection-method}
34643692
3465 如果字符串与给定字符串完全匹配,`whenExactly` 方法会调用给定的闭包。闭包将接收字符流处理实例:
 3693如果字符串与给定字符串完全匹配,`whenExactly` 方法会调用给定的闭包。闭包将接收字符流处理实例:
34663694
34673695   use Illuminate\Support\Str;
34683696   use Illuminate\Support\Stringable;
3469 
 3697   
34703698   $string = Str::of('laravel')->whenExactly('laravel', function (Stringable $string) {
34713699       return $string->title();
34723700   });
3473 
 3701   
34743702   // 'Laravel'
34753703
34763704<a name="method-fluent-str-when-not-exactly"></a>
 3705  
34773706#### `whenNotExactly` {.collection-method}
34783707
34793708如果字符串与给定字符串不完全匹配,`whenNotExactly`方法将调用给定的闭包。闭包将接收字符流处理实例:
34803709
34813710   use Illuminate\Support\Str;
34823711   use Illuminate\Support\Stringable;
3483 
 3712   
34843713   $string = Str::of('framework')->whenNotExactly('laravel', function (Stringable $string) {
34853714       return $string->title();
34863715   });
3487 
 3716   
34883717   // 'Framework'
34893718
34903719
34913720
34923721<a name="method-fluent-str-when-is"></a>
 3722  
34933723#### `whenIs` {.collection-method}
34943724
3495 如果字符串匹配给定的模式,`whenIs` 方法会调用给定的闭包。星号可用作通配符值。闭包将接收字符流处理实例:
 3725如果字符串匹配给定的模式,`whenIs` 方法会调用给定的闭包。星号可用作通配符值。闭包将接收字符流处理实例:
34963726
34973727   use Illuminate\Support\Str;
34983728   use Illuminate\Support\Stringable;
3499 
 3729   
35003730   $string = Str::of('foo/bar')->whenIs('foo/*', function (Stringable $string) {
35013731       return $string->append('/baz');
35023732   });
3503 
 3733   
35043734   // 'foo/bar/baz'
35053735
35063736<a name="method-fluent-str-when-is-ascii"></a>
 3737  
35073738#### `whenIsAscii` {.collection-method}
35083739
3509 如果字符串是 7 位 ASCII,`whenIsAscii` 方法会调用给定的闭包。闭包将接收字符流处理实例:
 3740如果字符串是 7 位 ASCII,`whenIsAscii` 方法会调用给定的闭包。闭包将接收字符流处理实例:
35103741
35113742   use Illuminate\Support\Str;
35123743   use Illuminate\Support\Stringable;
3513 
 3744   
35143745   $string = Str::of('laravel')->whenIsAscii(function (Stringable $string) {
35153746       return $string->title();
35163747   });
3517 
 3748   
35183749   // 'Laravel'
35193750
35203751<a name="method-fluent-str-when-is-ulid"></a>
 3752  
35213753#### `whenIsUlid` {.collection-method}
35223754
3523 如果字符串是有效的 ULID,`whenIsUlid` 方法会调用给定的闭包。闭包将接收字符流处理实例:
3524 
3525    use Illuminate\Support\Str;
3526 
 3755如果字符串是有效的 ULID,`whenIsUlid` 方法会调用给定的闭包。闭包将接收字符流处理实例:
 3756
 3757   use Illuminate\Support\Str;
 3758   
35273759   $string = Str::of('01gd6r360bp37zj17nxb55yv40')->whenIsUlid(function (Stringable $string) {
35283760       return $string->substr(0, 8);
35293761   });
3530 
 3762   
35313763   // '01gd6r36'
35323764
35333765<a name="method-fluent-str-when-is-uuid"></a>
 3766  
35343767#### `whenIsUuid` {.collection-method}
35353768
3536 如果字符串是有效的 UUID,`whenIsUuid` 方法会调用给定的闭包。闭包将接收字符流处理实例:
 3769如果字符串是有效的 UUID,`whenIsUuid` 方法会调用给定的闭包。闭包将接收字符流处理实例:
35373770
35383771   use Illuminate\Support\Str;
35393772   use Illuminate\Support\Stringable;
3540 
 3773   
35413774   $string = Str::of('a0a2a2d2-0b87-4a18-83f2-2529882be2de')->whenIsUuid(function (Stringable $string) {
35423775       return $string->substr(0, 8);
35433776   });
3544 
 3777   
35453778   // 'a0a2a2d2'
35463779
35473780<a name="method-fluent-str-when-test"></a>
 3781  
35483782#### `whenTest` {.collection-method}
35493783
3550 如果字符串匹配给定的正则表达式,`whenTest` 方法会调用给定的闭包。闭包将接收字符流处理实例:
 3784如果字符串匹配给定的正则表达式,`whenTest` 方法会调用给定的闭包。闭包将接收字符流处理实例:
35513785
35523786   use Illuminate\Support\Str;
35533787   use Illuminate\Support\Stringable;
3554 
 3788   
35553789   $string = Str::of('laravel framework')->whenTest('/laravel/', function (Stringable $string) {
35563790       return $string->title();
35573791   });
3558 
 3792   
35593793   // 'Laravel Framework'
35603794
35613795
35623796
35633797<a name="method-fluent-str-word-count"></a>
 3798  
35643799#### `wordCount` {.collection-method}
35653800
3566 `wordCount` 方法返回字符串包含的单词数:
 3801`wordCount` 方法返回字符串包含的单词数:
35673802
35683803```php
35693804use Illuminate\Support\Str;
 
35723807```
35733808
35743809<a name="method-fluent-str-words"></a>
 3810  
35753811#### `words` {.collection-method}
35763812
3577 `words` 方法限制字符串中的字数。如有必要,可以指定附加到截断字符串的附加字符串:
3578 
3579    use Illuminate\Support\Str;
3580 
 3813`words` 方法限制字符串中的字数。如有必要,可以指定附加到截断字符串的附加字符串:
 3814
 3815   use Illuminate\Support\Str;
 3816   
35813817   $string = Str::of('Perfectly balanced, as all things should be.')->words(3, ' >>>');
3582 
 3818   
35833819   // Perfectly balanced, as >>>
35843820
35853821<a name="urls"></a>
 3822  
35863823## URLs
35873824
35883825<a name="method-action"></a>
 3826  
35893827#### `action()` {.collection-method}
35903828
3591 `action` 函数为给定的控制器操作生成 URL:
 3829`action` 函数为给定的控制器操作生成 URL:
35923830
35933831   use App\Http\Controllers\HomeController;
3594 
 3832   
35953833   $url = action([HomeController::class, 'index']);
35963834
35973835如果该方法接受路由参数,则可以将它们作为第二个参数传递给该方法:
 
35993837   $url = action([UserController::class, 'profile'], ['id' => 1]);
36003838
36013839<a name="method-asset"></a>
 3840  
36023841#### `asset()` {.collection-method}
36033842
3604 `asset` 函数使用请求的当前方案(HTTP 或 HTTPS)生成 URL:
 3843`asset` 函数使用请求的当前方案(HTTP 或 HTTPS)生成 URL:
36053844
36063845   $url = asset('img/photo.jpg');
36073846
3608 你可以通过在`.env` 文件中设置 `ASSET_URL` 变量来配置资产 URL 主机。如果你将资产托管在外部服务(如 Amazon S3 或其他 CDN)上,这将非常有用:
 3847你可以通过在`.env` 文件中设置 `ASSET_URL` 变量来配置资产 URL 主机。如果你将资产托管在外部服务(如 Amazon S3 或其他 CDN)上,这将非常有用:
36093848
36103849   // ASSET_URL=http://example.com/assets
3611 
 3850   
36123851   $url = asset('img/photo.jpg'); // http://example.com/assets/img/photo.jpg
36133852
36143853<a name="method-route"></a>
 3854  
36153855#### `route()` {.collection-method}
36163856
3617 `route` 函数为给定的 [命名路由](/docs/laravel/10.x/routing#named-routes):
 3857`route` 函数为给定的 [命名路由](/docs/laravel/10.x/routing#named-routes):
36183858
36193859   $url = route('route.name');
36203860
 
36293869   $url = route('route.name', ['id' => 1], false);
36303870
36313871<a name="method-secure-asset"></a>
 3872  
36323873#### `secure_asset()` {.collection-method}
36333874
36343875`secure_asset` 函数使用 HTTPS 为静态资源生成 URL:
 
36363877   $url = secure_asset('img/photo.jpg');
36373878
36383879<a name="method-secure-url"></a>
 3880  
36393881#### `secure_url()` {.collection-method}
36403882
36413883`secure_url` 函数生成给定路径的完全限定 HTTPS URL。 可以在函数的第二个参数中传递额外的 URL 段:
36423884
36433885   $url = secure_url('user/profile');
3644 
 3886   
36453887   $url = secure_url('user/profile', [1]);
36463888
36473889<a name="method-to-route"></a>
 3890  
36483891#### `to_route()` {.collection-method}
36493892
36503893`to_route` 函数为给定的[命名路由](/docs/laravel/10.x/routing#named-routes) 生成一个[重定向 HTTP 响应](/docs/laravel/10.x/responses#redirects):
 
36563899   return to_route('users.show', ['user' => 1], 302, ['X-Framework' => 'Laravel']);
36573900
36583901<a name="method-url"></a>
 3902  
36593903#### `url()` {.collection-method}
36603904
36613905`url` 函数生成给定路径的完全限定 URL:
36623906
36633907   $url = url('user/profile');
3664 
 3908   
36653909   $url = url('user/profile', [1]);
36663910
36673911如果未提供路径,则返回一个 `Illuminate\Routing\UrlGenerator` 实例:
36683912
36693913   $current = url()->current();
3670 
 3914   
36713915   $full = url()->full();
3672 
 3916   
36733917   $previous = url()->previous();
36743918
36753919<a name="miscellaneous"></a>
 3920  
36763921## 杂项
36773922
36783923<a name="method-abort"></a>
 3924  
36793925#### `abort()` {.collection-method}
36803926
36813927使用`abort` 函数抛出一个[HTTP 异常](/docs/laravel/10.x/errors#http-exceptions)交给[异常处理](/docs/laravel/10.x/errors#the-exception-handler "异常处理程序")
 
36893935   abort(403, 'Unauthorized.', $headers);
36903936
36913937<a name="method-abort-if"></a>
 3938  
36923939#### `abort_if()` {.collection-method}
36933940
36943941如果给定的布尔表达式的计算结果为 `true`,则 `abort_if` 函数会抛出 HTTP 异常:
 
36983945与 `abort` 方法一样,你还可以提供异常的响应文本作为函数的第三个参数,并提供自定义响应标头数组作为函数的第四个参数。
36993946
37003947<a name="method-abort-unless"></a>
 3948  
37013949#### `abort_unless()` {.collection-method}
37023950
37033951如果给定的布尔表达式的计算结果为 `false`,则 `abort_unless` 函数会抛出 HTTP 异常:
 
37073955与 `abort` 方法一样,你还可以提供异常的响应文本作为函数的第三个参数,并提供自定义响应标头数组作为函数的第四个参数。
37083956
37093957<a name="method-app"></a>
 3958  
37103959#### `app()` {.collection-method}
37113960
37123961`app` 函数返回 [服务容器](/docs/laravel/10.x/container) 实例:
 
37183967   $api = app('HelpSpot\API');
37193968
37203969<a name="method-auth"></a>
 3970  
37213971#### `auth()` {.collection-method}
37223972
37233973`auth` 函数返回一个 [authenticator](/docs/laravel/10.x/authentication) 实例。 你可以将它用作 `Auth` 门面的替代品:
 
37293979   $user = auth('admin')->user();
37303980
37313981<a name="method-back"></a>
 3982  
37323983#### `back()` {.collection-method}
37333984
37343985`back` 函数生成一个 [重定向 HTTP 响应](/docs/laravel/10.x/responses#redirects) 到用户之前的位置:
37353986
37363987   return back($status = 302, $headers = [], $fallback = '/');
3737 
 3988   
37383989   return back();
37393990
37403991
37413992
37423993<a name="method-bcrypt"></a>
 3994  
37433995#### `bcrypt()` {.collection-method}
37443996
37453997`bcrypt` 函数 [hashes](/docs/laravel/10.x/hashing) 使用 Bcrypt 的给定值。 您可以使用此函数作为 `Hash` 门面的替代方法:
 
37473999   $password = bcrypt('my-secret-password');
37484000
37494001<a name="method-blank"></a>
 4002  
37504003#### `blank()` {.collection-method}
37514004
37524005`blank` 函数确定给定值是否为「空白」:
 
37554008   blank('  ');
37564009   blank(null);
37574010   blank(collect());
3758 
 4011   
37594012   // true
3760 
 4013   
37614014   blank(0);
37624015   blank(true);
37634016   blank(false);
3764 
 4017   
37654018   // false
37664019
37674020对于 `blank` 的反转,请参阅 [`filled`](#method-filled) 方法。
37684021
37694022<a name="method-broadcast"></a>
 4023  
37704024#### `broadcast()` {.collection-method}
37714025
37724026`broadcast` 函数 [broadcasts](/docs/laravel/10.x/broadcasting) 给定的 [event](/docs/laravel/10.x/events) 给它的听众:
37734027
37744028   broadcast(new UserRegistered($user));
3775 
 4029   
37764030   broadcast(new UserRegistered($user))->toOthers();
37774031
37784032<a name="method-cache"></a>
 4033  
37794034#### `cache()` {.collection-method}
37804035
37814036`cache` 函数可用于从 [cache](/docs/laravel/10.x/cache) 中获取值。 如果缓存中不存在给定的键,将返回一个可选的默认值:
37824037
37834038   $value = cache('key');
3784 
 4039   
37854040   $value = cache('key', 'default');
37864041
37874042你可以通过将键/值对数组传递给函数来将项目添加到缓存中。 你应该传递缓存值应被视为有效的秒数或持续时间:
37884043
37894044   cache(['key' => 'value'], 300);
3790 
 4045   
37914046   cache(['key' => 'value'], now()->addSeconds(10));
37924047
37934048<a name="method-class-uses-recursive"></a>
 4049  
37944050#### `class_uses_recursive()` {.collection-method}
37954051
37964052`class_uses_recursive` 函数返回一个类使用的所有特征,包括其所有父类使用的特征:
 
37984054   $traits = class_uses_recursive(App\Models\User::class);
37994055
38004056<a name="method-collect"></a>
 4057  
38014058#### `collect()` {.collection-method}
38024059
38034060`collect` 函数根据给定值创建一个 [collection](/docs/laravel/10.x/collections) 实例:
 
38074064
38084065
38094066<a name="method-config"></a>
 4067  
38104068#### `config()` {.collection-method}
38114069
38124070`config` 函数获取 [configuration](/docs/laravel/10.x/configuration) 变量的值。 可以使用「点」语法访问配置值,其中包括文件名和你希望访问的选项。 如果配置选项不存在,可以指定默认值并返回:
38134071
38144072   $value = config('app.timezone');
3815 
 4073   
38164074   $value = config('app.timezone', $default);
38174075
38184076你可以通过传递键/值对数组在运行时设置配置变量。 但是请注意,此函数只会影响当前请求的配置值,不会更新您的实际配置值:
 
38204078   config(['app.debug' => true]);
38214079
38224080<a name="method-cookie"></a>
 4081  
38234082#### `cookie()` {.collection-method}
38244083
38254084`cookie` 函数创建一个新的 [cookie](/docs/laravel/10.x/requests#cookies) 实例:
 
38274086   $cookie = cookie('name', 'value', $minutes);
38284087
38294088<a name="method-csrf-field"></a>
 4089  
38304090#### `csrf_field()` {.collection-method}
38314091
38324092`csrf_field` 函数生成一个 HTML `hidden` 输入字段,其中包含 CSRF 令牌的值。 例如,使用 [Blade 语法](/docs/laravel/10.x/blade):
 
38344094   {{ csrf_field() }}
38354095
38364096<a name="method-csrf-token"></a>
 4097  
38374098#### `csrf_token()` {.collection-method}
38384099
38394100`csrf_token` 函数检索当前 CSRF 令牌的值:
 
38414102   $token = csrf_token();
38424103
38434104<a name="method-decrypt"></a>
 4105  
38444106#### `decrypt()` {.collection-method}
38454107
38464108`decrypt` 函数 [解密](/docs/laravel/10.x/encryption) 给定的值。 你可以使用此函数作为 `Crypt` 门面的替代方法:
 
38484110   $password = decrypt($value);
38494111
38504112<a name="method-dd"></a>
 4113  
38514114#### `dd()` {.collection-method}
38524115
38534116`dd` 函数转储给定的变量并结束脚本的执行:
38544117
38554118   dd($value);
3856 
 4119   
38574120   dd($value1, $value2, $value3, ...);
38584121
38594122如果你不想停止脚本的执行,请改用 [`dump`](#method-dump) 函数。
38604123
38614124
38624125<a name="method-dispatch"></a>
 4126  
38634127#### `dispatch()` {.collection-method}
38644128
38654129`dispatch` 函数将给定的 [job](/docs/laravel/10.x/queues#creating-jobs) 推送到 Laravel [job queue](/docs/laravel/10.x/queues):
 
38674131   dispatch(new App\Jobs\SendEmails);
38684132
38694133<a name="method-dump"></a>
 4134  
38704135#### `dump()` {.collection-method}
38714136
38724137`dump` 函数转储给定的变量:
38734138
38744139   dump($value);
3875 
 4140   
38764141   dump($value1, $value2, $value3, ...);
38774142
38784143如果要在转储变量后停止执行脚本,请改用 [`dd`](#method-dd) 函数。
38794144
38804145<a name="method-encrypt"></a>
 4146  
38814147#### `encrypt()` {.collection-method}
38824148
38834149`encrypt` 函数 [encrypts](/docs/laravel/10.x/encryption) 给定值。 你可以使用此函数作为 `Crypt` 门面的替代方法:
 
38854151   $secret = encrypt('my-secret-value');
38864152
38874153<a name="method-env"></a>
 4154  
38884155#### `env()` {.collection-method}
38894156
38904157`env` 函数检索 [环境变量](/docs/laravel/10.x/configuration#environment-configuration) 的值或返回默认值:
38914158
38924159   $env = env('APP_ENV');
3893 
 4160   
38944161   $env = env('APP_ENV', 'production');
38954162
38964163> **警告** 
38974164> 如果你在部署过程中执行 `config:cache` 命令,你应该确保只从配置文件中调用 `env` 函数。 一旦配置被缓存,`.env` 文件将不会被加载,所有对 `env` 函数的调用都将返回 `null`。
38984165
38994166<a name="method-event"></a>
 4167  
39004168#### `event()` {.collection-method}
39014169
39024170`event` 函数将给定的 [event](/docs/laravel/10.x/events) 分派给它的监听器:
 
39044172   event(new UserRegistered($user));
39054173
39064174<a name="method-fake"></a>
 4175  
39074176#### `fake()` {.collection-method}
39084177
39094178`fake` 函数解析容器中的 [Faker](https://github.com/FakerPHP/Faker) 单例,这在模型工厂、数据库填充、测试和原型视图中创建假数据时非常有用:
 
39274196   fake('nl_NL')->name()
39284197
39294198<a name="method-filled"></a>
 4199  
39304200#### `filled()` {.collection-method}
39314201
39324202`filled` 函数确定给定值是否不是「空白」:
 
39344204   filled(0);
39354205   filled(true);
39364206   filled(false);
3937 
 4207   
39384208   // true
3939 
 4209   
39404210   filled('');
39414211   filled('  ');
39424212   filled(null);
39434213   filled(collect());
3944 
 4214   
39454215   // false
39464216
39474217对于 `filled` 的反转,请参阅 [`blank`](#method-blank) 方法。
39484218
39494219<a name="method-info"></a>
 4220  
39504221#### `info()` {.collection-method}
39514222
39524223`info` 函数会将信息写入应用程序的 [log](/docs/laravel/10.x/logging):
 
39584229   info('User login attempt failed.', ['id' => $user->id]);
39594230
39604231<a name="method-logger"></a>
 4232  
39614233#### `logger()` {.collection-method}
39624234
39634235`logger` 函数可用于将 `debug` 级别的消息写入 [log](/docs/laravel/10.x/logging):
 
39734245   logger()->error('You are not allowed here.');
39744246
39754247<a name="method-method-field"></a>
 4248  
39764249#### `method_field()` {.collection-method}
39774250
39784251`method_field` 函数生成一个 HTML `hidden` 输入字段,其中包含表单 HTTP 谓词的欺骗值。 例如,使用 [Blade 语法](/docs/laravel/10.x/blade):
 
39914264   $now = now();
39924265
39934266<a name="method-old"></a>
 4267  
39944268#### `old()` {.collection-method}
39954269
39964270`old` 函数 [retrieves](/docs/laravel/10.x/requests#retrieving-input) 一个 [old input](/docs/laravel/10.x/requests#old-input) 值闪入Session :
39974271
39984272   $value = old('value');
3999 
 4273   
40004274   $value = old('value', 'default');
40014275
40024276由于作为 `old` 函数的第二个参数提供的「默认值」通常是 Eloquent 模型的一个属性,Laravel 允许你简单地将整个 Eloquent 模型作为第二个参数传递给 `old` 函数。 这样做时,Laravel 将假定提供给 old 函数的第一个参数是 Eloquent 属性的名称,该属性应被视为「默认值」:
40034277
40044278   {{ old('name', $user->name) }}
4005 
4006    // Is equivalent to...
4007 
 4279   
 4280   // 相当于...
 4281   
40084282   {{ old('name', $user) }}
40094283
40104284<a name="method-optional"></a>
 4285  
40114286#### `optional()` {.collection-method}
40124287
40134288`optional` 函数接受任何参数并允许您访问该对象的属性或调用方法。 如果给定对象为「null」,属性和方法将返回「null」而不是导致错误:
40144289
40154290   return optional($user->address)->street;
4016 
 4291   
40174292   {!! old('name', optional($user)->name) !!}
40184293
40194294`optional` 函数也接受一个闭包作为它的第二个参数。 如果作为第一个参数提供的值不为空,则将调用闭包:
 
40234298   });
40244299
40254300<a name="method-policy"></a>
 4301  
40264302#### `policy()` {.collection-method}
40274303
40284304`policy` 方法检索给定类的 [policy](/docs/laravel/10.x/authorization#creating-policies) 实例:
 
40304306   $policy = policy(App\Models\User::class);
40314307
40324308<a name="method-redirect"></a>
 4309  
40334310#### `redirect()` {.collection-method}
40344311
40354312
 
40374314`redirect` 函数返回一个[重定向 HTTP 响应](/docs/laravel/10.x/responses#redirects),或者如果不带参数调用则返回重定向器实例:
40384315
40394316   return redirect($to = null, $status = 302, $headers = [], $https = null);
4040 
 4317   
40414318   return redirect('/home');
4042 
 4319   
40434320   return redirect()->route('route.name');
40444321
40454322<a name="method-report"></a>
 4323  
40464324#### `report()` {.collection-method}
40474325
40484326`report` 函数将使用您的 [异常处理程序](/docs/laravel/10.x/errors#the-exception-handler) 报告异常:
 
40544332   report('Something went wrong.');
40554333
40564334<a name="method-report-if"></a>
 4335  
40574336#### `report_if()` {.collection-method}
40584337
40594338如果给定条件为「真」,「report_if」函数将使用您的[异常处理程序](/docs/laravel/10.x/errors#the-exception-handler) 报告异常:
40604339
40614340   report_if($shouldReport, $e);
4062 
 4341   
40634342   report_if($shouldReport, 'Something went wrong.');
40644343
40654344<a name="method-report-unless"></a>
 4345  
40664346#### `report_unless()` {.collection-method}
40674347
40684348如果给定条件为 `false`,`report_unless` 函数将使用你的 [异常处理程序](/docs/laravel/10.x/errors#the-exception-handler) 报告异常:
40694349
40704350   report_unless($reportingDisabled, $e);
4071 
 4351   
40724352   report_unless($reportingDisabled, 'Something went wrong.');
40734353
40744354<a name="method-request"></a>
 4355  
40754356#### `request()` {.collection-method}
40764357
40774358`request` 函数返回当前的 [request](/docs/laravel/10.x/requests) 实例或从当前请求中获取输入字段的值:
40784359
40794360   $request = request();
4080 
 4361   
40814362   $value = request('key', $default);
40824363
40834364<a name="method-rescue"></a>
 4365  
40844366#### `rescue()` {.collection-method}
40854367
40864368`rescue` 函数执行给定的闭包并捕获其执行期间发生的任何异常。 捕获的所有异常都将发送到你的[异常处理程序](/docs/laravel/10.x/errors#the-exception-handler); 但是,请求将继续处理:
 
40964378   return rescue(function () {
40974379       return $this->method();
40984380   }, false);
4099 
 4381   
41004382   return rescue(function () {
41014383       return $this->method();
41024384   }, function () {
 
41044386   });
41054387
41064388<a name="method-resolve"></a>
 4389  
41074390#### `resolve()` {.collection-method}
41084391
41094392`resolve` 函数使用 [服务容器](/docs/laravel/10.x/container) 将给定的类或接口名称解析为实例:
 
41114394   $api = resolve('HelpSpot\API');
41124395
41134396<a name="method-response"></a>
 4397  
41144398#### `response()` {.collection-method}
41154399
41164400`response` 函数创建一个 [response](/docs/laravel/10.x/responses) 实例或获取响应工厂的实例:
41174401
41184402   return response('Hello World', 200, $headers);
4119 
 4403   
41204404   return response()->json(['foo' => 'bar'], 200, $headers);
41214405
41224406<a name="method-retry"></a>
 4407  
41234408#### `retry()` {.collection-method}
41244409
41254410`retry` 函数尝试执行给定的回调,直到达到给定的最大尝试阈值。 如果回调没有抛出异常,则返回它的返回值。 如果回调抛出异常,它会自动重试。 如果超过最大尝试次数,将抛出异常:
41264411
41274412   return retry(5, function () {
4128        // Attempt 5 times while resting 100ms between attempts...
 4413       // 尝试 5 次,两次尝试之间休息 100 ms...
41294414   }, 100);
41304415
41314416如果想手动计算两次尝试之间休眠的毫秒数,你可以将闭包作为第三个参数传递给 `retry` 函数:
41324417
41334418   use Exception;
4134 
 4419   
41354420   return retry(5, function () {
41364421       // ...
41374422   }, function (int $attempt, Exception $exception) {
 
41414426为方便起见,你可以提供一个数组作为「retry」函数的第一个参数。 该数组将用于确定后续尝试之间要休眠多少毫秒:
41424427
41434428   return retry([100, 200], function () {
4144        // Sleep for 100ms on first retry, 200ms on second retry...
 4429       // 第一次重试时休眠 100 ms,第二次重试时休眠 200 ms...
41454430   });
41464431
41474432
 
41494434要仅在特定条件下重试,您可以将闭包作为第四个参数传递给 `retry` 函数:
41504435
41514436   use Exception;
4152 
 4437   
41534438   return retry(5, function () {
41544439       // ...
41554440   }, 100, function (Exception $exception) {
 
41574442   });
41584443
41594444<a name="method-session"></a>
 4445  
41604446#### `session()` {.collection-method}
41614447
41624448`session` 函数可用于获取或设置 [session](/docs/laravel/10.x/session) 值:
 
41704456如果没有值传递给函数,会话存储将被返回:
41714457
41724458   $value = session()->get('key');
4173 
 4459   
41744460   session()->put('key', $value);
41754461
41764462<a name="method-tap"></a>
 4463  
41774464#### `tap()` {.collection-method}
41784465
41794466`tap` 函数接受两个参数:一个任意的 `$value` 和一个闭包。 `$value` 将传递给闭包,然后由 `tap` 函数返回。 闭包的返回值是无关紧要的:
41804467
41814468   $user = tap(User::first(), function (User $user) {
41824469       $user->name = 'taylor';
4183 
 4470   
41844471       $user->save();
41854472   });
41864473
 
42004487
42014488
42024489<a name="method-throw-if"></a>
 4490  
42034491#### `throw_if()` {.collection-method}
42044492
42054493如果给定的布尔表达式的计算结果为「真」,则 `throw_if` 函数会抛出给定的异常:
42064494
42074495   throw_if(! Auth::user()->isAdmin(), AuthorizationException::class);
4208 
 4496   
42094497   throw_if(
42104498       ! Auth::user()->isAdmin(),
42114499       AuthorizationException::class,
 
42134501   );
42144502
42154503<a name="method-throw-unless"></a>
 4504  
42164505#### `throw_unless()` {.collection-method}
42174506
42184507如果给定的布尔表达式的计算结果为 `false`,则 `throw_unless` 函数会抛出给定的异常:
42194508
42204509   throw_unless(Auth::user()->isAdmin(), AuthorizationException::class);
4221 
 4510   
42224511   throw_unless(
42234512       Auth::user()->isAdmin(),
42244513       AuthorizationException::class,
 
42264515   );
42274516
42284517<a name="method-today"></a>
 4518  
42294519#### `today()` {.collection-method}
42304520
42314521如果给定的布尔表达式的计算结果为 `false`,则 `throw_unless` 函数会抛出给定的异常:
 
42334523   $today = today();
42344524
42354525<a name="method-trait-uses-recursive"></a>
 4526  
42364527#### `trait_uses_recursive()` {.collection-method}
42374528
42384529`trait_uses_recursive` 函数返回特征使用的所有 trait:
 
42404531   $traits = trait_uses_recursive(\Illuminate\Notifications\Notifiable::class);
42414532
42424533<a name="method-transform"></a>
 4534  
42434535#### `transform()` {.collection-method}
42444536
42454537如果值不是 [blank](#method-blank),则 transform 函数会对给定值执行闭包,然后返回闭包的返回值:
 
42474539   $callback = function (int $value) {
42484540       return $value * 2;
42494541   };
4250 
 4542   
42514543   $result = transform(5, $callback);
4252 
 4544   
42534545   // 10
42544546
42554547默认值或闭包可以作为函数的第三个参数传递。 如果给定值为空,将返回此值:
42564548
42574549   $result = transform(null, $callback, 'The value is blank');
4258 
 4550   
42594551   // The value is blank
42604552
42614553<a name="method-validator"></a>
 4554  
42624555#### `validator()` {.collection-method}
42634556
42644557`validator` 函数使用给定的参数创建一个新的 [validator](/docs/laravel/10.x/validation) 实例。 你可以将它用作 `Validator` 门面的替代品:
 
42684561
42694562
42704563<a name="method-value"></a>
 4564  
42714565#### `value()` {.collection-method}
42724566
42734567`value` 函数返回给定的值。 但是,如果将闭包传递给函数,则将执行闭包并返回其返回值:
42744568
42754569   $result = value(true);
4276 
 4570   
42774571   // true
4278 
 4572   
42794573   $result = value(function () {
42804574       return false;
42814575   });
4282 
 4576   
42834577   // false
4284    
 4578
42854579可以将其他参数传递给「value」函数。 如果第一个参数是一个闭包,那么附加参数将作为参数传递给闭包,否则它们将被忽略:
42864580
42874581   $result = value(function (string $name) {
 
42914585   // 'Taylor'
42924586
42934587<a name="method-view"></a>
 4588  
42944589#### `view()` {.collection-method}
42954590
42964591`view` 函数检索一个 [view](/docs/laravel/10.x/views) 实例:
 
42984593   return view('auth.login');
42994594
43004595<a name="method-with"></a>
 4596  
43014597#### `with()` {.collection-method}
43024598
43034599`with` 函数返回给定的值。 如果将闭包作为函数的第二个参数传递,则将执行闭包并返回其返回值:
 
43054601   $callback = function (mixed $value) {
43064602       return is_numeric($value) ? $value * 2 : 0;
43074603   };
4308 
 4604   
43094605   $result = with(5, $callback);
4310 
 4606   
43114607   // 10
4312 
 4608   
43134609   $result = with(null, $callback);
4314 
 4610   
43154611   // 0
4316 
 4612   
43174613   $result = with(5, null);
4318 
 4614   
43194615   // 5
43204616
43214617<a name="other-utilities"></a>
 4618  
43224619## 其他
43234620
43244621<a name="benchmarking"></a>
4325 ### Benchmarking
 4622
 4623### 基准测试
43264624
43274625有时你可能希望快速测试应用程序某些部分的性能。 在这些情况下,您可以使用 Benchmark 支持类来测量给定回调完成所需的毫秒数:
43284626
43294627   <?php
4330 
 4628   
43314629   use App\Models\User;
43324630   use Illuminate\Support\Benchmark;
4333 
 4631   
43344632   Benchmark::dd(fn () => User::find(1)); // 0.1 ms
4335 
 4633   
43364634   Benchmark::dd([
43374635       'Scenario 1' => fn () => User::count(), // 0.5 ms
43384636       'Scenario 2' => fn () => User::all()->count(), // 20.0 ms
 
43464644   Benchmark::dd(fn () => User::count(), iterations: 10); // 0.5 ms
43474645
43484646<a name="pipeline"></a>
4349 ### Pipeline
 4647
 4648### 管道
43504649
43514650Laravel 的 Pipeline 门面提供了一种便捷的方式来通过一系列可调用类、闭包或可调用对象「管道」给定输入,让每个类都有机会检查或修改输入并调用管道中的下一个可调用对象:
43524651
 
43904689
43914690
43924691<a name="lottery"></a>
4393 ### Lottery
 4692
 4693### 彩票
43944694
43954695Laravel 的 Lottery 类可用于根据一组给定的赔率执行回调。 当你只想为一定比例的传入请求执行代码时,这会特别有用:
43964696
43974697   use Illuminate\Support\Lottery;
4398 
 4698   
43994699   Lottery::odds(1, 20)
44004700       ->winner(fn () => $user->won())
44014701       ->loser(fn () => $user->lost())
 
44064706   use Carbon\CarbonInterval;
44074707   use Illuminate\Support\Facades\DB;
44084708   use Illuminate\Support\Lottery;
4409 
 4709   
44104710   DB::whenQueryingForLongerThan(
44114711       CarbonInterval::seconds(2),
44124712       Lottery::odds(1, 100)->winner(fn () => report('Querying > 2 seconds.')),
44134713   );
44144714
44154715<a name="testing-lotteries"></a>
4416 #### Testing Lotteries
 4716
 4717#### 测试彩票
44174718
44184719Laravel 提供了一些简单的方法来让你轻松测试应用程序的 Lottery 调用:
44194720
4420    // Lottery will always win...
 4721   // 彩票总是取胜...
44214722   Lottery::alwaysWin();
4422 
4423    // Lottery will always lose...
 4723   
 4724   // 彩票总是获败...
44244725   Lottery::alwaysLose();
4425 
4426    // Lottery will win then lose, and finally return to normal behavior...
 4726   
 4727   // 彩票会先赢后输,最后恢复到正常行为...
44274728   Lottery::fix([true, false]);
4428 
4429    // Lottery will return to normal behavior...
4430    Lottery::determineResultsNormally();
4431 
4432 
 4729   
 4730   // 彩票将恢复到正常行为...
 4731   Lottery::determineResultsNormally();