存在未翻译的内容
相关信息:
- 类型:文档文章
- 文章: 辅助函数
- 文档: 《Laravel 10 中文文档(10.x)》
此投稿由 MArtian 在 2年前 合并。
标题修改:
内容修改:
| Old | New | Differences |
|---|---|---|
| 3 | 3 | - [简介](#introduction) |
| 4 | 4 | - [可用方法](#available-methods) |
| 5 | 5 | - [其他实用工具](#other-utilities) |
| 6 | | |
| 7 | | |
| 8 | | |
| 6 | - [Benchmarking](#benchmarking) | |
| 7 | - [Pipeline](#pipeline) | |
| 8 | - [Lottery](#lottery) | |
| 9 | 9 | |
| 10 | 10 | <a name="introduction"></a> |
| 11 | ||
| 11 | 12 | ## 简介 |
| 12 | 13 | |
| 13 | 14 | Laravel 包含各种各样的全局 PHP 「辅助」函数,框架本身也大量的使用了这些功能函数;如果你觉的方便,你可以在你的应用中任意使用这些函数。 |
| 14 | 15 | |
| 15 | 16 | <a name="available-methods"></a> |
| 17 | ||
| 16 | 18 | ## 可用方法 |
| 17 | 19 | |
| 18 | 20 | <style> |
| 19 | 21 | .collection-method-list > p { |
| 20 | 22 | columns: 10.8em 3; -moz-columns: 10.8em 3; -webkit-columns: 10.8em 3; |
| 21 | 23 | } |
| 24 | ||
| 22 | 25 | |
| 23 | 26 | .collection-method-list a { |
| 24 | 27 | display: block; | … | … |
| 26 | 29 | text-overflow: ellipsis; |
| 27 | 30 | white-space: nowrap; |
| 28 | 31 | } |
| 32 | ||
| 29 | 33 | </style> |
| 30 | 34 | |
| 31 | 35 | <a name="arrays-and-objects-method-list"></a> |
| 36 | ||
| 32 | 37 | ### 数组 & 对象 |
| 33 | 38 | |
| 34 | 39 | <div class="collection-method-list" markdown="1"> |
| 40 | ||
| 35 | 41 | |
| 36 | 42 | [Arr::accessible](#method-array-accessible) |
| 37 | 43 | [Arr::add](#method-array-add) | … | … |
| 78 | 84 | </div> |
| 79 | 85 | |
| 80 | 86 | <a name="paths-method-list"></a> |
| 87 | ||
| 81 | 88 | ### 路径 |
| 82 | 89 | |
| 83 | 90 | <div class="collection-method-list" markdown="1"> |
| 91 | ||
| 84 | 92 | |
| 85 | 93 | [app_path](#method-app-path) |
| 86 | 94 | [base_path](#method-base-path) | … | … |
| 97 | 105 | |
| 98 | 106 | |
| 99 | 107 | <a name="strings-method-list"></a> |
| 108 | ||
| 100 | 109 | ### 字符串 |
| 101 | 110 | |
| 102 | 111 | <div class="collection-method-list" markdown="1"> |
| 112 | ||
| 103 | 113 | |
| 104 | 114 | [\__](#method-__) |
| 105 | 115 | [class_basename](#method-class-basename) | … | … |
| 173 | 183 | </div> |
| 174 | 184 | |
| 175 | 185 | <a name="fluent-strings-method-list"></a> |
| 186 | ||
| 176 | 187 | ### 字符流处理 |
| 177 | 188 | |
| 178 | 189 | <div class="collection-method-list" markdown="1"> |
| 190 | ||
| 179 | 191 | |
| 180 | 192 | [after](#method-fluent-str-after) |
| 181 | 193 | [afterLast](#method-fluent-str-after-last) | … | … |
| 271 | 283 | |
| 272 | 284 | |
| 273 | 285 | <a name="urls-method-list"></a> |
| 286 | ||
| 274 | 287 | ### URLs |
| 275 | 288 | |
| 276 | 289 | <div class="collection-method-list" markdown="1"> |
| 290 | ||
| 277 | 291 | |
| 278 | 292 | [action](#method-action) |
| 279 | 293 | [asset](#method-asset) | … | … |
| 286 | 300 | </div> |
| 287 | 301 | |
| 288 | 302 | <a name="miscellaneous-method-list"></a> |
| 303 | ||
| 289 | 304 | ### 杂项 |
| 290 | 305 | |
| 291 | 306 | <div class="collection-method-list" markdown="1"> |
| 307 | ||
| 292 | 308 | |
| 293 | 309 | [abort](#method-abort) |
| 294 | 310 | [abort_if](#method-abort-if) | … | … |
| 346 | 362 | </div> |
| 347 | 363 | |
| 348 | 364 | <a name="method-listing"></a> |
| 365 | ||
| 349 | 366 | ## 方法列表 |
| 350 | 367 | |
| 351 | 368 | <style> | … | … |
| 353 | 370 | font-size: 14px; |
| 354 | 371 | } |
| 355 | 372 | |
| 373 | ||
| 356 | 374 | .collection-method:not(.first-collection-method) { |
| 357 | 375 | margin-top: 50px; |
| 358 | 376 | } |
| 377 | ||
| 359 | 378 | </style> |
| 360 | 379 | |
| 361 | 380 | <a name="arrays"></a> |
| 381 | ||
| 362 | 382 | ## 数组 & 对象 |
| 363 | 383 | |
| 364 | 384 | <a name="method-array-accessible"></a> |
| 385 | ||
| 365 | 386 | #### `Arr::accessible()` {.collection-method .first-collection-method} |
| 366 | 387 | |
| 367 | `Arr::accessible` | |
| 388 | `Arr::accessible` 方法检查给定的值是否可被数组式访问: | |
| 368 | 389 | |
| 369 | 390 | use Illuminate\Support\Arr; |
| 370 | 391 | use Illuminate\Support\Collection; |
| 371 | ||
| 392 | ||
| 372 | 393 | $isAccessible = Arr::accessible(['a' => 1, 'b' => 2]); |
| 373 | ||
| 394 | ||
| 374 | 395 | // true |
| 375 | ||
| 396 | ||
| 376 | 397 | $isAccessible = Arr::accessible(new Collection); |
| 377 | ||
| 398 | ||
| 378 | 399 | // true |
| 379 | ||
| 400 | ||
| 380 | 401 | $isAccessible = Arr::accessible('abc'); |
| 381 | ||
| 402 | ||
| 382 | 403 | // false |
| 383 | ||
| 404 | ||
| 384 | 405 | $isAccessible = Arr::accessible(new stdClass); |
| 385 | ||
| 406 | ||
| 386 | 407 | // false |
| 387 | 408 | |
| 388 | 409 | <a name="method-array-add"></a> |
| 410 | ||
| 389 | 411 | #### `Arr::add()` {.collection-method} |
| 390 | 412 | |
| 391 | 如果给定的键名在数组中不存在键值或该键值设置为 `null` ,那么 `Arr::add` | |
| 392 |
| |
| 393 | use Illuminate\Support\Arr;
| |
| 394 | ||
| 413 | 如果给定的键名在数组中不存在键值或该键值设置为 `null` ,那么 `Arr::add` 方法将会把给定的键值对添加到数组中: | |
| 414 | ||
| 415 | use Illuminate\Support\Arr; | |
| 416 | ||
| 395 | 417 | $array = Arr::add(['name' => 'Desk'], 'price', 100); |
| 396 | ||
| 418 | ||
| 397 | 419 | // ['name' => 'Desk', 'price' => 100] |
| 398 | ||
| 420 | ||
| 399 | 421 | $array = Arr::add(['name' => 'Desk', 'price' => null], 'price', 100); |
| 400 | ||
| 422 | ||
| 401 | 423 | // ['name' => 'Desk', 'price' => 100] |
| 402 | 424 | |
| 403 | 425 | |
| 404 | 426 | |
| 405 | 427 | |
| 406 | 428 | <a name="method-array-collapse"></a> |
| 429 | ||
| 407 | 430 | #### `Arr::collapse()` {.collection-method} |
| 408 | 431 | |
| 409 | `Arr::collapse` | |
| 410 |
| |
| 411 | use Illuminate\Support\Arr;
| |
| 412 | ||
| 432 | `Arr::collapse` 方法将多个数组合并为一个数组: | |
| 433 | ||
| 434 | use Illuminate\Support\Arr; | |
| 435 | ||
| 413 | 436 | $array = Arr::collapse([[1, 2, 3], [4, 5, 6], [7, 8, 9]]); |
| 414 | ||
| 437 | ||
| 415 | 438 | // [1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 416 | 439 | |
| 417 | 440 | <a name="method-array-crossjoin"></a> |
| 441 | ||
| 418 | 442 | #### `Arr::crossJoin()` {.collection-method} |
| 419 | 443 | |
| 420 | `Arr::crossJoin` | |
| 421 |
| |
| 422 | use Illuminate\Support\Arr;
| |
| 423 | ||
| 444 | `Arr::crossJoin` 方法交叉连接给定的数组,返回具有所有可能排列的笛卡尔乘积: | |
| 445 | ||
| 446 | use Illuminate\Support\Arr; | |
| 447 | ||
| 424 | 448 | $matrix = Arr::crossJoin([1, 2], ['a', 'b']); |
| 425 | ||
| 449 | ||
| 426 | 450 | /* |
| 427 | 451 | [ |
| 428 | 452 | [1, 'a'], | … | … |
| 431 | 455 | [2, 'b'], |
| 432 | 456 | ] |
| 433 | 457 | */ |
| 434 | ||
| 458 | ||
| 435 | 459 | $matrix = Arr::crossJoin([1, 2], ['a', 'b'], ['I', 'II']); |
| 436 | ||
| 460 | ||
| 437 | 461 | /* |
| 438 | 462 | [ |
| 439 | 463 | [1, 'a', 'I'], | … | … |
| 448 | 472 | */ |
| 449 | 473 | |
| 450 | 474 | <a name="method-array-divide"></a> |
| 475 | ||
| 451 | 476 | #### `Arr::divide()` {.collection-method} |
| 452 | 477 | |
| 453 | `Arr::divide` | |
| 454 |
| |
| 455 | use Illuminate\Support\Arr;
| |
| 456 | ||
| 478 | `Arr::divide` 方法返回一个二维数组,一个值包含原数组的键,另一个值包含原数组的值: | |
| 479 | ||
| 480 | use Illuminate\Support\Arr; | |
| 481 | ||
| 457 | 482 | [$keys, $values] = Arr::divide(['name' => 'Desk']); |
| 458 | ||
| 483 | ||
| 459 | 484 | // $keys: ['name'] |
| 460 | ||
| 485 | ||
| 461 | 486 | // $values: ['Desk'] |
| 462 | 487 | |
| 463 | 488 | <a name="method-array-dot"></a> |
| 489 | ||
| 464 | 490 | #### `Arr::dot()` {.collection-method} |
| 465 | 491 | |
| 466 | `Arr::dot` | |
| 467 |
| |
| 468 | use Illuminate\Support\Arr;
| |
| 469 | ||
| 492 | `Arr::dot` 方法将多维数组中所有的键平铺到一维数组中,新数组使用「.」符号表示层级包含关系: | |
| 493 | ||
| 494 | use Illuminate\Support\Arr; | |
| 495 | ||
| 470 | 496 | $array = ['products' => ['desk' => ['price' => 100]]]; |
| 471 | ||
| 497 | ||
| 472 | 498 | $flattened = Arr::dot($array); |
| 473 | ||
| 499 | ||
| 474 | 500 | // ['products.desk.price' => 100] |
| 475 | 501 | |
| 476 | 502 | <a name="method-array-except"></a> |
| 503 | ||
| 477 | 504 | #### `Arr::except()` {.collection-method} |
| 478 | 505 | |
| 479 | `Arr::except` | |
| 480 |
| |
| 481 | use Illuminate\Support\Arr;
| |
| 482 | ||
| 506 | `Arr::except` 方法从数组中删除指定的键值对: | |
| 507 | ||
| 508 | use Illuminate\Support\Arr; | |
| 509 | ||
| 483 | 510 | $array = ['name' => 'Desk', 'price' => 100]; |
| 484 | ||
| 511 | ||
| 485 | 512 | $filtered = Arr::except($array, ['price']); |
| 486 | ||
| 513 | ||
| 487 | 514 | // ['name' => 'Desk'] |
| 488 | 515 | |
| 489 | 516 | <a name="method-array-exists"></a> |
| 517 | ||
| 490 | 518 | #### `Arr::exists()` {.collection-method} |
| 491 | 519 | |
| 492 | `Arr::exists` | |
| 493 |
| |
| 494 | use Illuminate\Support\Arr;
| |
| 495 | ||
| 520 | `Arr::exists` 方法检查给定的键是否存在提供的数组中: | |
| 521 | ||
| 522 | use Illuminate\Support\Arr; | |
| 523 | ||
| 496 | 524 | $array = ['name' => 'John Doe', 'age' => 17]; |
| 497 | ||
| 525 | ||
| 498 | 526 | $exists = Arr::exists($array, 'name'); |
| 499 | ||
| 527 | ||
| 500 | 528 | // true |
| 501 | ||
| 529 | ||
| 502 | 530 | $exists = Arr::exists($array, 'salary'); |
| 503 | ||
| 531 | ||
| 504 | 532 | // false |
| 505 | 533 | |
| 506 | 534 | <a name="method-array-first"></a> | … | … |
| 508 | 536 | |
| 509 | 537 | #### `Arr::first()` {.collection-method} |
| 510 | 538 | |
| 511 | `Arr::first` | |
| 512 |
| |
| 513 | use Illuminate\Support\Arr;
| |
| 514 | ||
| 539 | `Arr::first` 方法返回数组中满足指定条件的第一个元素: | |
| 540 | ||
| 541 | use Illuminate\Support\Arr; | |
| 542 | ||
| 515 | 543 | $array = [100, 200, 300]; |
| 516 | ||
| 544 | ||
| 517 | 545 | $first = Arr::first($array, function (int $value, int $key) { |
| 518 | 546 | return $value >= 150; |
| 519 | 547 | }); |
| 520 | ||
| 548 | ||
| 521 | 549 | // 200 |
| 522 | 550 | |
| 523 | 551 | 可将默认值作为第三个参数传递给该方法,如果数组中没有值满足指定条件,则返回该默认值: |
| 524 | 552 | |
| 525 | 553 | use Illuminate\Support\Arr; |
| 526 | ||
| 554 | ||
| 527 | 555 | $first = Arr::first($array, $callback, $default); |
| 528 | 556 | |
| 529 | 557 | <a name="method-array-flatten"></a> |
| 558 | ||
| 530 | 559 | #### `Arr::flatten()` {.collection-method} |
| 531 | 560 | |
| 532 | `Arr::flatten` | |
| 533 |
| |
| 534 | use Illuminate\Support\Arr;
| |
| 535 | ||
| 561 | `Arr::flatten` 方法将多维数组中数组的值取出平铺为一维数组: | |
| 562 | ||
| 563 | use Illuminate\Support\Arr; | |
| 564 | ||
| 536 | 565 | $array = ['name' => 'Joe', 'languages' => ['PHP', 'Ruby']]; |
| 537 | ||
| 566 | ||
| 538 | 567 | $flattened = Arr::flatten($array); |
| 539 | ||
| 568 | ||
| 540 | 569 | // ['Joe', 'PHP', 'Ruby'] |
| 541 | 570 | |
| 542 | 571 | <a name="method-array-forget"></a> |
| 572 | ||
| 543 | 573 | #### `Arr::forget()` {.collection-method} |
| 544 | 574 | |
| 545 | `Arr::forget` | |
| 546 |
| |
| 547 | use Illuminate\Support\Arr;
| |
| 548 | ||
| 575 | `Arr::forget` 方法使用「.」符号从深度嵌套的数组中删除给定的键值对: | |
| 576 | ||
| 577 | use Illuminate\Support\Arr; | |
| 578 | ||
| 549 | 579 | $array = ['products' => ['desk' => ['price' => 100]]]; |
| 550 | ||
| 580 | ||
| 551 | 581 | Arr::forget($array, 'products.desk'); |
| 552 | ||
| 582 | ||
| 553 | 583 | // ['products' => []] |
| 554 | 584 | |
| 555 | 585 | <a name="method-array-get"></a> |
| 586 | ||
| 556 | 587 | #### `Arr::get()` {.collection-method} |
| 557 | 588 | |
| 558 | `Arr::get` | |
| 559 |
| |
| 560 | use Illuminate\Support\Arr;
| |
| 561 | ||
| 589 | `Arr::get` 方法使用「.」符号从深度嵌套的数组中根据指定键检索值: | |
| 590 | ||
| 591 | use Illuminate\Support\Arr; | |
| 592 | ||
| 562 | 593 | $array = ['products' => ['desk' => ['price' => 100]]]; |
| 563 | ||
| 594 | ||
| 564 | 595 | $price = Arr::get($array, 'products.desk.price'); |
| 565 | ||
| 596 | ||
| 566 | 597 | // 100 |
| 567 | 598 | |
| 568 | `Arr::get` | |
| 569 |
| |
| 570 | use Illuminate\Support\Arr;
| |
| 571 | ||
| 599 | `Arr::get` 方法也可以接受一个默认值,如果数组中不存在指定的键,则返回默认值: | |
| 600 | ||
| 601 | use Illuminate\Support\Arr; | |
| 602 | ||
| 572 | 603 | $discount = Arr::get($array, 'products.desk.discount', 0); |
| 573 | ||
| 604 | ||
| 574 | 605 | // 0 |
| 575 | 606 | |
| 576 | 607 | <a name="method-array-has"></a> |
| 608 | ||
| 577 | 609 | #### `Arr::has()` {.collection-method} |
| 578 | 610 | |
| 579 | `Arr::has` | |
| 580 |
| |
| 581 | use Illuminate\Support\Arr;
| |
| 582 | ||
| 611 | `Arr::has` 方法使用「.」符号判断数组中是否存在指定的一个或多个键: | |
| 612 | ||
| 613 | use Illuminate\Support\Arr; | |
| 614 | ||
| 583 | 615 | $array = ['product' => ['name' => 'Desk', 'price' => 100]]; |
| 584 | ||
| 616 | ||
| 585 | 617 | $contains = Arr::has($array, 'product.name'); |
| 586 | ||
| 618 | ||
| 587 | 619 | // true |
| 588 | ||
| 620 | ||
| 589 | 621 | $contains = Arr::has($array, ['product.price', 'product.discount']); |
| 590 | ||
| 622 | ||
| 591 | 623 | // false |
| 592 | 624 | |
| 593 | 625 | |
| 594 | 626 | |
| 595 | 627 | <a name="method-array-hasany"></a> |
| 628 | ||
| 596 | 629 | #### `Arr::hasAny()` {.collection-method} |
| 597 | 630 | |
| 598 | `Arr::hasAny` | |
| 599 |
| |
| 600 | use Illuminate\Support\Arr;
| |
| 601 | ||
| 631 | `Arr::hasAny` 方法使用「.」符号判断给定集合中的任一值是否存在于数组中: | |
| 632 | ||
| 633 | use Illuminate\Support\Arr; | |
| 634 | ||
| 602 | 635 | $array = ['product' => ['name' => 'Desk', 'price' => 100]]; |
| 603 | ||
| 636 | ||
| 604 | 637 | $contains = Arr::hasAny($array, 'product.name'); |
| 605 | ||
| 638 | ||
| 606 | 639 | // true |
| 607 | ||
| 640 | ||
| 608 | 641 | $contains = Arr::hasAny($array, ['product.name', 'product.discount']); |
| 609 | ||
| 642 | ||
| 610 | 643 | // true |
| 611 | ||
| 644 | ||
| 612 | 645 | $contains = Arr::hasAny($array, ['category', 'product.discount']); |
| 613 | ||
| 646 | ||
| 614 | 647 | // false |
| 615 | 648 | |
| 616 | 649 | <a name="method-array-isassoc"></a> |
| 650 | ||
| 617 | 651 | #### `Arr::isAssoc()` {.collection-method} |
| 618 | 652 | |
| 619 | 如果给定数组是关联数组,则 `Arr::isAssoc` | |
| 620 |
| |
| 621 | use Illuminate\Support\Arr;
| |
| 622 | ||
| 653 | 如果给定数组是关联数组,则 `Arr::isAssoc` 方法返回 `true`,如果该数组没有以零开头的顺序数字键,则将其视为「关联」数组: | |
| 654 | ||
| 655 | use Illuminate\Support\Arr; | |
| 656 | ||
| 623 | 657 | $isAssoc = Arr::isAssoc(['product' => ['name' => 'Desk', 'price' => 100]]); |
| 624 | ||
| 658 | ||
| 625 | 659 | // true |
| 626 | ||
| 660 | ||
| 627 | 661 | $isAssoc = Arr::isAssoc([1, 2, 3]); |
| 628 | ||
| 662 | ||
| 629 | 663 | // false |
| 630 | 664 | |
| 631 | 665 | <a name="method-array-islist"></a> |
| 666 | ||
| 632 | 667 | #### `Arr::isList()` {.collection-method} |
| 633 | 668 | |
| 634 | 669 | 如果给定数组的键是从零开始的连续整数,则 `Arr::isList` 方法返回 `true`: |
| 635 | 670 | |
| 636 | 671 | use Illuminate\Support\Arr; |
| 637 | ||
| 672 | ||
| 638 | 673 | $isList = Arr::isList(['foo', 'bar', 'baz']); |
| 639 | ||
| 674 | ||
| 640 | 675 | // true |
| 641 | ||
| 676 | ||
| 642 | 677 | $isList = Arr::isList(['product' => ['name' => 'Desk', 'price' => 100]]); |
| 643 | ||
| 678 | ||
| 644 | 679 | // false |
| 645 | 680 | |
| 646 | 681 | <a name="method-array-join"></a> |
| 682 | ||
| 647 | 683 | #### `Arr::join()` {.collection-method} |
| 648 | 684 | |
| 649 | 685 | `Arr::join()`方法将给定数组的所有值通过给定字符串连接起来。使用此方法的第二个参数,您还可以为数组中的最后一个元素指定连接的字符串: |
| 650 | 686 | |
| 651 | 687 | use Illuminate\Support\Arr; |
| 652 | ||
| 688 | ||
| 653 | 689 | $array = ['Tailwind', 'Alpine', 'Laravel', 'Livewire']; |
| 654 | ||
| 690 | ||
| 655 | 691 | $joined = Arr::join($array, ', '); |
| 656 | ||
| 692 | ||
| 657 | 693 | // Tailwind, Alpine, Laravel, Livewire |
| 658 | ||
| 694 | ||
| 659 | 695 | $joined = Arr::join($array, ', ', ' and '); |
| 660 | ||
| 696 | ||
| 661 | 697 | // Tailwind, Alpine, Laravel and Livewire |
| 662 | 698 | |
| 663 | 699 | <a name="method-array-keyby"></a> |
| 700 | ||
| 664 | 701 | #### `Arr::keyBy()` {.collection-method} |
| 665 | 702 | |
| 666 | 703 | `Arr::keyBy()`方法通过给定键名的值对该数组进行重组。如果数组中存在多个相同的值,则只有最后一个值会出现在新数组中: |
| 667 | 704 | |
| 668 | 705 | use Illuminate\Support\Arr; |
| 669 | ||
| 706 | ||
| 670 | 707 | $array = [ |
| 671 | 708 | ['product_id' => 'prod-100', 'name' => 'Desk'], |
| 672 | 709 | ['product_id' => 'prod-200', 'name' => 'Chair'], |
| 673 | 710 | ]; |
| 674 | ||
| 711 | ||
| 675 | 712 | $keyed = Arr::keyBy($array, 'product_id'); |
| 676 | ||
| 713 | ||
| 677 | 714 | /* |
| 678 | 715 | [ |
| 679 | 716 | 'prod-100' => ['product_id' => 'prod-100', 'name' => 'Desk'], | … | … |
| 684 | 721 | |
| 685 | 722 | |
| 686 | 723 | <a name="method-array-last"></a> |
| 724 | ||
| 687 | 725 | #### `Arr::last()` {.collection-method} |
| 688 | 726 | |
| 689 | 727 | `Arr::last` 方法返回数组中满足指定条件的最后一个元素: |
| 690 | 728 | |
| 691 | 729 | use Illuminate\Support\Arr; |
| 692 | ||
| 730 | ||
| 693 | 731 | $array = [100, 200, 300, 110]; |
| 694 | ||
| 732 | ||
| 695 | 733 | $last = Arr::last($array, function (int $value, int $key) { |
| 696 | 734 | return $value >= 150; |
| 697 | 735 | }); |
| 698 | ||
| 736 | ||
| 699 | 737 | // 300 |
| 700 | 738 | |
| 701 | 739 | 将默认值作为第三个参数传递给该方法,如果没有值满足条件,则返回该默认值: |
| 702 | 740 | |
| 703 | 741 | use Illuminate\Support\Arr; |
| 704 | ||
| 742 | ||
| 705 | 743 | $last = Arr::last($array, $callback, $default); |
| 706 | 744 | |
| 707 | 745 | <a name="method-array-map"></a> |
| 746 | ||
| 708 | 747 | #### `Arr::map()` {.collection-method} |
| 709 | 748 | |
| 710 | 749 | `Arr::map` 方法用来遍历数组,并将每个值和键传递给给定的回调。数组值由回调返回的值替换: |
| 711 | 750 | |
| 712 | 751 | use Illuminate\Support\Arr; |
| 713 | ||
| 752 | ||
| 714 | 753 | $array = ['first' => 'james', 'last' => 'kirk']; |
| 715 | ||
| 754 | ||
| 716 | 755 | $mapped = Arr::map($array, function (string $value, string $key) { |
| 717 | 756 | return ucfirst($value); |
| 718 | 757 | }); |
| 719 | ||
| 758 | ||
| 720 | 759 | // ['first' => 'James', 'last' => 'Kirk'] |
| 721 | 760 | |
| 722 | 761 | <a name="method-array-only"></a> |
| 762 | ||
| 723 | 763 | #### `Arr::only()` {.collection-method} |
| 724 | 764 | |
| 725 | 765 | `Arr::only` 方法仅返回给定数组中的指定键/值对: |
| 726 | 766 | |
| 727 | 767 | use Illuminate\Support\Arr; |
| 728 | ||
| 768 | ||
| 729 | 769 | $array = ['name' => 'Desk', 'price' => 100, 'orders' => 10]; |
| 730 | ||
| 770 | ||
| 731 | 771 | $slice = Arr::only($array, ['name', 'price']); |
| 732 | ||
| 772 | ||
| 733 | 773 | // ['name' => 'Desk', 'price' => 100] |
| 734 | 774 | |
| 735 | 775 | <a name="method-array-pluck"></a> |
| 776 | ||
| 736 | 777 | #### `Arr::pluck()` {.collection-method} |
| 737 | 778 | |
| 738 | 779 | `Arr::pluck` 方法从数组中检索给定键的所有值: |
| 739 | 780 | |
| 740 | 781 | use Illuminate\Support\Arr; |
| 741 | ||
| 782 | ||
| 742 | 783 | $array = [ |
| 743 | 784 | ['developer' => ['id' => 1, 'name' => 'Taylor']], |
| 744 | 785 | ['developer' => ['id' => 2, 'name' => 'Abigail']], |
| 745 | 786 | ]; |
| 746 | ||
| 787 | ||
| 747 | 788 | $names = Arr::pluck($array, 'developer.name'); |
| 748 | ||
| 789 | ||
| 749 | 790 | // ['Taylor', 'Abigail'] |
| 750 | 791 | |
| 751 | 792 | 你也可以指定结果的键: |
| 752 | 793 | |
| 753 | 794 | use Illuminate\Support\Arr; |
| 754 | ||
| 795 | ||
| 755 | 796 | $names = Arr::pluck($array, 'developer.name', 'developer.id'); |
| 756 | ||
| 797 | ||
| 757 | 798 | // [1 => 'Taylor', 2 => 'Abigail'] |
| 758 | 799 | |
| 759 | 800 | <a name="method-array-prepend"></a> |
| 760 | 801 | |
| 761 | 802 | |
| 762 | 803 | <a name="method-array-last"></a> |
| 804 | ||
| 763 | 805 | #### `Arr::last()` {.collection-method} |
| 764 | 806 | |
| 765 | `Arr::last` | |
| 766 |
| |
| 767 | use Illuminate\Support\Arr;
| |
| 768 | ||
| 807 | `Arr::last` 方法返回数组中满足指定条件的最后一个元素: | |
| 808 | ||
| 809 | use Illuminate\Support\Arr; | |
| 810 | ||
| 769 | 811 | $array = [100, 200, 300, 110]; |
| 770 | ||
| 812 | ||
| 771 | 813 | $last = Arr::last($array, function (int $value, int $key) { |
| 772 | 814 | return $value >= 150; |
| 773 | 815 | }); |
| 774 | ||
| 816 | ||
| 775 | 817 | // 300 |
| 776 | 818 | |
| 777 | 819 | 将默认值作为第三个参数传递给该方法,如果没有值满足指定条件,则返回该默认值: |
| 778 | 820 | |
| 779 | 821 | use Illuminate\Support\Arr; |
| 780 | ||
| 822 | ||
| 781 | 823 | $last = Arr::last($array, $callback, $default); |
| 782 | 824 | |
| 783 | 825 | <a name="method-array-map"></a> |
| 826 | ||
| 784 | 827 | #### `Arr::map()` {.collection-method} |
| 785 | 828 | |
| 786 | 829 | `Arr::map` 方法遍历数组并将每个键和值传递至给定的回调方法。数组的值将替换为该回调方法返回的值: |
| 787 | 830 | |
| 788 | 831 | use Illuminate\Support\Arr; |
| 789 | ||
| 832 | ||
| 790 | 833 | $array = ['first' => 'james', 'last' => 'kirk']; |
| 791 | ||
| 834 | ||
| 792 | 835 | $mapped = Arr::map($array, function (string $value, string $key) { |
| 793 | 836 | return ucfirst($value); |
| 794 | 837 | }); |
| 795 | ||
| 838 | ||
| 796 | 839 | // ['first' => 'James', 'last' => 'Kirk'] |
| 797 | 840 | |
| 798 | 841 | <a name="method-array-only"></a> |
| 842 | ||
| 799 | 843 | #### `Arr::only()` {.collection-method} |
| 800 | 844 | |
| 801 | `Arr::only` | |
| 802 |
| |
| 803 | use Illuminate\Support\Arr;
| |
| 804 | ||
| 845 | `Arr::only` 方法只返回给定数组中指定的键值对: | |
| 846 | ||
| 847 | use Illuminate\Support\Arr; | |
| 848 | ||
| 805 | 849 | $array = ['name' => 'Desk', 'price' => 100, 'orders' => 10]; |
| 806 | ||
| 850 | ||
| 807 | 851 | $slice = Arr::only($array, ['name', 'price']); |
| 808 | ||
| 852 | ||
| 809 | 853 | // ['name' => 'Desk', 'price' => 100] |
| 810 | 854 | |
| 811 | 855 | <a name="method-array-pluck"></a> |
| 856 | ||
| 812 | 857 | #### `Arr::pluck()` {.collection-method} |
| 813 | 858 | |
| 814 | `Arr::pluck` | |
| 815 |
| |
| 816 | use Illuminate\Support\Arr;
| |
| 817 | ||
| 859 | `Arr::pluck` 方法从数组中检索给定键的所有值: | |
| 860 | ||
| 861 | use Illuminate\Support\Arr; | |
| 862 | ||
| 818 | 863 | $array = [ |
| 819 | 864 | ['developer' => ['id' => 1, 'name' => 'Taylor']], |
| 820 | 865 | ['developer' => ['id' => 2, 'name' => 'Abigail']], |
| 821 | 866 | ]; |
| 822 | ||
| 867 | ||
| 823 | 868 | $names = Arr::pluck($array, 'developer.name'); |
| 824 | ||
| 869 | ||
| 825 | 870 | // ['Taylor', 'Abigail'] |
| 826 | 871 | |
| 827 | 872 | 你还可以指定结果的键: |
| 828 | 873 | |
| 829 | 874 | use Illuminate\Support\Arr; |
| 830 | ||
| 875 | ||
| 831 | 876 | $names = Arr::pluck($array, 'developer.name', 'developer.id'); |
| 832 | ||
| 877 | ||
| 833 | 878 | // [1 => 'Taylor', 2 => 'Abigail'] |
| 834 | 879 | |
| 835 | 880 | <a name="method-array-prepend"></a> | … | … |
| 837 | 882 | |
| 838 | 883 | #### `Arr::prepend()` {.collection-method} |
| 839 | 884 | |
| 840 | `Arr::prepend` | |
| 841 |
| |
| 842 | use Illuminate\Support\Arr;
| |
| 843 | ||
| 885 | `Arr::prepend` 方法将一个值插入到数组的开始位置: | |
| 886 | ||
| 887 | use Illuminate\Support\Arr; | |
| 888 | ||
| 844 | 889 | $array = ['one', 'two', 'three', 'four']; |
| 845 | ||
| 890 | ||
| 846 | 891 | $array = Arr::prepend($array, 'zero'); |
| 847 | ||
| 892 | ||
| 848 | 893 | // ['zero', 'one', 'two', 'three', 'four'] |
| 849 | 894 | |
| 850 | 895 | 你也可以指定插入值的键: |
| 851 | 896 | |
| 852 | 897 | use Illuminate\Support\Arr; |
| 853 | ||
| 898 | ||
| 854 | 899 | $array = ['price' => 100]; |
| 855 | ||
| 900 | ||
| 856 | 901 | $array = Arr::prepend($array, 'Desk', 'name'); |
| 857 | ||
| 902 | ||
| 858 | 903 | // ['name' => 'Desk', 'price' => 100] |
| 859 | 904 | |
| 860 | 905 | <a name="method-array-prependkeyswith"></a> |
| 906 | ||
| 861 | 907 | #### `Arr::prependKeysWith()` {.collection-method} |
| 862 | 908 | |
| 863 | `Arr::prependKeysWith` | |
| 864 |
| |
| 865 | use Illuminate\Support\Arr;
| |
| 866 | ||
| 909 | `Arr::prependKeysWith` 方法为关联数组中的所有键添加给定前缀: | |
| 910 | ||
| 911 | use Illuminate\Support\Arr; | |
| 912 | ||
| 867 | 913 | $array = [ |
| 868 | 914 | 'name' => 'Desk', |
| 869 | 915 | 'price' => 100, |
| 870 | 916 | ]; |
| 871 | ||
| 917 | ||
| 872 | 918 | $keyed = Arr::prependKeysWith($array, 'product.'); |
| 873 | ||
| 919 | ||
| 874 | 920 | /* |
| 875 | 921 | [ |
| 876 | 922 | 'product.name' => 'Desk', | … | … |
| 879 | 925 | */ |
| 880 | 926 | |
| 881 | 927 | <a name="method-array-pull"></a> |
| 928 | ||
| 882 | 929 | #### `Arr::pull()` {.collection-method} |
| 883 | 930 | |
| 884 | `Arr::pull` | |
| 885 |
| |
| 886 | use Illuminate\Support\Arr;
| |
| 887 | ||
| 931 | `Arr::pull` 方法从数组中返回指定键的值并删除此键值对: | |
| 932 | ||
| 933 | use Illuminate\Support\Arr; | |
| 934 | ||
| 888 | 935 | $array = ['name' => 'Desk', 'price' => 100]; |
| 889 | ||
| 936 | ||
| 890 | 937 | $name = Arr::pull($array, 'name'); |
| 891 | ||
| 938 | ||
| 892 | 939 | // $name: Desk |
| 893 | ||
| 940 | ||
| 894 | 941 | // $array: ['price' => 100] |
| 895 | 942 | |
| 896 | 943 | 默认值可以作为第三个参数传递给该方法。如果指定键不存在,则返回该值: |
| 897 | 944 | |
| 898 | 945 | use Illuminate\Support\Arr; |
| 899 | ||
| 946 | ||
| 900 | 947 | $value = Arr::pull($array, $key, $default); |
| 901 | 948 | |
| 902 | 949 | <a name="method-array-query"></a> |
| 950 | ||
| 903 | 951 | #### `Arr::query()` {.collection-method} |
| 904 | 952 | |
| 905 | `Arr::query` | |
| 906 |
| |
| 907 | use Illuminate\Support\Arr;
| |
| 908 | ||
| 953 | `Arr::query` 方法将数组转换为查询字符串: | |
| 954 | ||
| 955 | use Illuminate\Support\Arr; | |
| 956 | ||
| 909 | 957 | $array = [ |
| 910 | 958 | 'name' => 'Taylor', |
| 911 | 959 | 'order' => [ | … | … |
| 913 | 961 | 'direction' => 'desc' |
| 914 | 962 | ] |
| 915 | 963 | ]; |
| 916 | ||
| 964 | ||
| 917 | 965 | Arr::query($array); |
| 918 | ||
| 966 | ||
| 919 | 967 | // name=Taylor&order[column]=created_at&order[direction]=desc |
| 920 | 968 | |
| 921 | 969 | <a name="method-array-random"></a> |
| 970 | ||
| 922 | 971 | #### `Arr::random()` {.collection-method} |
| 923 | 972 | |
| 924 | `Arr::random` | |
| 925 |
| |
| 926 | use Illuminate\Support\Arr;
| |
| 927 | ||
| 973 | `Arr::random` 方法从数组中随机返回一个值: | |
| 974 | ||
| 975 | use Illuminate\Support\Arr; | |
| 976 | ||
| 928 | 977 | $array = [1, 2, 3, 4, 5]; |
| 929 | ||
| 978 | ||
| 930 | 979 | $random = Arr::random($array); |
| 931 | ||
| 980 | ||
| 932 | 981 | // 4 - (retrieved randomly) |
| 933 | 982 | |
| 934 | 983 | 你还可以指定返回值的数量作为可选的第二个参数传递给该方法,请注意,提供这个参数会返回一个数组,即使是你只需要一项: |
| 935 | 984 | |
| 936 | 985 | use Illuminate\Support\Arr; |
| 937 | ||
| 986 | ||
| 938 | 987 | $items = Arr::random($array, 2); |
| 939 | ||
| 988 | ||
| 940 | 989 | // [2, 5] - (retrieved randomly) |
| 941 | 990 | |
| 942 | 991 | |
| 943 | 992 | |
| 944 | 993 | <a name="method-array-set"></a> |
| 994 | ||
| 945 | 995 | #### `Arr::set()` {.collection-method} |
| 946 | 996 | |
| 947 | `Arr::set` | |
| 948 |
| |
| 949 | use Illuminate\Support\Arr;
| |
| 950 | ||
| 997 | `Arr::set` 方法使用「.」符号在多维数组中设置指定键的值: | |
| 998 | ||
| 999 | use Illuminate\Support\Arr; | |
| 1000 | ||
| 951 | 1001 | $array = ['products' => ['desk' => ['price' => 100]]]; |
| 952 | ||
| 1002 | ||
| 953 | 1003 | Arr::set($array, 'products.desk.price', 200); |
| 954 | ||
| 1004 | ||
| 955 | 1005 | // ['products' => ['desk' => ['price' => 200]]] |
| 956 | 1006 | |
| 957 | 1007 | <a name="method-array-shuffle"></a> |
| 1008 | ||
| 958 | 1009 | #### `Arr::shuffle()` {.collection-method} |
| 959 | 1010 | |
| 960 | `Arr::shuffle` | |
| 961 |
| |
| 962 | use Illuminate\Support\Arr;
| |
| 963 | ||
| 1011 | `Arr::shuffle` 方法将数组中值进行随机排序: | |
| 1012 | ||
| 1013 | use Illuminate\Support\Arr; | |
| 1014 | ||
| 964 | 1015 | $array = Arr::shuffle([1, 2, 3, 4, 5]); |
| 965 | ||
| 1016 | ||
| 966 | 1017 | // [3, 2, 5, 1, 4] - (generated randomly) |
| 967 | 1018 | |
| 968 | 1019 | <a name="method-array-sort"></a> |
| 1020 | ||
| 969 | 1021 | #### `Arr::sort()` {.collection-method} |
| 970 | 1022 | |
| 971 | `Arr::sort` | |
| 972 |
| |
| 973 | use Illuminate\Support\Arr;
| |
| 974 | ||
| 1023 | `Arr::sort` 方法根据给定数组的值进行升序排序: | |
| 1024 | ||
| 1025 | use Illuminate\Support\Arr; | |
| 1026 | ||
| 975 | 1027 | $array = ['Desk', 'Table', 'Chair']; |
| 976 | ||
| 1028 | ||
| 977 | 1029 | $sorted = Arr::sort($array); |
| 978 | ||
| 1030 | ||
| 979 | 1031 | // ['Chair', 'Desk', 'Table'] |
| 980 | 1032 | |
| 981 | 你还可以根据给定回调 | |
| 982 |
| |
| 983 | use Illuminate\Support\Arr;
| |
| 984 | ||
| 1033 | 你还可以根据给定回调方法的返回结果对数组进行排序: | |
| 1034 | ||
| 1035 | use Illuminate\Support\Arr; | |
| 1036 | ||
| 985 | 1037 | $array = [ |
| 986 | 1038 | ['name' => 'Desk'], |
| 987 | 1039 | ['name' => 'Table'], |
| 988 | 1040 | ['name' => 'Chair'], |
| 989 | 1041 | ]; |
| 990 | ||
| 1042 | ||
| 991 | 1043 | $sorted = array_values(Arr::sort($array, function (array $value) { |
| 992 | 1044 | return $value['name']; |
| 993 | 1045 | })); |
| 994 | ||
| 1046 | ||
| 995 | 1047 | /* |
| 996 | 1048 | [ |
| 997 | 1049 | ['name' => 'Chair'], | … | … |
| 1001 | 1053 | */ |
| 1002 | 1054 | |
| 1003 | 1055 | <a name="method-array-sort-desc"></a> |
| 1056 | ||
| 1004 | 1057 | #### `Arr::sortDesc()` {.collection-method} |
| 1005 | 1058 | |
| 1006 | `Arr::sortDesc` | |
| 1007 |
| |
| 1008 | use Illuminate\Support\Arr;
| |
| 1009 | ||
| 1059 | `Arr::sortDesc` 方法根据给定数组的值进行降序排序: | |
| 1060 | ||
| 1061 | use Illuminate\Support\Arr; | |
| 1062 | ||
| 1010 | 1063 | $array = ['Desk', 'Table', 'Chair']; |
| 1011 | ||
| 1064 | ||
| 1012 | 1065 | $sorted = Arr::sortDesc($array); |
| 1013 | ||
| 1066 | ||
| 1014 | 1067 | // ['Table', 'Desk', 'Chair'] |
| 1015 | 1068 | |
| 1016 | 你还可以根据给定回调 | |
| 1017 |
| |
| 1018 | use Illuminate\Support\Arr;
| |
| 1019 | ||
| 1069 | 你还可以根据给定回调方法的返回结果对数组进行排序: | |
| 1070 | ||
| 1071 | use Illuminate\Support\Arr; | |
| 1072 | ||
| 1020 | 1073 | $array = [ |
| 1021 | 1074 | ['name' => 'Desk'], |
| 1022 | 1075 | ['name' => 'Table'], |
| 1023 | 1076 | ['name' => 'Chair'], |
| 1024 | 1077 | ]; |
| 1025 | ||
| 1078 | ||
| 1026 | 1079 | $sorted = array_values(Arr::sortDesc($array, function (array $value) { |
| 1027 | 1080 | return $value['name']; |
| 1028 | 1081 | })); |
| 1029 | ||
| 1082 | ||
| 1030 | 1083 | /* |
| 1031 | 1084 | [ |
| 1032 | 1085 | ['name' => 'Table'], | … | … |
| 1036 | 1089 | */ |
| 1037 | 1090 | |
| 1038 | 1091 | <a name="method-array-sort-recursive"></a> |
| 1092 | ||
| 1039 | 1093 | #### `Arr::sortRecursive()` {.collection-method} |
| 1040 | 1094 | |
| 1041 | `Arr::sortRecursive` | |
| 1042 |
| |
| 1043 | use Illuminate\Support\Arr;
| |
| 1044 | ||
| 1095 | `Arr::sortRecursive` 方法对给定数组进行递归排序,使用 `sort` 方法对数字索引子数组进行按值升序排序,使用 `ksort` 方法对关联子数组进行按键升序排序: | |
| 1096 | ||
| 1097 | use Illuminate\Support\Arr; | |
| 1098 | ||
| 1045 | 1099 | $array = [ |
| 1046 | 1100 | ['Roman', 'Taylor', 'Li'], |
| 1047 | 1101 | ['PHP', 'Ruby', 'JavaScript'], |
| 1048 | 1102 | ['one' => 1, 'two' => 2, 'three' => 3], |
| 1049 | 1103 | ]; |
| 1050 | ||
| 1104 | ||
| 1051 | 1105 | $sorted = Arr::sortRecursive($array); |
| 1052 | ||
| 1106 | ||
| 1053 | 1107 | /* |
| 1054 | 1108 | [ |
| 1055 | 1109 | ['JavaScript', 'PHP', 'Ruby'], | … | … |
| 1061 | 1115 | |
| 1062 | 1116 | |
| 1063 | 1117 | <a name="method-array-to-css-classes"></a> |
| 1118 | ||
| 1064 | 1119 | #### `Arr::toCssClasses()` {.collection-method} |
| 1065 | 1120 | |
| 1066 | `Arr::toCssClasses` | |
| 1067 |
| |
| 1068 | use Illuminate\Support\Arr;
| |
| 1069 | ||
| 1121 | `Arr::toCssClasses` 方法根据给定的条件编译并返回 CSS 类字符串。该方法接受一个类数组,其中数组键包含你希望添加的一个或多个 CSS Class,而值是一个布尔表达式。如果数组元素有一个数字键,它将始终包含在呈现的类列表中: | |
| 1122 | ||
| 1123 | use Illuminate\Support\Arr; | |
| 1124 | ||
| 1070 | 1125 | $isActive = false; |
| 1071 | 1126 | $hasError = true; |
| 1072 | ||
| 1127 | ||
| 1073 | 1128 | $array = ['p-4', 'font-bold' => $isActive, 'bg-red' => $hasError]; |
| 1074 | ||
| 1129 | ||
| 1075 | 1130 | $classes = Arr::toCssClasses($array); |
| 1076 | ||
| 1131 | ||
| 1077 | 1132 | /* |
| 1078 | 1133 | 'p-4 bg-red' |
| 1079 | 1134 | */ |
| 1080 | 1135 | |
| 1081 | Laravel 基于该 | |
| 1136 | Laravel 基于该方法实现 [Blade组件里的条件合并类](/docs/laravel/10.x/blade#conditionally-merge-classes) 以及 `@class` [Blade指令](/docs/laravel/10.x/blade#conditional-classes): | |
| 1082 | 1137 | |
| 1083 | 1138 | <a name="method-array-undot"></a> |
| 1139 | ||
| 1084 | 1140 | #### `Arr::undot()` {.collection-method} |
| 1085 | 1141 | |
| 1086 | `Arr::undot` | |
| 1087 |
| |
| 1088 | use Illuminate\Support\Arr;
| |
| 1089 | ||
| 1142 | `Arr::undot` 方法使用「.」符号将一维数组扩展为多维数组: | |
| 1143 | ||
| 1144 | use Illuminate\Support\Arr; | |
| 1145 | ||
| 1090 | 1146 | $array = [ |
| 1091 | 1147 | 'user.name' => 'Kevin Malone', |
| 1092 | 1148 | 'user.occupation' => 'Accountant', |
| 1093 | 1149 | ]; |
| 1094 | ||
| 1150 | ||
| 1095 | 1151 | $array = Arr::undot($array); |
| 1096 | ||
| 1152 | ||
| 1097 | 1153 | // ['user' => ['name' => 'Kevin Malone', 'occupation' => 'Accountant']] |
| 1098 | 1154 | |
| 1099 | 1155 | <a name="method-array-where"></a> |
| 1156 | ||
| 1100 | 1157 | #### `Arr::where()` {.collection-method} |
| 1101 | 1158 | |
| 1102 | `Arr::where` | |
| 1103 |
| |
| 1104 | use Illuminate\Support\Arr;
| |
| 1105 | ||
| 1159 | `Arr::where` 方法使用给定的回调函数返回的结果过滤数组: | |
| 1160 | ||
| 1161 | use Illuminate\Support\Arr; | |
| 1162 | ||
| 1106 | 1163 | $array = [100, '200', 300, '400', 500]; |
| 1107 | ||
| 1164 | ||
| 1108 | 1165 | $filtered = Arr::where($array, function (string|int $value, int $key) { |
| 1109 | 1166 | return is_string($value); |
| 1110 | 1167 | }); |
| 1111 | ||
| 1168 | ||
| 1112 | 1169 | // [1 => '200', 3 => '400'] |
| 1113 | 1170 | |
| 1114 | 1171 | <a name="method-array-where-not-null"></a> |
| 1172 | ||
| 1115 | 1173 | #### `Arr::whereNotNull()` {.collection-method} |
| 1116 | 1174 | |
| 1117 | `Arr::whereNotNull` | |
| 1118 |
| |
| 1119 | use Illuminate\Support\Arr;
| |
| 1120 | ||
| 1175 | `Arr::whereNotNull` 方法将从给定数组中删除所有 `null` 值: | |
| 1176 | ||
| 1177 | use Illuminate\Support\Arr; | |
| 1178 | ||
| 1121 | 1179 | $array = [0, null]; |
| 1122 | ||
| 1180 | ||
| 1123 | 1181 | $filtered = Arr::whereNotNull($array); |
| 1124 | ||
| 1182 | ||
| 1125 | 1183 | // [0 => 0] |
| 1126 | 1184 | |
| 1127 | 1185 | <a name="method-array-wrap"></a> |
| 1186 | ||
| 1128 | 1187 | #### `Arr::wrap()` {.collection-method} |
| 1129 | 1188 | |
| 1130 | `Arr::wrap` | |
| 1131 |
| |
| 1132 | use Illuminate\Support\Arr;
| |
| 1133 | ||
| 1189 | `Arr::wrap` 方法可以将给定值转换为一个数组,如果给定的值已经是一个数组,它将原样返回: | |
| 1190 | ||
| 1191 | use Illuminate\Support\Arr; | |
| 1192 | ||
| 1134 | 1193 | $string = 'Laravel'; |
| 1135 | ||
| 1194 | ||
| 1136 | 1195 | $array = Arr::wrap($string); |
| 1137 | ||
| 1196 | ||
| 1138 | 1197 | // ['Laravel'] |
| 1139 | 1198 | |
| 1140 | 1199 | … | … |
| 1142 | 1201 | 如果给定值是 `null` ,将返回一个空数组: |
| 1143 | 1202 | |
| 1144 | 1203 | use Illuminate\Support\Arr; |
| 1145 | ||
| 1204 | ||
| 1146 | 1205 | $array = Arr::wrap(null); |
| 1147 | ||
| 1206 | ||
| 1148 | 1207 | // [] |
| 1149 | 1208 | |
| 1150 | 1209 | <a name="method-data-fill"></a> |
| 1210 | ||
| 1151 | 1211 | #### `data_fill()` {#collection-method} |
| 1152 | 1212 | |
| 1153 | 1213 | `data_fill` 函数使用「.」符号给多维数组或对象设置缺少的值: |
| 1154 | 1214 | |
| 1155 | 1215 | $data = ['products' => ['desk' => ['price' => 100]]]; |
| 1156 | ||
| 1216 | ||
| 1157 | 1217 | data_fill($data, 'products.desk.price', 200); |
| 1158 | ||
| 1218 | ||
| 1159 | 1219 | // ['products' => ['desk' => ['price' => 100]]] |
| 1160 | ||
| 1220 | ||
| 1161 | 1221 | data_fill($data, 'products.desk.discount', 10); |
| 1162 | ||
| 1222 | ||
| 1163 | 1223 | // ['products' => ['desk' => ['price' => 100, 'discount' => 10]]] |
| 1164 | 1224 | |
| 1165 | 1225 | 也可以接收 「\*」 作为通配符,设置相应缺少的值: | … | … |
| 1170 | 1230 | ['name' => 'Desk 2'], |
| 1171 | 1231 | ], |
| 1172 | 1232 | ]; |
| 1173 | ||
| 1233 | ||
| 1174 | 1234 | data_fill($data, 'products.*.price', 200); |
| 1175 | ||
| 1235 | ||
| 1176 | 1236 | /* |
| 1177 | 1237 | [ |
| 1178 | 1238 | 'products' => [ | … | … |
| 1183 | 1243 | */ |
| 1184 | 1244 | |
| 1185 | 1245 | <a name="method-data-get"></a> |
| 1246 | ||
| 1186 | 1247 | #### `data_get()` {#collection-method} |
| 1187 | 1248 | |
| 1188 | 1249 | `data_get` 函数使用 「.」 符号从多维数组或对象中根据指定键检索值 |
| 1189 | 1250 | |
| 1190 | 1251 | $data = ['products' => ['desk' => ['price' => 100]]]; |
| 1191 | ||
| 1252 | ||
| 1192 | 1253 | $price = data_get($data, 'products.desk.price'); |
| 1193 | ||
| 1254 | ||
| 1194 | 1255 | // 100 |
| 1195 | 1256 | |
| 1196 | 1257 | `data_get` 函数也接受一个默认值,如果没有找到指定的键,将返回默认值: |
| 1197 | 1258 | |
| 1198 | 1259 | $discount = data_get($data, 'products.desk.discount', 0); |
| 1199 | ||
| 1260 | ||
| 1200 | 1261 | // 0 |
| 1201 | 1262 | |
| 1202 | 1263 | 该函数还接受「\*」作为通配符,来指向数组或对象的任何键: | … | … |
| 1205 | 1266 | 'product-one' => ['name' => 'Desk 1', 'price' => 100], |
| 1206 | 1267 | 'product-two' => ['name' => 'Desk 2', 'price' => 150], |
| 1207 | 1268 | ]; |
| 1208 | ||
| 1269 | ||
| 1209 | 1270 | data_get($data, '*.name'); |
| 1210 | ||
| 1271 | ||
| 1211 | 1272 | // ['Desk 1', 'Desk 2']; |
| 1212 | 1273 | |
| 1213 | 1274 | <a name="method-data-set"></a> |
| 1275 | ||
| 1214 | 1276 | #### `data_set()` {#collection-method} |
| 1215 | 1277 | |
| 1216 | 1278 | `data_set` 函数使用「.」符号从多维数组或对象中根据指定键设置值: |
| 1217 | 1279 | |
| 1218 | 1280 | $data = ['products' => ['desk' => ['price' => 100]]]; |
| 1219 | ||
| 1281 | ||
| 1220 | 1282 | data_set($data, 'products.desk.price', 200); |
| 1221 | ||
| 1283 | ||
| 1222 | 1284 | // ['products' => ['desk' => ['price' => 200]]] |
| 1223 | 1285 | |
| 1224 | 1286 | 同 `data_get` 一样,函数也支持使用「\*」 作为通配符给相应键名赋值: | … | … |
| 1229 | 1291 | ['name' => 'Desk 2', 'price' => 150], |
| 1230 | 1292 | ], |
| 1231 | 1293 | ]; |
| 1232 | ||
| 1294 | ||
| 1233 | 1295 | data_set($data, 'products.*.price', 200); |
| 1234 | ||
| 1296 | ||
| 1235 | 1297 | /* |
| 1236 | 1298 | [ |
| 1237 | 1299 | 'products' => [ | … | … |
| 1246 | 1308 | 通常情况下,已存在的值将会被覆盖。如果只是希望设置一个目前不存在的值,你可以增加一个 `false` 作为函数的第四个参数: |
| 1247 | 1309 | |
| 1248 | 1310 | $data = ['products' => ['desk' => ['price' => 100]]]; |
| 1249 | ||
| 1311 | ||
| 1250 | 1312 | data_set($data, 'products.desk.price', 200, overwrite: false); |
| 1251 | ||
| 1313 | ||
| 1252 | 1314 | // ['products' => ['desk' => ['price' => 100]]] |
| 1253 | 1315 | |
| 1254 | 1316 | <a name="method-head"></a> |
| 1317 | ||
| 1255 | 1318 | #### `head()` {#collection-method} |
| 1256 | 1319 | |
| 1257 | 1320 | `head` 函数将返回数组中的第一个值: |
| 1258 | 1321 | |
| 1259 | 1322 | $array = [100, 200, 300]; |
| 1260 | ||
| 1323 | ||
| 1261 | 1324 | $first = head($array); |
| 1262 | ||
| 1325 | ||
| 1263 | 1326 | // 100 |
| 1264 | 1327 | |
| 1265 | 1328 | <a name="method-last"></a> |
| 1329 | ||
| 1266 | 1330 | #### `last()` {.collection-method} |
| 1267 | 1331 | |
| 1268 | 1332 | `last` 函数将返回数组中的最后一个值: |
| 1269 | 1333 | |
| 1270 | 1334 | $array = [100, 200, 300]; |
| 1271 | ||
| 1335 | ||
| 1272 | 1336 | $last = last($array); |
| 1273 | ||
| 1337 | ||
| 1274 | 1338 | // 300 |
| 1275 | 1339 | |
| 1276 | 1340 | <a name="paths"></a> |
| 1341 | ||
| 1277 | 1342 | ## 路径 |
| 1278 | 1343 | |
| 1279 | 1344 | <a name="method-app-path"></a> |
| 1345 | ||
| 1280 | 1346 | #### `app_path()` {.collection-method} |
| 1281 | 1347 | |
| 1282 | 1348 | `app_path` 函数返回 `app` 目录的完整路径。你也可以使用 `app_path` 函数来生成应用目录下特定文件的完整路径: |
| 1283 | 1349 | |
| 1284 | 1350 | $path = app_path(); |
| 1285 | ||
| 1351 | ||
| 1286 | 1352 | $path = app_path('Http/Controllers/Controller.php'); |
| 1287 | 1353 | |
| 1288 | 1354 | <a name="method-base-path"></a> |
| 1355 | ||
| 1289 | 1356 | #### `base_path()` {#collection-method} |
| 1290 | 1357 | |
| 1291 | 1358 | `base_path` 函数返回项目根目录的完整路径。你也可以使用 `base_path` 函数生成项目根目录下特定文件的完整路径: |
| 1292 | 1359 | |
| 1293 | 1360 | $path = base_path(); |
| 1294 | ||
| 1361 | ||
| 1295 | 1362 | $path = base_path('vendor/bin'); |
| 1296 | 1363 | |
| 1297 | 1364 | <a name="method-config-path"></a> |
| 1365 | ||
| 1298 | 1366 | #### `config_path()` {#collection-method} |
| 1299 | 1367 | |
| 1300 | 1368 | `config_path` 函数返回项目配置目录 (config) 的完整路径。你也可以使用 `config_path` 函数来生成应用配置目录中的特定文件的完整路径: |
| 1301 | 1369 | |
| 1302 | 1370 | $path = config_path(); |
| 1303 | ||
| 1371 | ||
| 1304 | 1372 | $path = config_path('app.php'); |
| 1305 | 1373 | |
| 1306 | 1374 | <a name="method-database-path"></a> | … | … |
| 1311 | 1379 | `database_path` 函数返回 `database` 目录的完整路径。你可以使用 `database_path` 函数来生成数据库目录下指定文件的完整路径: |
| 1312 | 1380 | |
| 1313 | 1381 | $path = database_path(); |
| 1314 | ||
| 1382 | ||
| 1315 | 1383 | $path = database_path('factories/UserFactory.php'); |
| 1316 | 1384 | |
| 1317 | 1385 | <a name="method-lang-path"></a> |
| 1386 | ||
| 1318 | 1387 | #### `lang_path()` {.collection-method} |
| 1319 | 1388 | |
| 1320 | 1389 | The `lang_path` 函数返回 `lang` 目录的完整路径。你可以使用 `lang_path` 函数来生成自定义语言目录下指定文件的完整路径: |
| 1321 | 1390 | |
| 1322 | 1391 | $path = lang_path(); |
| 1323 | ||
| 1392 | ||
| 1324 | 1393 | $path = lang_path('en/messages.php'); |
| 1325 | 1394 | |
| 1326 | 1395 | > **注意** |
| 1327 | 1396 | > 默认情况下,Laravel 框架不包含 `lang` 目录。如果你想自定义 Laravel 的语言文件,可以通过 Artisan 命令 `lang:publish` 来发布它们。 |
| 1328 | 1397 | |
| 1329 | 1398 | <a name="method-mix"></a> |
| 1399 | ||
| 1330 | 1400 | #### `mix()` {.collection-method} |
| 1331 | 1401 | |
| 1332 | 1402 | `mix` 函数返回 [编译前端资源(Mix)的路径](/docs/laravel/10.x/mix),便于加载 css,js 等静态文件: | … | … |
| 1334 | 1404 | $path = mix('css/app.css'); |
| 1335 | 1405 | |
| 1336 | 1406 | <a name="method-public-path"></a> |
| 1407 | ||
| 1337 | 1408 | #### `public_path()` {.collection-method} |
| 1338 | 1409 | |
| 1339 | 1410 | `public_path` 函数返回 `public` 目录的完整路径。你可以使用 `public_path` 函数来生成`public` 目录下指定文件的完整路径: |
| 1340 | 1411 | |
| 1341 | 1412 | $path = public_path(); |
| 1342 | ||
| 1413 | ||
| 1343 | 1414 | $path = public_path('css/app.css'); |
| 1344 | 1415 | |
| 1345 | 1416 | <a name="method-resource-path"></a> |
| 1417 | ||
| 1346 | 1418 | #### `resource_path()` {.collection-method} |
| 1347 | 1419 | |
| 1348 | 1420 | `resource_path` 函数返回 `resource` 目录的完整路径。你可以使用 `resource_path` 函数来生成位于资源路径中指定文件的完整路径: |
| 1349 | 1421 | |
| 1350 | 1422 | $path = resource_path(); |
| 1351 | ||
| 1423 | ||
| 1352 | 1424 | $path = resource_path('sass/app.scss'); |
| 1353 | 1425 | |
| 1354 | 1426 | |
| 1355 | 1427 | |
| 1356 | 1428 | <a name="method-storage-path"></a> |
| 1429 | ||
| 1357 | 1430 | #### `storage_path()` |
| 1358 | 1431 | |
| 1359 | `storage_path` | |
| 1432 | `storage_path` 函数返回 `storage` 目录的完整路径。 你也可以用 `storage_path` 函数来生成位于资源路径中的特定文件路径 | |
| 1360 | 1433 | |
| 1361 | 1434 | $path = storage_path(); |
| 1362 | ||
| 1435 | ||
| 1363 | 1436 | $path = storage_path('app/file.txt'); |
| 1364 | 1437 | |
| 1365 | 1438 | <a name="strings"></a> |
| 1439 | ||
| 1366 | 1440 | ## 字符串 |
| 1367 | 1441 | |
| 1368 | 1442 | <a name="method-__"></a> |
| 1443 | ||
| 1369 | 1444 | #### `__()` |
| 1370 | 1445 | |
| 1371 | 1446 | `__`函数可使用 [本地化文件](/docs/laravel/10.x/localization) 来翻译指定的字符串或特定的 key |
| 1372 | 1447 | |
| 1373 | 1448 | echo __('Welcome to our application'); |
| 1374 | ||
| 1449 | ||
| 1375 | 1450 | echo __('messages.welcome'); |
| 1376 | 1451 | |
| 1377 | 如果给定翻译的字符串或者 key 不存在, 则 | |
| 1452 | 如果给定翻译的字符串或者 key 不存在, 则 `__` 会返回你指定的值。所以上述例子中, 如果给定翻译的字符串或者 key 不存在,则 `__` 函数会返回 `messages.welcome`。 | |
| 1378 | 1453 | |
| 1379 | 1454 | <a name="method-class-basename"></a> |
| 1455 | ||
| 1380 | 1456 | #### `class_basename()` |
| 1381 | 1457 | |
| 1382 | 1458 | `class_basename` 函数返回不带命名空间的特定类的类名: |
| 1383 | 1459 | |
| 1384 | 1460 | $class = class_basename('Foo\Bar\Baz'); |
| 1385 | ||
| 1461 | ||
| 1386 | 1462 | // Baz |
| 1387 | 1463 | |
| 1388 | 1464 | <a name="method-e"></a> |
| 1465 | ||
| 1389 | 1466 | #### `e()` |
| 1390 | 1467 | |
| 1391 | `e` | |
| 1468 | `e` 函数运行 PHP 的 `htmlspecialchars` 函数,且 `double_encode` 默认设定为 `true`: | |
| 1392 | 1469 | |
| 1393 | 1470 | echo e('<html>foo</html>'); |
| 1394 | ||
| 1471 | ||
| 1395 | 1472 | // <html>foo</html> |
| 1396 | 1473 | |
| 1397 | 1474 | <a name="method-preg-replace-array"></a> |
| 1475 | ||
| 1398 | 1476 | #### `preg_replace_array()` {.collection-method} |
| 1399 | 1477 | |
| 1400 | `preg_replace_array` | |
| 1478 | `preg_replace_array` 函数按数组顺序替换字符串中符合给定模式的字符: | |
| 1401 | 1479 | |
| 1402 | 1480 | $string = 'The event will take place between :start and :end'; |
| 1403 | ||
| 1481 | ||
| 1404 | 1482 | $replaced = preg_replace_array('/:[a-z_]+/', ['8:30', '9:00'], $string); |
| 1405 | ||
| 1483 | ||
| 1406 | 1484 | // The event will take place between 8:30 and 9:00 |
| 1407 | 1485 | |
| 1408 | 1486 | <a name="method-str-after"></a> |
| 1487 | ||
| 1409 | 1488 | #### `Str::after()` |
| 1410 | 1489 | |
| 1411 | `Str::after` | |
| 1412 |
| |
| 1413 | use Illuminate\Support\Str;
| |
| 1414 | ||
| 1490 | `Str::after` 方法返回字符串中指定值之后的所有内容。如果字符串中不存在这个值,它将返回整个字符串: | |
| 1491 | ||
| 1492 | use Illuminate\Support\Str; | |
| 1493 | ||
| 1415 | 1494 | $slice = Str::after('This is my name', 'This is'); |
| 1416 | ||
| 1495 | ||
| 1417 | 1496 | // ' my name' |
| 1418 | 1497 | |
| 1419 | 1498 | |
| 1420 | 1499 | |
| 1421 | 1500 | <a name="method-str-after-last"></a> |
| 1501 | ||
| 1422 | 1502 | #### `Str::afterLast()` |
| 1423 | 1503 | |
| 1424 | `Str::afterLast` | |
| 1425 |
| |
| 1426 | use Illuminate\Support\Str;
| |
| 1427 | ||
| 1504 | `Str::afterLast` 方法返回字符串中指定值最后一次出现后的所有内容。如果字符串中不存在这个值,它将返回整个字符串: | |
| 1505 | ||
| 1506 | use Illuminate\Support\Str; | |
| 1507 | ||
| 1428 | 1508 | $slice = Str::afterLast('App\Http\Controllers\Controller', '\\'); |
| 1429 | ||
| 1509 | ||
| 1430 | 1510 | // 'Controller' |
| 1431 | 1511 | |
| 1432 | 1512 | <a name="method-str-ascii"></a> |
| 1513 | ||
| 1433 | 1514 | #### `Str::ascii()` |
| 1434 | 1515 | |
| 1435 | `Str::ascii` | |
| 1436 |
| |
| 1437 | use Illuminate\Support\Str;
| |
| 1438 | ||
| 1516 | `Str::ascii` 方法尝试将字符串转换为 ASCII 值: | |
| 1517 | ||
| 1518 | use Illuminate\Support\Str; | |
| 1519 | ||
| 1439 | 1520 | $slice = Str::ascii('û'); |
| 1440 | ||
| 1521 | ||
| 1441 | 1522 | // 'u' |
| 1442 | 1523 | |
| 1443 | 1524 | <a name="method-str-before"></a> |
| 1525 | ||
| 1444 | 1526 | #### `Str::before()` |
| 1445 | 1527 | |
| 1446 | `Str::before` | |
| 1447 |
| |
| 1448 | use Illuminate\Support\Str;
| |
| 1449 | ||
| 1528 | `Str::before` 方法返回字符串中指定值之前的所有内容: | |
| 1529 | ||
| 1530 | use Illuminate\Support\Str; | |
| 1531 | ||
| 1450 | 1532 | $slice = Str::before('This is my name', 'my name'); |
| 1451 | ||
| 1533 | ||
| 1452 | 1534 | // 'This is ' |
| 1453 | 1535 | |
| 1454 | 1536 | <a name="method-str-before-last"></a> |
| 1537 | ||
| 1455 | 1538 | #### `Str::beforeLast()` |
| 1456 | 1539 | |
| 1457 | `Str::beforeLast` | |
| 1458 |
| |
| 1459 | use Illuminate\Support\Str;
| |
| 1460 | ||
| 1540 | `Str::beforeLast` 方法返回字符串中指定值最后一次出现前的所有内容: | |
| 1541 | ||
| 1542 | use Illuminate\Support\Str; | |
| 1543 | ||
| 1461 | 1544 | $slice = Str::beforeLast('This is my name', 'is'); |
| 1462 | ||
| 1545 | ||
| 1463 | 1546 | // 'This ' |
| 1464 | 1547 | |
| 1465 | 1548 | <a name="method-str-between"></a> |
| 1549 | ||
| 1466 | 1550 | #### `Str::between()` |
| 1467 | 1551 | |
| 1468 | `Str::between` | |
| 1469 |
| |
| 1470 | use Illuminate\Support\Str;
| |
| 1471 | ||
| 1552 | `Str::between` 方法返回字符串在指定两个值之间的内容: | |
| 1553 | ||
| 1554 | use Illuminate\Support\Str; | |
| 1555 | ||
| 1472 | 1556 | $slice = Str::between('This is my name', 'This', 'name'); |
| 1473 | ||
| 1557 | ||
| 1474 | 1558 | // ' is my ' |
| 1475 | 1559 | |
| 1476 | 1560 | <a name="method-str-between-first"></a> |
| 1561 | ||
| 1477 | 1562 | #### `Str::betweenFirst()` |
| 1478 | 1563 | |
| 1479 | 1564 | `Str::betweenFirst` 方法返回字符串在指定两个值之间的最小可能的部分: |
| 1480 | 1565 | |
| 1481 | 1566 | use Illuminate\Support\Str; |
| 1482 | ||
| 1567 | ||
| 1483 | 1568 | $slice = Str::betweenFirst('[a] bc [d]', '[', ']'); |
| 1484 | ||
| 1569 | ||
| 1485 | 1570 | // 'a' |
| 1486 | 1571 | |
| 1487 | 1572 | <a name="method-camel-case"></a> |
| 1573 | ||
| 1488 | 1574 | #### `Str::camel()` |
| 1489 | 1575 | |
| 1490 | `Str::camel` | |
| 1491 |
| |
| 1492 | use Illuminate\Support\Str;
| |
| 1493 | ||
| 1576 | `Str::camel` 方法将指定字符串转换为 `驼峰式` 表示方法: | |
| 1577 | ||
| 1578 | use Illuminate\Support\Str; | |
| 1579 | ||
| 1494 | 1580 | $converted = Str::camel('foo_bar'); |
| 1495 | ||
| 1581 | ||
| 1496 | 1582 | // fooBar |
| 1497 | 1583 | |
| 1498 | 1584 | |
| 1499 | 1585 | |
| 1500 | 1586 | <a name="method-str-contains"></a> |
| 1587 | ||
| 1501 | 1588 | #### `Str::contains()` |
| 1502 | 1589 | |
| 1503 | `Str::contains` | |
| 1504 |
| |
| 1505 | use Illuminate\Support\Str;
| |
| 1506 | ||
| 1590 | `Str::contains` 方法判断指定字符串中是否包含另一指定字符串(区分大小写): | |
| 1591 | ||
| 1592 | use Illuminate\Support\Str; | |
| 1593 | ||
| 1507 | 1594 | $contains = Str::contains('This is my name', 'my'); |
| 1508 | ||
| 1595 | ||
| 1509 | 1596 | // true |
| 1510 | 1597 | |
| 1511 | 1598 | 你也可以传递一个数组来判断指定字符串是否包含数组中的任一值: |
| 1512 | 1599 | |
| 1513 | 1600 | use Illuminate\Support\Str; |
| 1514 | ||
| 1601 | ||
| 1515 | 1602 | $contains = Str::contains('This is my name', ['my', 'foo']); |
| 1516 | ||
| 1603 | ||
| 1517 | 1604 | // true |
| 1518 | 1605 | |
| 1519 | 1606 | <a name="method-str-contains-all"></a> |
| 1607 | ||
| 1520 | 1608 | #### `Str::containsAll()` |
| 1521 | 1609 | |
| 1522 | `Str::containsAll` | |
| 1523 |
| |
| 1524 | use Illuminate\Support\Str;
| |
| 1525 | ||
| 1610 | `Str::containsAll` 方法用于判断指定字符串是否包含指定数组中的所有值: | |
| 1611 | ||
| 1612 | use Illuminate\Support\Str; | |
| 1613 | ||
| 1526 | 1614 | $containsAll = Str::containsAll('This is my name', ['my', 'name']); |
| 1527 | ||
| 1615 | ||
| 1528 | 1616 | // true |
| 1529 | 1617 | |
| 1530 | 1618 | <a name="method-ends-with"></a> |
| 1619 | ||
| 1531 | 1620 | #### `Str::endsWith()` |
| 1532 | 1621 | |
| 1533 | `Str::endsWith` | |
| 1534 |
| |
| 1535 | use Illuminate\Support\Str;
| |
| 1536 | ||
| 1622 | `Str::endsWith` 方法用于判断指定字符串是否以另一指定字符串结尾: | |
| 1623 | ||
| 1624 | use Illuminate\Support\Str; | |
| 1625 | ||
| 1537 | 1626 | $result = Str::endsWith('This is my name', 'name'); |
| 1538 | ||
| 1627 | ||
| 1539 | 1628 | // true |
| 1540 | 1629 | |
| 1541 | 1630 | 你也可以传一个数组来判断指定字符串是否以指定数组中的任一值结尾: |
| 1542 | 1631 | |
| 1543 | 1632 | use Illuminate\Support\Str; |
| 1544 | ||
| 1633 | ||
| 1545 | 1634 | $result = Str::endsWith('This is my name', ['name', 'foo']); |
| 1546 | ||
| 1635 | ||
| 1547 | 1636 | // true |
| 1548 | ||
| 1637 | ||
| 1549 | 1638 | $result = Str::endsWith('This is my name', ['this', 'foo']); |
| 1550 | ||
| 1639 | ||
| 1551 | 1640 | // false |
| 1552 | 1641 | |
| 1553 | 1642 | <a name="method-excerpt"></a> |
| 1643 | ||
| 1554 | 1644 | #### `Str::excerpt()` |
| 1555 | 1645 | |
| 1556 | `Str::excerpt` | |
| 1557 |
| |
| 1558 | use Illuminate\Support\Str;
| |
| 1559 | ||
| 1646 | `Str::excerpt` 方法提取字符串中给定短语匹配到的第一个片段: | |
| 1647 | ||
| 1648 | use Illuminate\Support\Str; | |
| 1649 | ||
| 1560 | 1650 | $excerpt = Str::excerpt('This is my name', 'my', [ |
| 1561 | 1651 | 'radius' => 3 |
| 1562 | 1652 | ]); |
| 1563 | ||
| 1653 | ||
| 1564 | 1654 | // '...is my na...' |
| 1565 | 1655 | |
| 1566 | `radius` | |
| 1656 | `radius` 选项默认为 `100`,允许你定义应出现在截断字符串前后的字符数。 | |
| 1567 | 1657 | |
| 1568 | 1658 | |
| 1569 | 1659 | |
| 1570 | 1660 | 此外,你可以使用`omission`选项来定义将附加到截断字符串的字符串: |
| 1571 | 1661 | |
| 1572 | 1662 | use Illuminate\Support\Str; |
| 1573 | ||
| 1663 | ||
| 1574 | 1664 | $excerpt = Str::excerpt('This is my name', 'name', [ |
| 1575 | 1665 | 'radius' => 3, |
| 1576 | 1666 | 'omission' => '(...) ' |
| 1577 | 1667 | ]); |
| 1578 | ||
| 1668 | ||
| 1579 | 1669 | // '(...) my name' |
| 1580 | 1670 | |
| 1581 | 1671 | <a name="method-str-finish"></a> |
| 1672 | ||
| 1582 | 1673 | #### `Str::finish()` {.collection-method} |
| 1583 | 1674 | |
| 1584 | 1675 | `Str::finish`方法将指定的字符串修改为以指定的值结尾的形式: |
| 1585 | 1676 | |
| 1586 | 1677 | use Illuminate\Support\Str; |
| 1587 | ||
| 1678 | ||
| 1588 | 1679 | $adjusted = Str::finish('this/string', '/'); |
| 1589 | ||
| 1680 | ||
| 1590 | 1681 | // this/string/ |
| 1591 | ||
| 1682 | ||
| 1592 | 1683 | $adjusted = Str::finish('this/string/', '/'); |
| 1593 | ||
| 1684 | ||
| 1594 | 1685 | // this/string/ |
| 1595 | 1686 | |
| 1596 | 1687 | <a name="method-str-headline"></a> |
| 1688 | ||
| 1597 | 1689 | #### `Str::headline()` {.collection-method} |
| 1598 | 1690 | |
| 1599 | 1691 | `Str::headline`方法会将由大小写、连字符或下划线分隔的字符串转换为空格分隔的字符串,同时保证每个单词的首字母大写: |
| 1600 | 1692 | |
| 1601 | 1693 | use Illuminate\Support\Str; |
| 1602 | ||
| 1694 | ||
| 1603 | 1695 | $headline = Str::headline('steve_jobs'); |
| 1604 | ||
| 1696 | ||
| 1605 | 1697 | // Steve Jobs |
| 1606 | ||
| 1698 | ||
| 1607 | 1699 | $headline = Str::headline('邮件通知发送'); |
| 1608 | ||
| 1700 | ||
| 1609 | 1701 | // 邮件通知发送 |
| 1610 | 1702 | |
| 1611 | 1703 | <a name="method-str-inline-markdown"></a> |
| 1704 | ||
| 1612 | 1705 | #### `Str::inlineMarkdown()` {.collection-method} |
| 1613 | 1706 | |
| 1614 | 1707 | `Str::inlineMarkdown`方法使用[通用标记](https://commonmark.thephpleague.com/)将 GitHub 风味 Markdown 转换为内联 HTML。然而,与`markdown`方法不同的是,它不会将所有生成的 HTML 都包装在块级元素中: |
| 1615 | 1708 | |
| 1616 | 1709 | use Illuminate\Support\Str; |
| 1617 | ||
| 1710 | ||
| 1618 | 1711 | $html = Str::inlineMarkdown('**Laravel**'); |
| 1619 | ||
| 1712 | ||
| 1620 | 1713 | // <strong>Laravel</strong> |
| 1621 | 1714 | |
| 1622 | 1715 | <a name="method-str-is"></a> |
| 1716 | ||
| 1623 | 1717 | #### `Str::is()` {.collection-method} |
| 1624 | 1718 | |
| 1625 | 1719 | `Str::is`方法用来判断字符串是否与指定模式匹配。星号`*`可用于表示通配符: |
| 1626 | 1720 | |
| 1627 | 1721 | use Illuminate\Support\Str; |
| 1628 | ||
| 1722 | ||
| 1629 | 1723 | $matches = Str::is('foo*', 'foobar'); |
| 1630 | ||
| 1724 | ||
| 1631 | 1725 | // true |
| 1632 | ||
| 1726 | ||
| 1633 | 1727 | $matches = Str::is('baz*', 'foobar'); |
| 1634 | ||
| 1728 | ||
| 1635 | 1729 | // false |
| 1636 | 1730 | |
| 1637 | 1731 | <a name="method-str-is-ascii"></a> |
| 1732 | ||
| 1638 | 1733 | #### `Str::isAscii()` {.collection-method} |
| 1639 | 1734 | |
| 1640 | 1735 | `Str::isAscii`方法用于判断字符串是否是 7 位 ASCII: |
| 1641 | 1736 | |
| 1642 | 1737 | use Illuminate\Support\Str; |
| 1643 | ||
| 1738 | ||
| 1644 | 1739 | $isAscii = Str::isAscii('Taylor'); |
| 1645 | ||
| 1740 | ||
| 1646 | 1741 | // true |
| 1647 | ||
| 1742 | ||
| 1648 | 1743 | $isAscii = Str::isAscii('ü'); |
| 1649 | ||
| 1744 | ||
| 1650 | 1745 | // false |
| 1651 | 1746 | |
| 1652 | 1747 | |
| 1653 | 1748 | |
| 1654 | 1749 | <a name="method-str-is-json"></a> |
| 1750 | ||
| 1655 | 1751 | #### `Str::isJson()` {.collection-method} |
| 1656 | 1752 | |
| 1657 | 1753 | `Str::isJson`方法确定给定的字符串是否是有效的 JSON: |
| 1658 | 1754 | |
| 1659 | 1755 | use Illuminate\Support\Str; |
| 1660 | ||
| 1756 | ||
| 1661 | 1757 | $result = Str::isJson('[1,2,3]'); |
| 1662 | ||
| 1758 | ||
| 1663 | 1759 | // true |
| 1664 | ||
| 1760 | ||
| 1665 | 1761 | $result = Str::isJson('{"first": "John", "last": "Doe"}'); |
| 1666 | ||
| 1762 | ||
| 1667 | 1763 | // true |
| 1668 | ||
| 1764 | ||
| 1669 | 1765 | $result = Str::isJson('{first: "John", last: "Doe"}'); |
| 1670 | ||
| 1766 | ||
| 1671 | 1767 | // false |
| 1672 | 1768 | |
| 1673 | 1769 | <a name="method-str-is-ulid"></a> |
| 1770 | ||
| 1674 | 1771 | #### `Str::isUlid()` {.collection-method} |
| 1675 | 1772 | |
| 1676 | 1773 | `Str::isUlid`方法用于判断指定字符串是否是有效的 ULID: |
| 1677 | 1774 | |
| 1678 | 1775 | use Illuminate\Support\Str; |
| 1679 | ||
| 1776 | ||
| 1680 | 1777 | $isUlid = Str::isUlid('01gd6r360bp37zj17nxb55yv40'); |
| 1681 | ||
| 1778 | ||
| 1682 | 1779 | // true |
| 1683 | ||
| 1780 | ||
| 1684 | 1781 | $isUlid = Str::isUlid('laravel'); |
| 1685 | ||
| 1782 | ||
| 1686 | 1783 | // false |
| 1687 | 1784 | |
| 1688 | 1785 | <a name="method-str-is-uuid"></a> |
| 1786 | ||
| 1689 | 1787 | #### `Str::isUuid()` {.collection-method} |
| 1690 | 1788 | |
| 1691 | 1789 | `Str::isUuid`方法用于判断指定字符串是否是有效的 UUID: |
| 1692 | 1790 | |
| 1693 | 1791 | use Illuminate\Support\Str; |
| 1694 | ||
| 1792 | ||
| 1695 | 1793 | $isUuid = Str::isUuid('a0a2a2d2-0b87-4a18-83f2-2529882be2de'); |
| 1696 | ||
| 1794 | ||
| 1697 | 1795 | // true |
| 1698 | ||
| 1796 | ||
| 1699 | 1797 | $isUuid = Str::isUuid('laravel'); |
| 1700 | ||
| 1798 | ||
| 1701 | 1799 | // false |
| 1702 | 1800 | |
| 1703 | 1801 | <a name="method-kebab-case"></a> |
| 1802 | ||
| 1704 | 1803 | #### `Str::kebab()` {.collection-method} |
| 1705 | 1804 | |
| 1706 | 1805 | `Str::kebab`方法将字符串转换为`烤串式( kebab-case )`表示方法: |
| 1707 | 1806 | |
| 1708 | 1807 | use Illuminate\Support\Str; |
| 1709 | ||
| 1808 | ||
| 1710 | 1809 | $converted = Str::kebab('fooBar'); |
| 1711 | ||
| 1810 | ||
| 1712 | 1811 | // foo-bar |
| 1713 | 1812 | |
| 1714 | 1813 | <a name="method-str-lcfirst"></a> |
| 1814 | ||
| 1715 | 1815 | #### `Str::lcfirst()` {.collection-method} |
| 1716 | 1816 | |
| 1717 | 1817 | `Str::lcfirst`方法返回第一个小写字符的给定字符串: |
| 1718 | 1818 | |
| 1719 | 1819 | use Illuminate\Support\Str; |
| 1720 | ||
| 1820 | ||
| 1721 | 1821 | $string = Str::lcfirst('Foo Bar'); |
| 1722 | ||
| 1822 | ||
| 1723 | 1823 | // foo Bar |
| 1724 | 1824 | |
| 1725 | 1825 | <a name="method-str-length"></a> |
| 1826 | ||
| 1726 | 1827 | #### `Str::length()` {.collection-method} |
| 1727 | 1828 | |
| 1728 | 1829 | `Str::length`方法返回指定字符串的长度: |
| 1729 | 1830 | |
| 1730 | 1831 | use Illuminate\Support\Str; |
| 1731 | ||
| 1832 | ||
| 1732 | 1833 | $length = Str::length('Laravel'); |
| 1733 | ||
| 1834 | ||
| 1734 | 1835 | // 7 |
| 1735 | 1836 | |
| 1736 | 1837 | <a name="method-str-limit"></a> |
| 1838 | ||
| 1737 | 1839 | #### `Str::limit()` {.collection-method} |
| 1738 | 1840 | |
| 1739 | 1841 | `Str::limit`方法将字符串以指定长度进行截断: |
| 1740 | 1842 | |
| 1741 | 1843 | use Illuminate\Support\Str; |
| 1742 | ||
| 1844 | ||
| 1743 | 1845 | $truncated = Str::limit('敏捷的棕色狐狸跳过懒惰的狗', 20); |
| 1744 | ||
| 1846 | ||
| 1745 | 1847 | // 敏捷的棕色狐狸... |
| 1746 | 1848 | |
| 1747 | 1849 | … | … |
| 1749 | 1851 | 你也可通过第三个参数来改变追加到末尾的字符串: |
| 1750 | 1852 | |
| 1751 | 1853 | use Illuminate\Support\Str; |
| 1752 | ||
| 1854 | ||
| 1753 | 1855 | $truncated = Str::limit('敏捷的棕色狐狸跳过懒惰的狗', 20, ' (...)'); |
| 1754 | ||
| 1856 | ||
| 1755 | 1857 | // 敏捷的棕色狐狸 (...) |
| 1756 | 1858 | |
| 1757 | 1859 | <a name="method-str-lower"></a> |
| 1860 | ||
| 1758 | 1861 | #### `Str::lower()` {.collection-method} |
| 1759 | 1862 | |
| 1760 | 1863 | `Str::lower`方法用于将字符串转换为小写: |
| 1761 | 1864 | |
| 1762 | 1865 | use Illuminate\Support\Str; |
| 1763 | ||
| 1866 | ||
| 1764 | 1867 | $converted = Str::lower('LARAVEL'); |
| 1765 | ||
| 1868 | ||
| 1766 | 1869 | // laravel |
| 1767 | 1870 | |
| 1768 | 1871 | <a name="method-str-markdown"></a> |
| 1872 | ||
| 1769 | 1873 | #### `Str::markdown()` {.collection-method} |
| 1770 | 1874 | |
| 1771 | 1875 | `Str::markdown`方法将 GitHub 风格的 Markdown 转换为 HTML 使用[通用标记](https://commonmark.thephpleague.com/): |
| 1772 | 1876 | |
| 1773 | 1877 | use Illuminate\Support\Str; |
| 1774 | ||
| 1878 | ||
| 1775 | 1879 | $html = Str::markdown('# Laravel'); |
| 1776 | ||
| 1880 | ||
| 1777 | 1881 | // <h1>Laravel</h1> |
| 1778 | ||
| 1882 | ||
| 1779 | 1883 | $html = Str::markdown('# Taylor <b>Otwell</b>', [ |
| 1780 | 1884 | 'html_input' => 'strip', |
| 1781 | 1885 | ]); |
| 1782 | ||
| 1886 | ||
| 1783 | 1887 | // <h1>Taylor Otwell</h1> |
| 1784 | 1888 | |
| 1785 | 1889 | <a name="method-str-mask"></a> |
| 1890 | ||
| 1786 | 1891 | #### `Str::mask()` {.collection-method} |
| 1787 | 1892 | |
| 1788 | 1893 | `Str::mask`方法会使用重复的字符掩盖字符串的一部分,并可用于混淆字符串段,例如电子邮件地址和电话号码: |
| 1789 | 1894 | |
| 1790 | 1895 | use Illuminate\Support\Str; |
| 1791 | ||
| 1896 | ||
| 1792 | 1897 | $string = Str::mask('taylor@example.com', '*', 3); |
| 1793 | ||
| 1898 | ||
| 1794 | 1899 | // tay*************** |
| 1795 | 1900 | |
| 1796 | 1901 | 你可以提供一个负数作为`mask`方法的第三个参数,这将指示该方法在距字符串末尾的给定距离处开始屏蔽: |
| 1797 | 1902 | |
| 1798 | 1903 | $string = Str::mask('taylor@example.com', '*', -15, 3); |
| 1799 | ||
| 1904 | ||
| 1800 | 1905 | // tay***@example.com |
| 1801 | 1906 | |
| 1802 | 1907 | <a name="method-str-ordered-uuid"></a> |
| 1908 | ||
| 1803 | 1909 | #### `Str::orderedUuid()` {.collection-method} |
| 1804 | 1910 | |
| 1805 | 1911 | `Str::orderedUuid`方法用于生成一个「时间戳优先」的 UUID ,它可作为数据库索引列的有效值。使用此方法生成的每个 UUID 将排在之前使用该方法生成的 UUID 后面: |
| 1806 | 1912 | |
| 1807 | 1913 | use Illuminate\Support\Str; |
| 1808 | ||
| 1914 | ||
| 1809 | 1915 | return (string) Str::orderedUuid(); |
| 1810 | 1916 | |
| 1811 | 1917 | |
| 1812 | 1918 | |
| 1813 | 1919 | <a name="method-str-padboth"></a> |
| 1920 | ||
| 1814 | 1921 | #### `Str::padBoth()` {.collection-method} |
| 1815 | 1922 | |
| 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 | ||
| 1820 | 1927 | $padded = Str::padBoth('James', 10, '_'); |
| 1821 | ||
| 1928 | ||
| 1822 | 1929 | // '__James___' |
| 1823 | ||
| 1930 | ||
| 1824 | 1931 | $padded = Str::padBoth('James', 10); |
| 1825 | ||
| 1932 | ||
| 1826 | 1933 | // ' James ' |
| 1827 | 1934 | |
| 1828 | 1935 | <a name="method-str-padleft"></a> |
| 1936 | ||
| 1829 | 1937 | #### `Str::padLeft()` {.collection-method} |
| 1830 | 1938 | |
| 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 | ||
| 1835 | 1943 | $padded = Str::padLeft('James', 10, '-='); |
| 1836 | ||
| 1944 | ||
| 1837 | 1945 | // '-=-=-James' |
| 1838 | ||
| 1946 | ||
| 1839 | 1947 | $padded = Str::padLeft('James', 10); |
| 1840 | ||
| 1948 | ||
| 1841 | 1949 | // ' James' |
| 1842 | 1950 | |
| 1843 | 1951 | <a name="method-str-padright"></a> |
| 1952 | ||
| 1844 | 1953 | #### `Str::padRight()` {.collection-method} |
| 1845 | 1954 | |
| 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 | ||
| 1850 | 1959 | $padded = Str::padRight('James', 10, '-'); |
| 1851 | ||
| 1960 | ||
| 1852 | 1961 | // 'James-----' |
| 1853 | ||
| 1962 | ||
| 1854 | 1963 | $padded = Str::padRight('James', 10); |
| 1855 | ||
| 1964 | ||
| 1856 | 1965 | // 'James ' |
| 1857 | 1966 | |
| 1858 | 1967 | <a name="method-str-password"></a> |
| 1968 | ||
| 1859 | 1969 | #### `Str::password()` {.collection-method} |
| 1860 | 1970 | |
| 1861 | 1971 | `Str::password`方法可用于生成给定长度的安全随机密码。密码由字母、数字、符号和空格组成。默认情况下,密码长度为32位: |
| 1862 | 1972 | |
| 1863 | 1973 | use Illuminate\Support\Str; |
| 1864 | ||
| 1974 | ||
| 1865 | 1975 | $password = Str::password(); |
| 1866 | ||
| 1976 | ||
| 1867 | 1977 | // 'EbJo2vE-AS:U,$%_gkrV4n,q~1xy/-_4' |
| 1868 | ||
| 1978 | ||
| 1869 | 1979 | $password = Str::password(12); |
| 1870 | ||
| 1980 | ||
| 1871 | 1981 | // 'qwuar>#V|i]N' |
| 1872 | 1982 | |
| 1873 | 1983 | <a name="method-str-plural"></a> |
| 1984 | ||
| 1874 | 1985 | #### `Str::plural()` {.collection-method} |
| 1875 | 1986 | |
| 1876 | `Str::plural`方法将单数形式的字符串转换为复数形式。此 | |
| 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 | ||
| 1880 | 1991 | $plural = Str::plural('car'); |
| 1881 | ||
| 1992 | ||
| 1882 | 1993 | // cars |
| 1883 | ||
| 1994 | ||
| 1884 | 1995 | $plural = Str::plural('child'); |
| 1885 | ||
| 1996 | ||
| 1886 | 1997 | // children |
| 1887 | 1998 | |
| 1888 | 1999 | |
| 1889 | 2000 | |
| 1890 | 你可以提供一个整数作为 | |
| 1891 |
| |
| 1892 | use Illuminate\Support\Str;
| |
| 1893 | ||
| 2001 | 你可以提供一个整数作为方法的第二个参数来检索字符串的单数或复数形式: | |
| 2002 | ||
| 2003 | use Illuminate\Support\Str; | |
| 2004 | ||
| 1894 | 2005 | $plural = Str::plural('child', 2); |
| 1895 | ||
| 2006 | ||
| 1896 | 2007 | // children |
| 1897 | ||
| 2008 | ||
| 1898 | 2009 | $singular = Str::plural('child', 1); |
| 1899 | ||
| 2010 | ||
| 1900 | 2011 | // child |
| 1901 | 2012 | |
| 1902 | 2013 | <a name="method-str-plural-studly"></a> |
| 2014 | ||
| 1903 | 2015 | #### `Str::pluralStudly()` {.collection-method} |
| 1904 | 2016 | |
| 1905 | `Str::pluralStudly`方法将以驼峰格式的单数字符串转化为其复数形式。此 | |
| 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 | ||
| 1909 | 2021 | $plural = Str::pluralStudly('VerifiedHuman'); |
| 1910 | ||
| 2022 | ||
| 1911 | 2023 | // VerifiedHumans |
| 1912 | ||
| 2024 | ||
| 1913 | 2025 | $plural = Str::pluralStudly('UserFeedback'); |
| 1914 | ||
| 2026 | ||
| 1915 | 2027 | // UserFeedback |
| 1916 | 2028 | |
| 1917 | 你可以提供一个整数作为 | |
| 1918 |
| |
| 1919 | use Illuminate\Support\Str;
| |
| 1920 | ||
| 2029 | 你可以提供一个整数作为方法的第二个参数来检索字符串的单数或复数形式: | |
| 2030 | ||
| 2031 | use Illuminate\Support\Str; | |
| 2032 | ||
| 1921 | 2033 | $plural = Str::pluralStudly('VerifiedHuman', 2); |
| 1922 | ||
| 2034 | ||
| 1923 | 2035 | // VerifiedHumans |
| 1924 | ||
| 2036 | ||
| 1925 | 2037 | $singular = Str::pluralStudly('VerifiedHuman', 1); |
| 1926 | ||
| 2038 | ||
| 1927 | 2039 | // VerifiedHuman |
| 1928 | 2040 | |
| 1929 | 2041 | <a name="method-str-random"></a> |
| 2042 | ||
| 1930 | 2043 | #### `Str::random()` {.collection-method} |
| 1931 | 2044 | |
| 1932 | `Str::random` 方法用于生成指定长度的随机字符串。这个 | |
| 1933 |
| |
| 1934 | use Illuminate\Support\Str;
| |
| 1935 | ||
| 2045 | `Str::random` 方法用于生成指定长度的随机字符串。这个方法使用了PHP的 `random_bytes` 函数: | |
| 2046 | ||
| 2047 | use Illuminate\Support\Str; | |
| 2048 | ||
| 1936 | 2049 | $random = Str::random(40); |
| 1937 | 2050 | |
| 1938 | 2051 | <a name="method-str-remove"></a> |
| 2052 | ||
| 1939 | 2053 | #### `Str::remove()` {.collection-method} |
| 1940 | 2054 | |
| 1941 | 2055 | `Str::remove` 方法从字符串中删除给定值或给定数组内的所有值: |
| 1942 | 2056 | |
| 1943 | 2057 | use Illuminate\Support\Str; |
| 1944 | ||
| 2058 | ||
| 1945 | 2059 | $string = 'Peter Piper picked a peck of pickled peppers.'; |
| 1946 | ||
| 2060 | ||
| 1947 | 2061 | $removed = Str::remove('e', $string); |
| 1948 | ||
| 2062 | ||
| 1949 | 2063 | // Ptr Pipr pickd a pck of pickld ppprs. |
| 1950 | 2064 | |
| 1951 | 2065 | 你还可以将`false`作为第三个参数传递给`remove`方法以在删除字符串时忽略大小写。 |
| 1952 | 2066 | |
| 1953 | 2067 | <a name="method-str-replace"></a> |
| 2068 | ||
| 1954 | 2069 | #### `Str::replace()` {.collection-method} |
| 1955 | 2070 | |
| 1956 | 2071 | `Str::replace` 方法用于替换字符串中的给定字符串: |
| 1957 | 2072 | |
| 1958 | 2073 | use Illuminate\Support\Str; |
| 1959 | ||
| 2074 | ||
| 1960 | 2075 | $string = 'Laravel 8.x'; |
| 1961 | ||
| 2076 | ||
| 1962 | 2077 | $replaced = Str::replace('8.x', '9.x', $string); |
| 1963 | ||
| 2078 | ||
| 1964 | 2079 | // Laravel 9.x |
| 1965 | 2080 | |
| 1966 | 2081 | |
| 1967 | 2082 | |
| 1968 | 2083 | <a name="method-str-replace-array"></a> |
| 2084 | ||
| 1969 | 2085 | #### `Str::replaceArray()` {.collection-method} |
| 1970 | 2086 | |
| 1971 | `Str::replaceArray` | |
| 1972 |
| |
| 1973 | use Illuminate\Support\Str;
| |
| 1974 | ||
| 2087 | `Str::replaceArray` 方法使用数组有序的替换字符串中的特定字符: | |
| 2088 | ||
| 2089 | use Illuminate\Support\Str; | |
| 2090 | ||
| 1975 | 2091 | $string = '该活动将在 ? 至 ? 举行'; |
| 1976 | ||
| 2092 | ||
| 1977 | 2093 | $replaced = Str::replaceArray('?', ['8:30', '9:00'], $string); |
| 1978 | ||
| 2094 | ||
| 1979 | 2095 | // 该活动将在 8:30 至 9:00 举行 |
| 1980 | 2096 | |
| 1981 | 2097 | <a name="method-str-replace-first"></a> |
| 2098 | ||
| 1982 | 2099 | #### `Str::replaceFirst()` {.collection-method} |
| 1983 | 2100 | |
| 1984 | `Str::replaceFirst` | |
| 1985 |
| |
| 1986 | use Illuminate\Support\Str;
| |
| 1987 | ||
| 2101 | `Str::replaceFirst` 方法替换字符串中给定值的第一个匹配项: | |
| 2102 | ||
| 2103 | use Illuminate\Support\Str; | |
| 2104 | ||
| 1988 | 2105 | $replaced = Str::replaceFirst('the', 'a', 'the quick brown fox jumps over the lazy dog'); |
| 1989 | ||
| 2106 | ||
| 1990 | 2107 | // a quick brown fox jumps over the lazy dog |
| 1991 | 2108 | |
| 1992 | 2109 | <a name="method-str-replace-last"></a> |
| 2110 | ||
| 1993 | 2111 | #### `Str::replaceLast()` {.collection-method} |
| 1994 | 2112 | |
| 1995 | `Str::replaceLast` | |
| 1996 |
| |
| 1997 | use Illuminate\Support\Str;
| |
| 1998 | ||
| 2113 | `Str::replaceLast` 方法替换字符串中最后一次出现的给定值: | |
| 2114 | ||
| 2115 | use Illuminate\Support\Str; | |
| 2116 | ||
| 1999 | 2117 | $replaced = Str::replaceLast('the', 'a', 'the quick brown fox jumps over the lazy dog'); |
| 2000 | ||
| 2118 | ||
| 2001 | 2119 | // the quick brown fox jumps over a lazy dog |
| 2002 | 2120 | |
| 2003 | 2121 | |
| 2004 | 2122 | <a name="method-str-reverse"></a> |
| 2123 | ||
| 2005 | 2124 | #### `Str::reverse()` {.collection-method} |
| 2006 | 2125 | |
| 2007 | `Str::reverse` | |
| 2008 |
| |
| 2009 | use Illuminate\Support\Str;
| |
| 2010 | ||
| 2126 | `Str::reverse` 方法用于反转给定的字符串: | |
| 2127 | ||
| 2128 | use Illuminate\Support\Str; | |
| 2129 | ||
| 2011 | 2130 | $reversed = Str::reverse('Hello World'); |
| 2012 | ||
| 2131 | ||
| 2013 | 2132 | // dlroW olleH |
| 2014 | 2133 | |
| 2015 | 2134 | <a name="method-str-singular"></a> |
| 2135 | ||
| 2016 | 2136 | #### `Str::singular()` {.collection-method} |
| 2017 | 2137 | |
| 2018 | `Str::singular` | |
| 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 | ||
| 2022 | 2142 | $singular = Str::singular('cars'); |
| 2023 | ||
| 2143 | ||
| 2024 | 2144 | // car |
| 2025 | ||
| 2145 | ||
| 2026 | 2146 | $singular = Str::singular('children'); |
| 2027 | ||
| 2147 | ||
| 2028 | 2148 | // child |
| 2029 | 2149 | |
| 2030 | 2150 | <a name="method-str-slug"></a> |
| 2151 | ||
| 2031 | 2152 | #### `Str::slug()` {.collection-method} |
| 2032 | 2153 | |
| 2033 | `Str::slug` | |
| 2034 |
| |
| 2035 | use Illuminate\Support\Str;
| |
| 2036 | ||
| 2154 | `Str::slug` 方法将给定的字符串生成一个 URL 友好的「slug」: | |
| 2155 | ||
| 2156 | use Illuminate\Support\Str; | |
| 2157 | ||
| 2037 | 2158 | $slug = Str::slug('Laravel 5 Framework', '-'); |
| 2038 | ||
| 2159 | ||
| 2039 | 2160 | // laravel-5-framework |
| 2040 | 2161 | |
| 2041 | 2162 | |
| 2042 | 2163 | |
| 2043 | 2164 | <a name="method-snake-case"></a> |
| 2165 | ||
| 2044 | 2166 | #### `Str::snake()` {.collection-method} |
| 2045 | 2167 | |
| 2046 | `Str::snake` | |
| 2047 |
| |
| 2048 | use Illuminate\Support\Str;
| |
| 2049 | ||
| 2168 | `Str::snake` 方法是将驼峰的函数名或者字符串转换成 `_` 命名的函数或者字符串,例如 `snakeCase` 转换成 `snake_case`: | |
| 2169 | ||
| 2170 | use Illuminate\Support\Str; | |
| 2171 | ||
| 2050 | 2172 | $converted = Str::snake('fooBar'); |
| 2051 | ||
| 2173 | ||
| 2052 | 2174 | // foo_bar |
| 2053 | ||
| 2175 | ||
| 2054 | 2176 | $converted = Str::snake('fooBar', '-'); |
| 2055 | ||
| 2177 | ||
| 2056 | 2178 | // foo-bar |
| 2057 | 2179 | |
| 2058 | 2180 | <a name="method-str-squish"></a> |
| 2181 | ||
| 2059 | 2182 | #### `Str::squish()` {.collection-method} |
| 2060 | 2183 | |
| 2061 | 2184 | `Str::squish`方法删除字符串中所有多余的空白,包括单词之间多余的空白: |
| 2062 | 2185 | |
| 2063 | 2186 | use Illuminate\Support\Str; |
| 2064 | ||
| 2187 | ||
| 2065 | 2188 | $string = Str::squish(' laravel framework '); |
| 2066 | ||
| 2189 | ||
| 2067 | 2190 | // laravel framework |
| 2068 | 2191 | |
| 2069 | 2192 | <a name="method-str-start"></a> |
| 2193 | ||
| 2070 | 2194 | #### `Str::start()` {.collection-method} |
| 2071 | 2195 | |
| 2072 | 2196 | `Str::start`方法是将给定的值添加到字符串的开始位置,例如: |
| 2073 | 2197 | |
| 2074 | 2198 | use Illuminate\Support\Str; |
| 2075 | ||
| 2199 | ||
| 2076 | 2200 | $adjusted = Str::start('this/string', '/'); |
| 2077 | ||
| 2201 | ||
| 2078 | 2202 | // /this/string |
| 2079 | ||
| 2203 | ||
| 2080 | 2204 | $adjusted = Str::start('/this/string', '/'); |
| 2081 | ||
| 2205 | ||
| 2082 | 2206 | // /this/string |
| 2083 | 2207 | |
| 2084 | 2208 | <a name="method-starts-with"></a> |
| 2209 | ||
| 2085 | 2210 | #### `Str::startsWith()` {.collection-method} |
| 2086 | 2211 | |
| 2087 | 2212 | `Str::startsWith`方法用来判断给定的字符串是否为给定值的开头: |
| 2088 | 2213 | |
| 2089 | 2214 | use Illuminate\Support\Str; |
| 2090 | ||
| 2215 | ||
| 2091 | 2216 | $result = Str::startsWith('This is my name', 'This'); |
| 2092 | ||
| 2217 | ||
| 2093 | 2218 | // true |
| 2094 | 2219 | |
| 2095 | 2220 | 如果传递了一个可能值的数组且字符串以任何给定值开头,则`startsWith`方法将返回`true`: |
| 2096 | 2221 | |
| 2097 | 2222 | $result = Str::startsWith('This is my name', ['This', 'That', 'There']); |
| 2098 | ||
| 2223 | ||
| 2099 | 2224 | // true |
| 2100 | 2225 | |
| 2101 | 2226 | <a name="method-studly-case"></a> |
| 2227 | ||
| 2102 | 2228 | #### `Str::studly()` {.collection-method} |
| 2103 | 2229 | |
| 2104 | 2230 | `Str::studly`方法将给定的字符串转换为`驼峰命名`的字符串: |
| 2105 | 2231 | |
| 2106 | 2232 | use Illuminate\Support\Str; |
| 2107 | ||
| 2233 | ||
| 2108 | 2234 | $converted = Str::studly('foo_bar'); |
| 2109 | ||
| 2235 | ||
| 2110 | 2236 | // FooBar |
| 2111 | 2237 | |
| 2112 | 2238 | <a name="method-str-substr"></a> |
| 2239 | ||
| 2113 | 2240 | #### `Str::substr()` {.collection-method} |
| 2114 | 2241 | |
| 2115 | 2242 | `Str::substr`方法返回由 start 和 length 参数指定的字符串部分: |
| 2116 | 2243 | |
| 2117 | 2244 | use Illuminate\Support\Str; |
| 2118 | ||
| 2245 | ||
| 2119 | 2246 | $converted = Str::substr('The Laravel Framework', 4, 7); |
| 2120 | ||
| 2247 | ||
| 2121 | 2248 | // Laravel |
| 2122 | 2249 | |
| 2123 | 2250 | |
| 2124 | 2251 | |
| 2125 | 2252 | <a name="method-str-substrcount"></a> |
| 2253 | ||
| 2126 | 2254 | #### `Str::substrCount()` {.collection-method} |
| 2127 | 2255 | |
| 2128 | 2256 | `Str::substrCount` 方法返回给定字符串中给定值的出现次数: |
| 2129 | 2257 | |
| 2130 | 2258 | use Illuminate\Support\Str; |
| 2131 | ||
| 2259 | ||
| 2132 | 2260 | $count = Str::substrCount('If you like ice cream, you will like snow cones.', 'like'); |
| 2133 | ||
| 2261 | ||
| 2134 | 2262 | // 2 |
| 2135 | 2263 | |
| 2136 | 2264 | <a name="method-str-substrreplace"></a> |
| 2265 | ||
| 2137 | 2266 | #### `Str::substrReplace()` {.collection-method} |
| 2138 | 2267 | |
| 2139 | 2268 | `Str::substrReplace` 方法替换字符串一部分中的文本,从第三个参数指定的位置开始,替换第四个参数指定的字符数。 当「0」传递给方法的第四个参数将在指定位置插入字符串,而不是替换字符串中的任何现有字符: |
| 2140 | 2269 | |
| 2141 | 2270 | use Illuminate\Support\Str; |
| 2142 | ||
| 2271 | ||
| 2143 | 2272 | $result = Str::substrReplace('1300', ':', 2); |
| 2144 | 2273 | // 13: |
| 2145 | ||
| 2274 | ||
| 2146 | 2275 | $result = Str::substrReplace('1300', ':', 2, 0); |
| 2147 | 2276 | // 13:00 |
| 2148 | 2277 | |
| 2149 | 2278 | <a name="method-str-swap"></a> |
| 2279 | ||
| 2150 | 2280 | #### `Str::swap()` {.collection-method} |
| 2151 | 2281 | |
| 2152 | 2282 | `Str::swap` 方法使用 PHP 的 `strtr` 函数替换给定字符串中的多个值: |
| 2153 | 2283 | |
| 2154 | 2284 | use Illuminate\Support\Str; |
| 2155 | ||
| 2285 | ||
| 2156 | 2286 | $string = Str::swap([ |
| 2157 | 2287 | 'Tacos' => 'Burritos', |
| 2158 | 2288 | 'great' => 'fantastic', |
| 2159 | 2289 | ], 'Tacos are great!'); |
| 2160 | ||
| 2290 | ||
| 2161 | 2291 | // Burritos are fantastic! |
| 2162 | 2292 | |
| 2163 | 2293 | <a name="method-title-case"></a> |
| 2294 | ||
| 2164 | 2295 | #### `Str::title()` {.collection-method} |
| 2165 | 2296 | |
| 2166 | 2297 | `Str::title` 方法将给定的字符串转换为 `Title Case`: |
| 2167 | 2298 | |
| 2168 | 2299 | use Illuminate\Support\Str; |
| 2169 | ||
| 2300 | ||
| 2170 | 2301 | $converted = Str::title('a nice title uses the correct case'); |
| 2171 | ||
| 2302 | ||
| 2172 | 2303 | // A Nice Title Uses The Correct Case |
| 2173 | 2304 | |
| 2174 | 2305 | <a name="method-str-to-html-string"></a> |
| 2306 | ||
| 2175 | 2307 | #### `Str::toHtmlString()` {.collection-method} |
| 2176 | 2308 | |
| 2177 | 2309 | `Str::toHtmlString` 方法将字符串实例转换为 `Illuminate\Support\HtmlString` 的实例,它可以显示在 Blade 模板中: |
| 2178 | 2310 | |
| 2179 | 2311 | use Illuminate\Support\Str; |
| 2180 | ||
| 2312 | ||
| 2181 | 2313 | $htmlString = Str::of('Nuno Maduro')->toHtmlString(); |
| 2182 | 2314 | |
| 2183 | 2315 | <a name="method-str-ucfirst"></a> |
| 2316 | ||
| 2184 | 2317 | #### `Str::ucfirst()` {.collection-method} |
| 2185 | 2318 | |
| 2186 | 2319 | `Str::ucfirst` 方法返回第一个字符大写的给定字符串: |
| 2187 | 2320 | |
| 2188 | 2321 | use Illuminate\Support\Str; |
| 2189 | ||
| 2322 | ||
| 2190 | 2323 | $string = Str::ucfirst('foo bar'); |
| 2191 | ||
| 2324 | ||
| 2192 | 2325 | // Foo bar |
| 2193 | 2326 | |
| 2194 | 2327 | |
| 2195 | 2328 | |
| 2196 | 2329 | <a name="method-str-ucsplit"></a> |
| 2330 | ||
| 2197 | 2331 | #### `Str::ucsplit()` {.collection-method} |
| 2198 | 2332 | |
| 2199 | 2333 | `Str::ucsplit` 方法将给定的字符串按大写字符拆分为数组: |
| 2200 | 2334 | |
| 2201 | 2335 | use Illuminate\Support\Str; |
| 2202 | ||
| 2336 | ||
| 2203 | 2337 | $segments = Str::ucsplit('FooBar'); |
| 2204 | ||
| 2338 | ||
| 2205 | 2339 | // [0 => 'Foo', 1 => 'Bar'] |
| 2206 | 2340 | |
| 2207 | 2341 | <a name="method-str-upper"></a> |
| 2342 | ||
| 2208 | 2343 | #### `Str::upper()` {.collection-method} |
| 2209 | 2344 | |
| 2210 | 2345 | `Str::upper` 方法将给定的字符串转换为大写: |
| 2211 | 2346 | |
| 2212 | 2347 | use Illuminate\Support\Str; |
| 2213 | ||
| 2348 | ||
| 2214 | 2349 | $string = Str::upper('laravel'); |
| 2215 | ||
| 2350 | ||
| 2216 | 2351 | // LARAVEL |
| 2217 | 2352 | |
| 2218 | 2353 | <a name="method-str-ulid"></a> |
| 2354 | ||
| 2219 | 2355 | #### `Str::ulid()` {.collection-method} |
| 2220 | 2356 | |
| 2221 | 2357 | `Str::ulid` 方法生成一个 ULID: |
| 2222 | 2358 | |
| 2223 | 2359 | use Illuminate\Support\Str; |
| 2224 | ||
| 2360 | ||
| 2225 | 2361 | return (string) Str::ulid(); |
| 2226 | 2362 | |
| 2227 | 2363 | // 01gd6r360bp37zj17nxb55yv40 |
| 2228 | 2364 | |
| 2229 | 2365 | <a name="method-str-uuid"></a> |
| 2366 | ||
| 2230 | 2367 | #### `Str::uuid()` {.collection-method} |
| 2231 | 2368 | |
| 2232 | 2369 | `Str::uuid` 方法生成一个 UUID(版本 4): |
| 2233 | 2370 | |
| 2234 | 2371 | use Illuminate\Support\Str; |
| 2235 | ||
| 2372 | ||
| 2236 | 2373 | return (string) Str::uuid(); |
| 2237 | 2374 | |
| 2238 | 2375 | <a name="method-str-word-count"></a> |
| 2376 | ||
| 2239 | 2377 | #### `Str::wordCount()` {.collection-method} |
| 2240 | 2378 | |
| 2241 | 2379 | `Str::wordCount` 方法返回字符串包含的单词数 | … | … |
| 2247 | 2385 | ``` |
| 2248 | 2386 | |
| 2249 | 2387 | <a name="method-str-words"></a> |
| 2388 | ||
| 2250 | 2389 | #### `Str::words()` {.collection-method} |
| 2251 | 2390 | |
| 2252 | 2391 | `Str::words` 方法限制字符串中的单词数。 可以通过其第三个参数将附加字符串传递给此方法,以指定应将这个字符串附加到截断后的字符串末尾: |
| 2253 | 2392 | |
| 2254 | 2393 | use Illuminate\Support\Str; |
| 2255 | ||
| 2394 | ||
| 2256 | 2395 | return Str::words('Perfectly balanced, as all things should be.', 3, ' >>>'); |
| 2257 | ||
| 2396 | ||
| 2258 | 2397 | // Perfectly balanced, as >>> |
| 2259 | 2398 | |
| 2260 | 2399 | <a name="method-str"></a> |
| 2400 | ||
| 2261 | 2401 | #### `str()` {.collection-method} |
| 2262 | 2402 | |
| 2263 | 2403 | `str` 函数返回给定字符串的新 `Illuminate\Support\Stringable` 实例。 此函数等效于 `Str::of` 方法: |
| 2264 | 2404 | |
| 2265 | 2405 | $string = str('Taylor')->append(' Otwell'); |
| 2266 | ||
| 2406 | ||
| 2267 | 2407 | // 'Taylor Otwell' |
| 2268 | 2408 | |
| 2269 | 2409 | 如果没有为 `str` 函数提供参数,该函数将返回 `Illuminate\Support\Str` 的实例: |
| 2270 | 2410 | |
| 2271 | 2411 | $snake = str()->snake('FooBar'); |
| 2272 | ||
| 2412 | ||
| 2273 | 2413 | // 'foo_bar' |
| 2274 | 2414 | |
| 2275 | 2415 | |
| 2276 | 2416 | |
| 2277 | 2417 | <a name="method-trans"></a> |
| 2418 | ||
| 2278 | 2419 | #### `trans()` {.collection-method} |
| 2279 | 2420 | |
| 2280 | 2421 | `trans` 函数使用你的 [语言文件](/docs/laravel/10.x/localization) 翻译给定的翻译键: |
| 2422 | ||
| 2281 | 2423 | ``` |
| 2282 | 2424 | echo trans('messages.welcome'); |
| 2283 | 2425 | ``` |
| 2426 | ||
| 2284 | 2427 | 如果指定的翻译键不存在,`trans` 函数将返回给定的键。 因此,使用上面的示例,如果翻译键不存在,`trans` 函数将返回 `messages.welcome`。 |
| 2285 | 2428 | |
| 2286 | 2429 | <a name="method-trans-choice"></a> |
| 2430 | ||
| 2287 | 2431 | #### `trans_choice()` {.collection-method} |
| 2288 | 2432 | |
| 2289 | 2433 | `trans_choice` 函数用词形变化翻译给定的翻译键: |
| 2434 | ||
| 2290 | 2435 | ``` |
| 2291 | 2436 | echo trans_choice('messages.notifications', $unreadCount); |
| 2292 | 2437 | ``` |
| 2438 | ||
| 2293 | 2439 | 如果指定的翻译键不存在,`trans_choice` 函数将返回给定的键。 因此,使用上面的示例,如果翻译键不存在,`trans_choice` 函数将返回 `messages.notifications`。 |
| 2294 | 2440 | |
| 2295 | 2441 | <a name="fluent-strings"></a> |
| 2442 | ||
| 2296 | 2443 | ## 字符流处理 |
| 2297 | 2444 | |
| 2298 | 2445 | Fluent strings 提供了一个更流畅的、面向对象的接口来处理字符串值,与传统的字符串操作相比,允许你使用更易读的语法将多个字符串操作链接在一起。 |
| 2299 | 2446 | |
| 2300 | 2447 | <a name="method-fluent-str-after"></a> |
| 2448 | ||
| 2301 | 2449 | #### `after` {.collection-method} |
| 2302 | 2450 | |
| 2303 | 2451 | `after` 方法返回字符串中给定值之后的所有内容。 如果字符串中不存在该值,则将返回整个字符串: |
| 2304 | 2452 | |
| 2305 | 2453 | use Illuminate\Support\Str; |
| 2306 | ||
| 2454 | ||
| 2307 | 2455 | $slice = Str::of('This is my name')->after('This is'); |
| 2308 | ||
| 2456 | ||
| 2309 | 2457 | // ' my name' |
| 2310 | 2458 | |
| 2311 | 2459 | <a name="method-fluent-str-after-last"></a> |
| 2460 | ||
| 2312 | 2461 | #### `afterLast` {.collection-method} |
| 2313 | 2462 | |
| 2314 | 2463 | `afterLast` 方法返回字符串中最后一次出现给定值之后的所有内容。 如果字符串中不存在该值,则将返回整个字符串 |
| 2315 | 2464 | |
| 2316 | 2465 | use Illuminate\Support\Str; |
| 2317 | ||
| 2466 | ||
| 2318 | 2467 | $slice = Str::of('App\Http\Controllers\Controller')->afterLast('\\'); |
| 2319 | ||
| 2468 | ||
| 2320 | 2469 | // 'Controller' |
| 2321 | 2470 | |
| 2322 | 2471 | |
| 2323 | 2472 | |
| 2324 | 2473 | <a name="method-fluent-str-append"></a> |
| 2474 | ||
| 2325 | 2475 | #### `append` {.collection-method} |
| 2326 | 2476 | |
| 2327 | 2477 | `append` 方法将给定的值附加到字符串: |
| 2328 | 2478 | |
| 2329 | 2479 | use Illuminate\Support\Str; |
| 2330 | ||
| 2480 | ||
| 2331 | 2481 | $string = Str::of('Taylor')->append(' Otwell'); |
| 2332 | ||
| 2482 | ||
| 2333 | 2483 | // 'Taylor Otwell' |
| 2334 | 2484 | |
| 2335 | 2485 | <a name="method-fluent-str-ascii"></a> |
| 2486 | ||
| 2336 | 2487 | #### `ascii` {.collection-method} |
| 2337 | 2488 | |
| 2338 | 2489 | `ascii` 方法将尝试将字符串音译为 ASCII 值: |
| 2339 | 2490 | |
| 2340 | 2491 | use Illuminate\Support\Str; |
| 2341 | ||
| 2492 | ||
| 2342 | 2493 | $string = Str::of('ü')->ascii(); |
| 2343 | ||
| 2494 | ||
| 2344 | 2495 | // 'u' |
| 2345 | 2496 | |
| 2346 | 2497 | <a name="method-fluent-str-basename"></a> |
| 2498 | ||
| 2347 | 2499 | #### `basename` {.collection-method} |
| 2348 | 2500 | |
| 2349 | 2501 | `basename` 方法将返回给定字符串的结尾名称部分: |
| 2350 | 2502 | |
| 2351 | 2503 | use Illuminate\Support\Str; |
| 2352 | ||
| 2504 | ||
| 2353 | 2505 | $string = Str::of('/foo/bar/baz')->basename(); |
| 2354 | ||
| 2506 | ||
| 2355 | 2507 | // 'baz' |
| 2356 | 2508 | |
| 2357 | 2509 | 如果需要,你可以提供将从尾随组件中删除的「扩展名」: |
| 2358 | 2510 | |
| 2359 | 2511 | use Illuminate\Support\Str; |
| 2360 | ||
| 2512 | ||
| 2361 | 2513 | $string = Str::of('/foo/bar/baz.jpg')->basename('.jpg'); |
| 2362 | ||
| 2514 | ||
| 2363 | 2515 | // 'baz' |
| 2364 | 2516 | |
| 2365 | 2517 | <a name="method-fluent-str-before"></a> |
| 2518 | ||
| 2366 | 2519 | #### `before` {.collection-method} |
| 2367 | 2520 | |
| 2368 | 2521 | `before` 方法返回字符串中给定值之前的所有内容: |
| 2369 | 2522 | |
| 2370 | 2523 | use Illuminate\Support\Str; |
| 2371 | ||
| 2524 | ||
| 2372 | 2525 | $slice = Str::of('This is my name')->before('my name'); |
| 2373 | ||
| 2526 | ||
| 2374 | 2527 | // 'This is ' |
| 2375 | 2528 | |
| 2376 | 2529 | <a name="method-fluent-str-before-last"></a> |
| 2530 | ||
| 2377 | 2531 | #### `beforeLast` {.collection-method} |
| 2378 | 2532 | |
| 2379 | 2533 | `beforeLast` 方法返回字符串中最后一次出现给定值之前的所有内容: |
| 2380 | 2534 | |
| 2381 | 2535 | use Illuminate\Support\Str; |
| 2382 | ||
| 2536 | ||
| 2383 | 2537 | $slice = Str::of('This is my name')->beforeLast('is'); |
| 2384 | ||
| 2538 | ||
| 2385 | 2539 | // 'This ' |
| 2386 | 2540 | |
| 2387 | 2541 | <a name="method-fluent-str-between"></a> |
| 2542 | ||
| 2388 | 2543 | #### `between` {.collection-method} |
| 2389 | 2544 | |
| 2390 | 2545 | `between` 方法返回两个值之间的字符串部分: |
| 2391 | 2546 | |
| 2392 | 2547 | use Illuminate\Support\Str; |
| 2393 | ||
| 2548 | ||
| 2394 | 2549 | $converted = Str::of('This is my name')->between('This', 'name'); |
| 2395 | ||
| 2550 | ||
| 2396 | 2551 | // ' is my ' |
| 2397 | 2552 | |
| 2398 | 2553 | <a name="method-fluent-str-between-first"></a> |
| 2554 | ||
| 2399 | 2555 | #### `betweenFirst` {.collection-method} |
| 2400 | 2556 | |
| 2401 | 2557 | `betweenFirst` 方法返回两个值之间字符串的最小可能部分: |
| 2402 | 2558 | |
| 2403 | 2559 | use Illuminate\Support\Str; |
| 2404 | ||
| 2560 | ||
| 2405 | 2561 | $converted = Str::of('[a] bc [d]')->betweenFirst('[', ']'); |
| 2406 | ||
| 2562 | ||
| 2407 | 2563 | // 'a' |
| 2408 | 2564 | |
| 2409 | 2565 | |
| 2410 | 2566 | |
| 2411 | 2567 | <a name="method-fluent-str-camel"></a> |
| 2568 | ||
| 2412 | 2569 | #### `camel` {.collection-method} |
| 2413 | 2570 | |
| 2414 | 2571 | `camel` 方法将给定的字符串转换为 `camelCase`: |
| 2415 | 2572 | |
| 2416 | 2573 | use Illuminate\Support\Str; |
| 2417 | ||
| 2574 | ||
| 2418 | 2575 | $converted = Str::of('foo_bar')->camel(); |
| 2419 | ||
| 2576 | ||
| 2420 | 2577 | // fooBar |
| 2421 | 2578 | |
| 2422 | 2579 | <a name="method-fluent-str-class-basename"></a> |
| 2580 | ||
| 2423 | 2581 | #### `classBasename` {.collection-method} |
| 2424 | 2582 | |
| 2425 | 2583 | `classBasename` 方法返回给定类的类名,删除了类的命名空间: |
| 2426 | 2584 | |
| 2427 | 2585 | use Illuminate\Support\Str; |
| 2428 | ||
| 2586 | ||
| 2429 | 2587 | $class = Str::of('Foo\Bar\Baz')->classBasename(); |
| 2430 | ||
| 2588 | ||
| 2431 | 2589 | // Baz |
| 2432 | 2590 | |
| 2433 | 2591 | <a name="method-fluent-str-contains"></a> |
| 2592 | ||
| 2434 | 2593 | #### `contains` {.collection-method} |
| 2435 | 2594 | |
| 2436 | 2595 | `contains` 方法确定给定的字符串是否包含给定的值。 此方法区分大小写: |
| 2437 | 2596 | |
| 2438 | 2597 | use Illuminate\Support\Str; |
| 2439 | ||
| 2598 | ||
| 2440 | 2599 | $contains = Str::of('This is my name')->contains('my'); |
| 2441 | ||
| 2600 | ||
| 2442 | 2601 | // true |
| 2443 | 2602 | |
| 2444 | 2603 | 你还可以传递一个值数组来确定给定字符串是否包含数组中的任意值: |
| 2445 | 2604 | |
| 2446 | 2605 | use Illuminate\Support\Str; |
| 2447 | ||
| 2606 | ||
| 2448 | 2607 | $contains = Str::of('This is my name')->contains(['my', 'foo']); |
| 2449 | ||
| 2608 | ||
| 2450 | 2609 | // true |
| 2451 | 2610 | |
| 2452 | 2611 | <a name="method-fluent-str-contains-all"></a> |
| 2612 | ||
| 2453 | 2613 | #### `containsAll` {.collection-method} |
| 2454 | 2614 | |
| 2455 | 2615 | `containsAll` 方法确定给定字符串是否包含给定数组中的所有值: |
| 2456 | 2616 | |
| 2457 | 2617 | use Illuminate\Support\Str; |
| 2458 | ||
| 2618 | ||
| 2459 | 2619 | $containsAll = Str::of('This is my name')->containsAll(['my', 'name']); |
| 2460 | ||
| 2620 | ||
| 2461 | 2621 | // true |
| 2462 | 2622 | |
| 2463 | 2623 | <a name="method-fluent-str-dirname"></a> |
| 2624 | ||
| 2464 | 2625 | #### `dirname` {.collection-method} |
| 2465 | 2626 | |
| 2466 | 2627 | `dirname` 方法返回给定字符串的父目录部分: |
| 2467 | 2628 | |
| 2468 | 2629 | use Illuminate\Support\Str; |
| 2469 | ||
| 2630 | ||
| 2470 | 2631 | $string = Str::of('/foo/bar/baz')->dirname(); |
| 2471 | ||
| 2632 | ||
| 2472 | 2633 | // '/foo/bar' |
| 2473 | 2634 | |
| 2474 | 2635 | 如有必要,你还可以指定要从字符串中删除多少目录级别: |
| 2475 | 2636 | |
| 2476 | 2637 | use Illuminate\Support\Str; |
| 2477 | ||
| 2638 | ||
| 2478 | 2639 | $string = Str::of('/foo/bar/baz')->dirname(2); |
| 2479 | ||
| 2640 | ||
| 2480 | 2641 | // '/foo' |
| 2481 | 2642 | |
| 2482 | 2643 | <a name="method-fluent-str-excerpt"></a> |
| 2644 | ||
| 2483 | 2645 | #### `excerpt` {.collection-method} |
| 2484 | 2646 | |
| 2485 | 2647 | `excerpt` 方法从字符串中提取与该字符串中短语的第一个实例匹配的摘录: |
| 2486 | 2648 | |
| 2487 | 2649 | use Illuminate\Support\Str; |
| 2488 | ||
| 2650 | ||
| 2489 | 2651 | $excerpt = Str::of('This is my name')->excerpt('my', [ |
| 2490 | 2652 | 'radius' => 3 |
| 2491 | 2653 | ]); |
| 2492 | ||
| 2654 | ||
| 2493 | 2655 | // '...is my na...' |
| 2494 | 2656 | |
| 2495 | 2657 | … | … |
| 2499 | 2661 | 此外,还可以使用 `omission` 选项更改将添加到截断字符串之前和附加的字符串 |
| 2500 | 2662 | |
| 2501 | 2663 | use Illuminate\Support\Str; |
| 2502 | ||
| 2664 | ||
| 2503 | 2665 | $excerpt = Str::of('This is my name')->excerpt('name', [ |
| 2504 | 2666 | 'radius' => 3, |
| 2505 | 2667 | 'omission' => '(...) ' |
| 2506 | 2668 | ]); |
| 2507 | ||
| 2669 | ||
| 2508 | 2670 | // '(...) my name' |
| 2509 | 2671 | |
| 2510 | 2672 | <a name="method-fluent-str-ends-with"></a> |
| 2673 | ||
| 2511 | 2674 | #### `endsWith` {.collection-method} |
| 2512 | 2675 | |
| 2513 | 2676 | `endsWith` 方法确定给定字符串是否以给定值结尾: |
| 2514 | 2677 | |
| 2515 | 2678 | use Illuminate\Support\Str; |
| 2516 | ||
| 2679 | ||
| 2517 | 2680 | $result = Str::of('This is my name')->endsWith('name'); |
| 2518 | ||
| 2681 | ||
| 2519 | 2682 | // true |
| 2520 | 2683 | |
| 2521 | 2684 | 你还可以传递一个值数组来确定给定字符串是否以数组中的任何值结尾: |
| 2522 | 2685 | |
| 2523 | 2686 | use Illuminate\Support\Str; |
| 2524 | ||
| 2687 | ||
| 2525 | 2688 | $result = Str::of('This is my name')->endsWith(['name', 'foo']); |
| 2526 | ||
| 2689 | ||
| 2527 | 2690 | // true |
| 2528 | ||
| 2691 | ||
| 2529 | 2692 | $result = Str::of('This is my name')->endsWith(['this', 'foo']); |
| 2530 | ||
| 2693 | ||
| 2531 | 2694 | // false |
| 2532 | 2695 | |
| 2533 | 2696 | <a name="method-fluent-str-exactly"></a> |
| 2697 | ||
| 2534 | 2698 | #### `exactly` {.collection-method} |
| 2535 | 2699 | |
| 2536 | 2700 | `exactly` 方法确定给定的字符串是否与另一个字符串完全匹配: |
| 2537 | 2701 | |
| 2538 | 2702 | use Illuminate\Support\Str; |
| 2539 | ||
| 2703 | ||
| 2540 | 2704 | $result = Str::of('Laravel')->exactly('Laravel'); |
| 2541 | ||
| 2705 | ||
| 2542 | 2706 | // true |
| 2543 | 2707 | |
| 2544 | 2708 | <a name="method-fluent-str-explode"></a> |
| 2709 | ||
| 2545 | 2710 | #### `explode` {.collection-method} |
| 2546 | 2711 | |
| 2547 | 2712 | `explode` 方法按给定的分隔符拆分字符串并返回包含拆分字符串的每个部分的集合: |
| 2548 | 2713 | |
| 2549 | 2714 | use Illuminate\Support\Str; |
| 2550 | ||
| 2715 | ||
| 2551 | 2716 | $collection = Str::of('foo bar baz')->explode(' '); |
| 2552 | ||
| 2717 | ||
| 2553 | 2718 | // collect(['foo', 'bar', 'baz']) |
| 2554 | 2719 | |
| 2555 | 2720 | <a name="method-fluent-str-finish"></a> |
| 2721 | ||
| 2556 | 2722 | #### `finish` {.collection-method} |
| 2557 | 2723 | |
| 2558 | 2724 | `finish` 方法将给定值的单个实例添加到字符串中(如果它尚未以该值结尾): |
| 2559 | 2725 | use Illuminate\Support\Str; |
| 2560 | 2726 | |
| 2561 | 2727 | $adjusted = Str::of('this/string')->finish('/'); |
| 2562 | ||
| 2728 | ||
| 2563 | 2729 | // this/string/ |
| 2564 | ||
| 2730 | ||
| 2565 | 2731 | $adjusted = Str::of('this/string/')->finish('/'); |
| 2566 | ||
| 2732 | ||
| 2567 | 2733 | // this/string/ |
| 2568 | 2734 | |
| 2569 | 2735 | |
| 2570 | 2736 | |
| 2571 | 2737 | <a name="method-fluent-str-headline"></a> |
| 2738 | ||
| 2572 | 2739 | #### `headline` {.collection-method} |
| 2573 | 2740 | |
| 2574 | 2741 | `headline` 方法会将由大小写、连字符或下划线分隔的字符串转换为空格分隔的字符串,每个单词的首字母大写: |
| 2575 | 2742 | |
| 2576 | 2743 | use Illuminate\Support\Str; |
| 2577 | ||
| 2744 | ||
| 2578 | 2745 | $headline = Str::of('taylor_otwell')->headline(); |
| 2579 | ||
| 2746 | ||
| 2580 | 2747 | // Taylor Otwell |
| 2581 | ||
| 2748 | ||
| 2582 | 2749 | $headline = Str::of('EmailNotificationSent')->headline(); |
| 2583 | ||
| 2750 | ||
| 2584 | 2751 | // Email Notification Sent |
| 2585 | 2752 | |
| 2586 | 2753 | <a name="method-fluent-str-inline-markdown"></a> |
| 2754 | ||
| 2587 | 2755 | #### `inlineMarkdown` {.collection-method} |
| 2588 | 2756 | |
| 2589 | 2757 | `inlineMarkdown` 方法使用 [CommonMark](https://commonmark.thephpleague.com/) 将 GitHub 风格的 Markdown 转换为内联 HTML。 但是,与 `markdown` 方法不同,它不会将所有生成的 HTML 包装在块级元素中: |
| 2590 | 2758 | |
| 2591 | 2759 | use Illuminate\Support\Str; |
| 2592 | ||
| 2760 | ||
| 2593 | 2761 | $html = Str::of('**Laravel**')->inlineMarkdown(); |
| 2594 | ||
| 2762 | ||
| 2595 | 2763 | // <strong>Laravel</strong> |
| 2596 | 2764 | |
| 2597 | 2765 | <a name="method-fluent-str-is"></a> |
| 2766 | ||
| 2598 | 2767 | #### `is` {.collection-method} |
| 2599 | 2768 | |
| 2600 | 2769 | `is` 方法确定给定字符串是否与给定模式匹配。 星号可用作通配符值 |
| 2601 | 2770 | |
| 2602 | 2771 | use Illuminate\Support\Str; |
| 2603 | ||
| 2772 | ||
| 2604 | 2773 | $matches = Str::of('foobar')->is('foo*'); |
| 2605 | ||
| 2774 | ||
| 2606 | 2775 | // true |
| 2607 | ||
| 2776 | ||
| 2608 | 2777 | $matches = Str::of('foobar')->is('baz*'); |
| 2609 | ||
| 2778 | ||
| 2610 | 2779 | // false |
| 2611 | 2780 | |
| 2612 | 2781 | <a name="method-fluent-str-is-ascii"></a> |
| 2782 | ||
| 2613 | 2783 | #### `isAscii` {.collection-method} |
| 2614 | 2784 | |
| 2615 | 2785 | `isAscii` 方法确定给定字符串是否为 ASCII 字符串: |
| 2616 | 2786 | |
| 2617 | 2787 | use Illuminate\Support\Str; |
| 2618 | ||
| 2788 | ||
| 2619 | 2789 | $result = Str::of('Taylor')->isAscii(); |
| 2620 | ||
| 2790 | ||
| 2621 | 2791 | // true |
| 2622 | ||
| 2792 | ||
| 2623 | 2793 | $result = Str::of('ü')->isAscii(); |
| 2624 | ||
| 2794 | ||
| 2625 | 2795 | // false |
| 2626 | 2796 | |
| 2627 | 2797 | <a name="method-fluent-str-is-empty"></a> |
| 2798 | ||
| 2628 | 2799 | #### `isEmpty` {.collection-method} |
| 2629 | 2800 | |
| 2630 | 2801 | `isEmpty` 方法确定给定的字符串是否为空: |
| 2631 | 2802 | |
| 2632 | 2803 | use Illuminate\Support\Str; |
| 2633 | ||
| 2804 | ||
| 2634 | 2805 | $result = Str::of(' ')->trim()->isEmpty(); |
| 2635 | ||
| 2806 | ||
| 2636 | 2807 | // true |
| 2637 | ||
| 2808 | ||
| 2638 | 2809 | $result = Str::of('Laravel')->trim()->isEmpty(); |
| 2639 | ||
| 2810 | ||
| 2640 | 2811 | // false |
| 2641 | 2812 | |
| 2642 | 2813 | <a name="method-fluent-str-is-not-empty"></a> |
| 2814 | ||
| 2643 | 2815 | #### `isNotEmpty` {.collection-method} |
| 2644 | 2816 | |
| 2645 | 2817 | `isNotEmpty` 方法确定给定的字符串是否不为空: |
| 2646 | 2818 | |
| 2647 | 2819 | use Illuminate\Support\Str; |
| 2648 | ||
| 2820 | ||
| 2649 | 2821 | $result = Str::of(' ')->trim()->isNotEmpty(); |
| 2650 | ||
| 2822 | ||
| 2651 | 2823 | // false |
| 2652 | ||
| 2824 | ||
| 2653 | 2825 | $result = Str::of('Laravel')->trim()->isNotEmpty(); |
| 2654 | ||
| 2826 | ||
| 2655 | 2827 | // true |
| 2656 | 2828 | |
| 2657 | 2829 | |
| 2658 | 2830 | |
| 2659 | 2831 | <a name="method-fluent-str-is-json"></a> |
| 2832 | ||
| 2660 | 2833 | #### `isJson` {.collection-method} |
| 2661 | 2834 | |
| 2662 | 2835 | `isJson` 方法确定给定的字符串是否是有效的 JSON: |
| 2663 | 2836 | |
| 2664 | 2837 | use Illuminate\Support\Str; |
| 2665 | ||
| 2838 | ||
| 2666 | 2839 | $result = Str::of('[1,2,3]')->isJson(); |
| 2667 | ||
| 2840 | ||
| 2668 | 2841 | // true |
| 2669 | ||
| 2842 | ||
| 2670 | 2843 | $result = Str::of('{"first": "John", "last": "Doe"}')->isJson(); |
| 2671 | ||
| 2844 | ||
| 2672 | 2845 | // true |
| 2673 | ||
| 2846 | ||
| 2674 | 2847 | $result = Str::of('{first: "John", last: "Doe"}')->isJson(); |
| 2675 | ||
| 2848 | ||
| 2676 | 2849 | // false |
| 2677 | 2850 | |
| 2678 | 2851 | <a name="method-fluent-str-is-ulid"></a> |
| 2852 | ||
| 2679 | 2853 | #### `isUlid` {.collection-method} |
| 2680 | 2854 | |
| 2681 | 2855 | `isUlid` 方法确定给定的字符串是否一个 ULID: |
| 2682 | 2856 | |
| 2683 | 2857 | use Illuminate\Support\Str; |
| 2684 | ||
| 2858 | ||
| 2685 | 2859 | $result = Str::of('01gd6r360bp37zj17nxb55yv40')->isUlid(); |
| 2686 | ||
| 2860 | ||
| 2687 | 2861 | // true |
| 2688 | ||
| 2862 | ||
| 2689 | 2863 | $result = Str::of('Taylor')->isUlid(); |
| 2690 | ||
| 2864 | ||
| 2691 | 2865 | // false |
| 2692 | 2866 | |
| 2693 | 2867 | <a name="method-fluent-str-is-uuid"></a> |
| 2868 | ||
| 2694 | 2869 | #### `isUuid` {.collection-method} |
| 2695 | 2870 | |
| 2696 | 2871 | `isUuid` 方法确定给定的字符串是否是一个 UUID: |
| 2697 | 2872 | |
| 2698 | 2873 | use Illuminate\Support\Str; |
| 2699 | ||
| 2874 | ||
| 2700 | 2875 | $result = Str::of('5ace9ab9-e9cf-4ec6-a19d-5881212a452c')->isUuid(); |
| 2701 | ||
| 2876 | ||
| 2702 | 2877 | // true |
| 2703 | ||
| 2878 | ||
| 2704 | 2879 | $result = Str::of('Taylor')->isUuid(); |
| 2705 | ||
| 2880 | ||
| 2706 | 2881 | // false |
| 2707 | 2882 | |
| 2708 | 2883 | <a name="method-fluent-str-kebab"></a> |
| 2884 | ||
| 2709 | 2885 | #### `kebab` {.collection-method} |
| 2710 | 2886 | |
| 2711 | 2887 | `kebab` 方法转变给定的字符串为 `kebab-case`: |
| 2712 | 2888 | |
| 2713 | 2889 | use Illuminate\Support\Str; |
| 2714 | ||
| 2890 | ||
| 2715 | 2891 | $converted = Str::of('fooBar')->kebab(); |
| 2716 | ||
| 2892 | ||
| 2717 | 2893 | // foo-bar |
| 2718 | 2894 | |
| 2719 | 2895 | <a name="method-fluent-str-lcfirst"></a> |
| 2896 | ||
| 2720 | 2897 | #### `lcfirst` {.collection-method} |
| 2721 | 2898 | |
| 2722 | 2899 | `lcfirst` 方法返回给定的字符串的第一个字符为小写字母: |
| 2723 | 2900 | |
| 2724 | 2901 | use Illuminate\Support\Str; |
| 2725 | ||
| 2902 | ||
| 2726 | 2903 | $string = Str::of('Foo Bar')->lcfirst(); |
| 2727 | ||
| 2904 | ||
| 2728 | 2905 | // foo Bar |
| 2729 | 2906 | |
| 2730 | 2907 | |
| 2731 | 2908 | <a name="method-fluent-str-length"></a> |
| 2909 | ||
| 2732 | 2910 | #### `length` {.collection-method} |
| 2733 | 2911 | |
| 2734 | 2912 | `length` 方法返回给定字符串的长度: |
| 2735 | 2913 | |
| 2736 | 2914 | use Illuminate\Support\Str; |
| 2737 | ||
| 2915 | ||
| 2738 | 2916 | $length = Str::of('Laravel')->length(); |
| 2739 | ||
| 2917 | ||
| 2740 | 2918 | // 7 |
| 2741 | 2919 | |
| 2742 | 2920 | <a name="method-fluent-str-limit"></a> |
| 2921 | ||
| 2743 | 2922 | #### `limit` {.collection-method} |
| 2744 | 2923 | |
| 2745 | 2924 | `limit` 方法将给定的字符串截断为指定的长度: |
| 2746 | 2925 | |
| 2747 | 2926 | use Illuminate\Support\Str; |
| 2748 | ||
| 2927 | ||
| 2749 | 2928 | $truncated = Str::of('The quick brown fox jumps over the lazy dog')->limit(20); |
| 2750 | ||
| 2929 | ||
| 2751 | 2930 | // The quick brown fox... |
| 2752 | 2931 | |
| 2753 | 2932 | … | … |
| 2755 | 2934 | 你也可以通过第二个参数来改变追加到末尾的字符串: |
| 2756 | 2935 | |
| 2757 | 2936 | use Illuminate\Support\Str; |
| 2758 | ||
| 2937 | ||
| 2759 | 2938 | $truncated = Str::of('The quick brown fox jumps over the lazy dog')->limit(20, ' (...)'); |
| 2760 | ||
| 2939 | ||
| 2761 | 2940 | // The quick brown fox (...) |
| 2762 | 2941 | |
| 2763 | 2942 | <a name="method-fluent-str-lower"></a> |
| 2943 | ||
| 2764 | 2944 | #### `lower` |
| 2765 | 2945 | |
| 2766 | `lower` | |
| 2767 |
| |
| 2768 | use Illuminate\Support\Str;
| |
| 2769 | ||
| 2946 | `lower` 方法将指定字符串转换为小写: | |
| 2947 | ||
| 2948 | use Illuminate\Support\Str; | |
| 2949 | ||
| 2770 | 2950 | $result = Str::of('LARAVEL')->lower(); |
| 2771 | ||
| 2951 | ||
| 2772 | 2952 | // 'laravel' |
| 2773 | 2953 | |
| 2774 | 2954 | <a name="method-fluent-str-ltrim"></a> |
| 2955 | ||
| 2775 | 2956 | #### `ltrim` |
| 2776 | 2957 | |
| 2777 | `ltrim` | |
| 2778 |
| |
| 2779 | use Illuminate\Support\Str;
| |
| 2780 | ||
| 2958 | `ltrim` 方法移除字符串左端指定的字符: | |
| 2959 | ||
| 2960 | use Illuminate\Support\Str; | |
| 2961 | ||
| 2781 | 2962 | $string = Str::of(' Laravel ')->ltrim(); |
| 2782 | ||
| 2963 | ||
| 2783 | 2964 | // 'Laravel ' |
| 2784 | ||
| 2965 | ||
| 2785 | 2966 | $string = Str::of('/Laravel/')->ltrim('/'); |
| 2786 | ||
| 2967 | ||
| 2787 | 2968 | // 'Laravel/' |
| 2788 | 2969 | |
| 2789 | 2970 | <a name="method-fluent-str-markdown"></a> |
| 2971 | ||
| 2790 | 2972 | #### `markdown` {.collection-method} |
| 2791 | 2973 | |
| 2792 | `markdown` | |
| 2793 |
| |
| 2794 | use Illuminate\Support\Str;
| |
| 2795 | ||
| 2974 | `markdown` 方法将 Github 风格的 Markdown 转换为 HTML: | |
| 2975 | ||
| 2976 | use Illuminate\Support\Str; | |
| 2977 | ||
| 2796 | 2978 | $html = Str::of('# Laravel')->markdown(); |
| 2797 | ||
| 2979 | ||
| 2798 | 2980 | // <h1>Laravel</h1> |
| 2799 | ||
| 2981 | ||
| 2800 | 2982 | $html = Str::of('# Taylor <b>Otwell</b>')->markdown([ |
| 2801 | 2983 | 'html_input' => 'strip', |
| 2802 | 2984 | ]); |
| 2803 | ||
| 2985 | ||
| 2804 | 2986 | // <h1>Taylor Otwell</h1> |
| 2805 | 2987 | |
| 2806 | 2988 | <a name="method-fluent-str-mask"></a> |
| 2989 | ||
| 2807 | 2990 | #### `mask` |
| 2808 | 2991 | |
| 2809 | `mask` | |
| 2810 |
| |
| 2811 | use Illuminate\Support\Str;
| |
| 2812 | ||
| 2992 | `mask` 方法用重复字符掩盖字符串的一部分,可用于模糊处理字符串的某些段,例如电子邮件地址和电话号码: | |
| 2993 | ||
| 2994 | use Illuminate\Support\Str; | |
| 2995 | ||
| 2813 | 2996 | $string = Str::of('taylor@example.com')->mask('*', 3); |
| 2814 | ||
| 2997 | ||
| 2815 | 2998 | // tay*************** |
| 2816 | 2999 | |
| 2817 | 需要的话,你可以提供一个负数作为 | |
| 3000 | 需要的话,你可以提供一个负数作为 `mask` 方法的第三或第四个参数,这将指示该方法在距字符串末尾的给定距离处开始屏蔽: | |
| 2818 | 3001 | |
| 2819 | 3002 | $string = Str::of('taylor@example.com')->mask('*', -15, 3); |
| 2820 | ||
| 3003 | ||
| 2821 | 3004 | // tay***@example.com |
| 2822 | ||
| 3005 | ||
| 2823 | 3006 | $string = Str::of('taylor@example.com')->mask('*', 4, -4); |
| 2824 | ||
| 3007 | ||
| 2825 | 3008 | // tayl**********.com |
| 2826 | 3009 | |
| 2827 | 3010 | |
| 2828 | 3011 | |
| 2829 | 3012 | <a name="method-fluent-str-match"></a> |
| 3013 | ||
| 2830 | 3014 | #### `match` |
| 2831 | 3015 | |
| 2832 | `match` | |
| 2833 |
| |
| 2834 | use Illuminate\Support\Str;
| |
| 2835 | ||
| 3016 | `match` 方法将会返回字符串中和指定正则表达式匹配的部分: | |
| 3017 | ||
| 3018 | use Illuminate\Support\Str; | |
| 3019 | ||
| 2836 | 3020 | $result = Str::of('foo bar')->match('/bar/'); |
| 2837 | ||
| 3021 | ||
| 2838 | 3022 | // 'bar' |
| 2839 | ||
| 3023 | ||
| 2840 | 3024 | $result = Str::of('foo bar')->match('/foo (.*)/'); |
| 2841 | ||
| 3025 | ||
| 2842 | 3026 | // 'bar' |
| 2843 | 3027 | |
| 2844 | 3028 | <a name="method-fluent-str-match-all"></a> |
| 3029 | ||
| 2845 | 3030 | #### `matchAll` |
| 2846 | 3031 | |
| 2847 | `matchAll` | |
| 2848 |
| |
| 2849 | use Illuminate\Support\Str;
| |
| 2850 | ||
| 3032 | `matchAll` 方法将会返回一个集合,该集合包含了字符串中与指定正则表达式匹配的部分 | |
| 3033 | ||
| 3034 | use Illuminate\Support\Str; | |
| 3035 | ||
| 2851 | 3036 | $result = Str::of('bar foo bar')->matchAll('/bar/'); |
| 2852 | ||
| 3037 | ||
| 2853 | 3038 | // collect(['bar', 'bar']) |
| 2854 | 3039 | |
| 2855 | 3040 | 如果你在正则表达式中指定了一个匹配组, Laravel 将会返回与该组匹配的集合: |
| 2856 | 3041 | |
| 2857 | 3042 | use Illuminate\Support\Str; |
| 2858 | ||
| 3043 | ||
| 2859 | 3044 | $result = Str::of('bar fun bar fly')->matchAll('/f(\w*)/'); |
| 2860 | ||
| 3045 | ||
| 2861 | 3046 | // collect(['un', 'ly']); |
| 2862 | 3047 | |
| 2863 | 3048 | 如果没有找到任何匹配项,则返回空集合。 |
| 2864 | 3049 | |
| 2865 | 3050 | <a name="method-fluent-str-is-match"></a> |
| 3051 | ||
| 2866 | 3052 | #### `isMatch` |
| 2867 | 3053 | |
| 2868 | 3054 | `isMatch` 方法用于判断给定的字符串是否与正则表达式匹配: |
| 2869 | 3055 | |
| 2870 | 3056 | use Illuminate\Support\Str; |
| 2871 | ||
| 3057 | ||
| 2872 | 3058 | $result = Str::of('foo bar')->isMatch('/foo (.*)/'); |
| 2873 | ||
| 3059 | ||
| 2874 | 3060 | // true |
| 2875 | ||
| 3061 | ||
| 2876 | 3062 | $result = Str::of('laravel')->match('/foo (.*)/'); |
| 2877 | ||
| 3063 | ||
| 2878 | 3064 | // false |
| 2879 | 3065 | |
| 2880 | 3066 | <a name="method-fluent-str-new-line"></a> |
| 3067 | ||
| 2881 | 3068 | #### `newLine` |
| 2882 | 3069 | |
| 2883 | 3070 | `newLine` 方法将给字符串追加换行的字符: |
| 2884 | 3071 | |
| 2885 | 3072 | use Illuminate\Support\Str; |
| 2886 | ||
| 3073 | ||
| 2887 | 3074 | $padded = Str::of('Laravel')->newLine()->append('Framework'); |
| 2888 | ||
| 3075 | ||
| 2889 | 3076 | // 'Laravel |
| 2890 | 3077 | // Framework' |
| 2891 | 3078 | |
| 2892 | 3079 | <a name="method-fluent-str-padboth"></a> |
| 3080 | ||
| 2893 | 3081 | #### `padBoth` |
| 2894 | 3082 | |
| 2895 | `padBoth` | |
| 2896 |
| |
| 2897 | use Illuminate\Support\Str;
| |
| 2898 | ||
| 3083 | `padBoth` 方法包装了 PHP 的 `str_pad` 函数,在指定字符串的两侧填充上另一字符串,直至该字符串到达指定的长度: | |
| 3084 | ||
| 3085 | use Illuminate\Support\Str; | |
| 3086 | ||
| 2899 | 3087 | $padded = Str::of('James')->padBoth(10, '_'); |
| 2900 | ||
| 3088 | ||
| 2901 | 3089 | // '__James___' |
| 2902 | ||
| 3090 | ||
| 2903 | 3091 | $padded = Str::of('James')->padBoth(10); |
| 2904 | ||
| 3092 | ||
| 2905 | 3093 | // ' James ' |
| 2906 | 3094 | |
| 2907 | 3095 | |
| 2908 | 3096 | |
| 2909 | 3097 | <a name="method-fluent-str-padleft"></a> |
| 3098 | ||
| 2910 | 3099 | #### `padLeft` |
| 2911 | 3100 | |
| 2912 | The | |
| 2913 |
| |
| 2914 | use Illuminate\Support\Str;
| |
| 2915 | ||
| 3101 | The `padLeft` 方法包装了 PHP 的 `str_pad` 函数,在指定字符串的左侧填充上另一字符串,直至该字符串到达指定的长度: | |
| 3102 | ||
| 3103 | use Illuminate\Support\Str; | |
| 3104 | ||
| 2916 | 3105 | $padded = Str::of('James')->padLeft(10, '-='); |
| 2917 | ||
| 3106 | ||
| 2918 | 3107 | // '-=-=-James' |
| 2919 | ||
| 3108 | ||
| 2920 | 3109 | $padded = Str::of('James')->padLeft(10); |
| 2921 | ||
| 3110 | ||
| 2922 | 3111 | // ' James' |
| 2923 | 3112 | |
| 2924 | 3113 | <a name="method-fluent-str-padright"></a> |
| 3114 | ||
| 2925 | 3115 | #### `padRight` |
| 2926 | 3116 | |
| 2927 | `padRight` | |
| 2928 |
| |
| 2929 | use Illuminate\Support\Str;
| |
| 2930 | ||
| 3117 | `padRight` 方法包装了 PHP 的 `str_pad` 函数,在指定字符串的右侧填充上另一字符串,直至该字符串到达指定的长度: | |
| 3118 | ||
| 3119 | use Illuminate\Support\Str; | |
| 3120 | ||
| 2931 | 3121 | $padded = Str::of('James')->padRight(10, '-'); |
| 2932 | ||
| 3122 | ||
| 2933 | 3123 | // 'James-----' |
| 2934 | ||
| 3124 | ||
| 2935 | 3125 | $padded = Str::of('James')->padRight(10); |
| 2936 | ||
| 3126 | ||
| 2937 | 3127 | // 'James ' |
| 2938 | 3128 | |
| 2939 | 3129 | <a name="method-fluent-str-pipe"></a> |
| 3130 | ||
| 2940 | 3131 | #### `pipe` |
| 2941 | 3132 | |
| 2942 | 3133 | |
| 2943 | `pipe` | |
| 3134 | `pipe` 方法将把字符串的当前值传递给指定的函数来转换字符串: | |
| 2944 | 3135 | |
| 2945 | 3136 | use Illuminate\Support\Str; |
| 2946 | 3137 | use Illuminate\Support\Stringable; |
| 2947 | ||
| 3138 | ||
| 2948 | 3139 | $hash = Str::of('Laravel')->pipe('md5')->prepend('Checksum: '); |
| 2949 | ||
| 3140 | ||
| 2950 | 3141 | // 'Checksum: a5c95b86291ea299fcbe64458ed12702' |
| 2951 | ||
| 3142 | ||
| 2952 | 3143 | $closure = Str::of('foo')->pipe(function (Stringable $str) { |
| 2953 | 3144 | return 'bar'; |
| 2954 | 3145 | }); |
| 2955 | ||
| 3146 | ||
| 2956 | 3147 | // 'bar' |
| 2957 | 3148 | |
| 2958 | 3149 | <a name="method-fluent-str-plural"></a> |
| 3150 | ||
| 2959 | 3151 | #### `plural` |
| 2960 | 3152 | |
| 2961 | `plural` | |
| 2962 |
| |
| 2963 | use Illuminate\Support\Str;
| |
| 2964 | ||
| 3153 | `plural` 方法将单数形式的字符串转换为复数形式。该此函数支持 [Laravel的复数化器支持的任何语言](/docs/laravel/10.x/localization#pluralization-language) | |
| 3154 | ||
| 3155 | use Illuminate\Support\Str; | |
| 3156 | ||
| 2965 | 3157 | $plural = Str::of('car')->plural(); |
| 2966 | ||
| 3158 | ||
| 2967 | 3159 | // cars |
| 2968 | ||
| 3160 | ||
| 2969 | 3161 | $plural = Str::of('child')->plural(); |
| 2970 | ||
| 3162 | ||
| 2971 | 3163 | // children |
| 2972 | 3164 | |
| 2973 | 3165 | 你也可以给该函数提供一个整数作为第二个参数,用于检索字符串的单数或复数形式: |
| 2974 | 3166 | |
| 2975 | 3167 | use Illuminate\Support\Str; |
| 2976 | ||
| 3168 | ||
| 2977 | 3169 | $plural = Str::of('child')->plural(2); |
| 2978 | ||
| 3170 | ||
| 2979 | 3171 | // children |
| 2980 | ||
| 3172 | ||
| 2981 | 3173 | $plural = Str::of('child')->plural(1); |
| 2982 | ||
| 3174 | ||
| 2983 | 3175 | // child |
| 2984 | 3176 | |
| 2985 | 3177 | |
| 2986 | 3178 | |
| 2987 | 3179 | <a name="method-fluent-str-prepend"></a> |
| 3180 | ||
| 2988 | 3181 | #### `prepend` |
| 2989 | 3182 | |
| 2990 | `prepend` | |
| 2991 |
| |
| 2992 | use Illuminate\Support\Str;
| |
| 2993 | ||
| 3183 | `prepend` 方法用于在指定字符串的开头插入另一指定字符串: | |
| 3184 | ||
| 3185 | use Illuminate\Support\Str; | |
| 3186 | ||
| 2994 | 3187 | $string = Str::of('Framework')->prepend('Laravel '); |
| 2995 | ||
| 3188 | ||
| 2996 | 3189 | // Laravel Framework |
| 2997 | 3190 | |
| 2998 | 3191 | <a name="method-fluent-str-remove"></a> |
| 3192 | ||
| 2999 | 3193 | #### `remove` |
| 3000 | 3194 | |
| 3001 | `remove` | |
| 3002 |
| |
| 3003 | use Illuminate\Support\Str;
| |
| 3004 | ||
| 3195 | `remove` 方法用于从字符串中删除给定的值或值数组: | |
| 3196 | ||
| 3197 | use Illuminate\Support\Str; | |
| 3198 | ||
| 3005 | 3199 | $string = Str::of('Arkansas is quite beautiful!')->remove('quite'); |
| 3006 | ||
| 3200 | ||
| 3007 | 3201 | // Arkansas is beautiful! |
| 3008 | 3202 | |
| 3009 | 你也可以传递 | |
| 3203 | 你也可以传递 `false` 作为第二个参数以在删除字符串时忽略大小写。 | |
| 3010 | 3204 | |
| 3011 | 3205 | <a name="method-fluent-str-replace"></a> |
| 3206 | ||
| 3012 | 3207 | #### `replace` |
| 3013 | 3208 | |
| 3014 | `replace` | |
| 3015 |
| |
| 3016 | use Illuminate\Support\Str;
| |
| 3017 | ||
| 3209 | `replace` 方法用于将字符串中的指定字符串替换为另一指定字符串: | |
| 3210 | ||
| 3211 | use Illuminate\Support\Str; | |
| 3212 | ||
| 3018 | 3213 | $replaced = Str::of('Laravel 6.x')->replace('6.x', '7.x'); |
| 3019 | ||
| 3214 | ||
| 3020 | 3215 | // Laravel 7.x |
| 3021 | 3216 | |
| 3022 | 3217 | <a name="method-fluent-str-replace-array"></a> |
| 3218 | ||
| 3023 | 3219 | #### `replaceArray` |
| 3024 | 3220 | |
| 3025 | `replaceArray` | |
| 3026 |
| |
| 3027 | use Illuminate\Support\Str;
| |
| 3028 | ||
| 3221 | `replaceArray` 方法使用数组顺序替换字符串中的给定值: | |
| 3222 | ||
| 3223 | use Illuminate\Support\Str; | |
| 3224 | ||
| 3029 | 3225 | $string = 'The event will take place between ? and ?'; |
| 3030 | ||
| 3226 | ||
| 3031 | 3227 | $replaced = Str::of($string)->replaceArray('?', ['8:30', '9:00']); |
| 3032 | ||
| 3228 | ||
| 3033 | 3229 | // The event will take place between 8:30 and 9:00 |
| 3034 | 3230 | |
| 3035 | 3231 | <a name="method-fluent-str-replace-first"></a> |
| 3232 | ||
| 3036 | 3233 | #### `replaceFirst` |
| 3037 | 3234 | |
| 3038 | `replaceFirst` | |
| 3039 |
| |
| 3040 | use Illuminate\Support\Str;
| |
| 3041 | ||
| 3235 | `replaceFirst` 方法替换字符串中给定值的第一个匹配项: | |
| 3236 | ||
| 3237 | use Illuminate\Support\Str; | |
| 3238 | ||
| 3042 | 3239 | $replaced = Str::of('the quick brown fox jumps over the lazy dog')->replaceFirst('the', 'a'); |
| 3043 | ||
| 3240 | ||
| 3044 | 3241 | // a quick brown fox jumps over the lazy dog |
| 3045 | 3242 | |
| 3046 | 3243 | <a name="method-fluent-str-replace-last"></a> |
| 3244 | ||
| 3047 | 3245 | #### `replaceLast` |
| 3048 | 3246 | |
| 3049 | `replaceLast` | |
| 3050 |
| |
| 3051 | use Illuminate\Support\Str;
| |
| 3052 | ||
| 3247 | `replaceLast` 方法替换字符串中给定值的最后一个匹配项: | |
| 3248 | ||
| 3249 | use Illuminate\Support\Str; | |
| 3250 | ||
| 3053 | 3251 | $replaced = Str::of('the quick brown fox jumps over the lazy dog')->replaceLast('the', 'a'); |
| 3054 | ||
| 3252 | ||
| 3055 | 3253 | // the quick brown fox jumps over a lazy dog |
| 3056 | 3254 | |
| 3057 | 3255 | |
| 3058 | 3256 | |
| 3059 | 3257 | <a name="method-fluent-str-replace-matches"></a> |
| 3258 | ||
| 3060 | 3259 | #### `replaceMatches` |
| 3061 | 3260 | |
| 3062 | `replaceMatches` | |
| 3063 |
| |
| 3064 | use Illuminate\Support\Str;
| |
| 3065 | ||
| 3261 | `replaceMatches` 方法用给定的替换字符串替换与模式匹配的字符串的所有部分 | |
| 3262 | ||
| 3263 | use Illuminate\Support\Str; | |
| 3264 | ||
| 3066 | 3265 | $replaced = Str::of('(+1) 501-555-1000')->replaceMatches('/[^A-Za-z0-9]++/', '') |
| 3067 | ||
| 3266 | ||
| 3068 | 3267 | // '15015551000' |
| 3069 | 3268 | |
| 3070 | `replaceMatches` | |
| 3269 | `replaceMatches` 方法还接受一个闭包,该闭包将在字符串的每个部分与给定模式匹配时调用,从而允许你在闭包中执行替换逻辑并返回替换的值: | |
| 3071 | 3270 | |
| 3072 | 3271 | use Illuminate\Support\Str; |
| 3073 | 3272 | use Illuminate\Support\Stringable; |
| 3074 | ||
| 3273 | ||
| 3075 | 3274 | $replaced = Str::of('123')->replaceMatches('/\d/', function (Stringable $match) { |
| 3076 | 3275 | return '['.$match[0].']'; |
| 3077 | 3276 | }); |
| 3078 | ||
| 3277 | ||
| 3079 | 3278 | // '[1][2][3]' |
| 3080 | 3279 | |
| 3081 | 3280 | <a name="method-fluent-str-rtrim"></a> |
| 3281 | ||
| 3082 | 3282 | #### `rtrim` |
| 3083 | 3283 | |
| 3084 | 3284 | `rtrim` 方法修剪给定字符串的右侧: |
| 3085 | 3285 | |
| 3086 | 3286 | use Illuminate\Support\Str; |
| 3087 | ||
| 3287 | ||
| 3088 | 3288 | $string = Str::of(' Laravel ')->rtrim(); |
| 3089 | ||
| 3289 | ||
| 3090 | 3290 | // ' Laravel' |
| 3091 | ||
| 3291 | ||
| 3092 | 3292 | $string = Str::of('/Laravel/')->rtrim('/'); |
| 3093 | ||
| 3293 | ||
| 3094 | 3294 | // '/Laravel' |
| 3095 | 3295 | |
| 3096 | 3296 | <a name="method-fluent-str-scan"></a> |
| 3297 | ||
| 3097 | 3298 | #### `scan` |
| 3098 | 3299 | |
| 3099 | `scan` | |
| 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 | ||
| 3103 | 3304 | $collection = Str::of('filename.jpg')->scan('%[^.].%s'); |
| 3104 | ||
| 3305 | ||
| 3105 | 3306 | // collect(['filename', 'jpg']) |
| 3106 | 3307 | |
| 3107 | 3308 | <a name="method-fluent-str-singular"></a> |
| 3309 | ||
| 3108 | 3310 | #### `singular` |
| 3109 | 3311 | |
| 3110 | `singular` | |
| 3111 |
| |
| 3112 | use Illuminate\Support\Str;
| |
| 3113 | ||
| 3312 | `singular` 方法将字符串转换为其单数形式。此函数支持 [Laravel的复数化器支持的任何语言](/docs/laravel/10.x/localization#pluralization-language) : | |
| 3313 | ||
| 3314 | use Illuminate\Support\Str; | |
| 3315 | ||
| 3114 | 3316 | $singular = Str::of('cars')->singular(); |
| 3115 | ||
| 3317 | ||
| 3116 | 3318 | // car |
| 3117 | ||
| 3319 | ||
| 3118 | 3320 | $singular = Str::of('children')->singular(); |
| 3119 | ||
| 3321 | ||
| 3120 | 3322 | // child |
| 3121 | 3323 | |
| 3122 | 3324 | |
| 3123 | 3325 | |
| 3124 | 3326 | <a name="method-fluent-str-slug"></a> |
| 3327 | ||
| 3125 | 3328 | #### `slug` {.collection-method} |
| 3126 | 3329 | |
| 3127 | `slug` | |
| 3128 |
| |
| 3129 | use Illuminate\Support\Str;
| |
| 3130 | ||
| 3330 | `slug` 方法从给定字符串生成 URL 友好的 "slug": | |
| 3331 | ||
| 3332 | use Illuminate\Support\Str; | |
| 3333 | ||
| 3131 | 3334 | $slug = Str::of('Laravel Framework')->slug('-'); |
| 3132 | ||
| 3335 | ||
| 3133 | 3336 | // laravel-framework |
| 3134 | 3337 | |
| 3135 | 3338 | <a name="method-fluent-str-snake"></a> |
| 3339 | ||
| 3136 | 3340 | #### `snake` {.collection-method} |
| 3137 | 3341 | |
| 3138 | `snake` | |
| 3139 |
| |
| 3140 | use Illuminate\Support\Str;
| |
| 3141 | ||
| 3342 | `snake` 方法将给定字符串转换为 `snake_case` | |
| 3343 | ||
| 3344 | use Illuminate\Support\Str; | |
| 3345 | ||
| 3142 | 3346 | $converted = Str::of('fooBar')->snake(); |
| 3143 | ||
| 3347 | ||
| 3144 | 3348 | // foo_bar |
| 3145 | 3349 | |
| 3146 | 3350 | <a name="method-fluent-str-split"></a> |
| 3351 | ||
| 3147 | 3352 | #### `split` {.collection-method} |
| 3148 | 3353 | |
| 3149 | 3354 | split 方法使用正则表达式将字符串拆分为集合: |
| 3150 | 3355 | |
| 3151 | 3356 | use Illuminate\Support\Str; |
| 3152 | ||
| 3357 | ||
| 3153 | 3358 | $segments = Str::of('one, two, three')->split('/[\s,]+/'); |
| 3154 | ||
| 3359 | ||
| 3155 | 3360 | // collect(["one", "two", "three"]) |
| 3156 | 3361 | |
| 3157 | 3362 | <a name="method-fluent-str-squish"></a> |
| 3363 | ||
| 3158 | 3364 | #### `squish` {.collection-method} |
| 3159 | 3365 | |
| 3160 | 3366 | `squish` 方法删除字符串中所有无关紧要的空白,包括字符串之间的空白: |
| 3161 | 3367 | |
| 3162 | 3368 | use Illuminate\Support\Str; |
| 3163 | ||
| 3369 | ||
| 3164 | 3370 | $string = Str::of(' laravel framework ')->squish(); |
| 3165 | ||
| 3371 | ||
| 3166 | 3372 | // laravel framework |
| 3167 | 3373 | |
| 3168 | 3374 | <a name="method-fluent-str-start"></a> |
| 3375 | ||
| 3169 | 3376 | #### `start` {.collection-method} |
| 3170 | 3377 | |
| 3171 | 3378 | `start` 方法将给定值的单个实例添加到字符串中,前提是该字符串尚未以该值开头: |
| 3172 | 3379 | |
| 3173 | 3380 | use Illuminate\Support\Str; |
| 3174 | ||
| 3381 | ||
| 3175 | 3382 | $adjusted = Str::of('this/string')->start('/'); |
| 3176 | ||
| 3383 | ||
| 3177 | 3384 | // /this/string |
| 3178 | ||
| 3385 | ||
| 3179 | 3386 | $adjusted = Str::of('/this/string')->start('/'); |
| 3180 | ||
| 3387 | ||
| 3181 | 3388 | // /this/string |
| 3182 | 3389 | |
| 3183 | 3390 | <a name="method-fluent-str-starts-with"></a> |
| 3391 | ||
| 3184 | 3392 | #### `startsWith` {.collection-method} |
| 3185 | 3393 | |
| 3186 | 3394 | `startsWith` 方法确定给定字符串是否以给定值开头: |
| 3187 | 3395 | |
| 3188 | 3396 | use Illuminate\Support\Str; |
| 3189 | ||
| 3397 | ||
| 3190 | 3398 | $result = Str::of('This is my name')->startsWith('This'); |
| 3191 | ||
| 3399 | ||
| 3192 | 3400 | // true |
| 3193 | 3401 | |
| 3194 | 3402 | <a name="method-fluent-str-studly"></a> |
| 3403 | ||
| 3195 | 3404 | #### `studly` {.collection-method} |
| 3196 | 3405 | |
| 3197 | 3406 | `studly` 方法将给定字符串转换为 `StudlyCase`: |
| 3198 | 3407 | |
| 3199 | 3408 | use Illuminate\Support\Str; |
| 3200 | ||
| 3409 | ||
| 3201 | 3410 | $converted = Str::of('foo_bar')->studly(); |
| 3202 | ||
| 3411 | ||
| 3203 | 3412 | // FooBar |
| 3204 | 3413 | |
| 3205 | 3414 | |
| 3206 | 3415 | |
| 3207 | 3416 | <a name="method-fluent-str-substr"></a> |
| 3417 | ||
| 3208 | 3418 | #### `substr` {.collection-method} |
| 3209 | 3419 | |
| 3210 | 3420 | `substr` 方法返回由给定的起始参数和长度参数指定的字符串部分: |
| 3211 | 3421 | |
| 3212 | 3422 | use Illuminate\Support\Str; |
| 3213 | ||
| 3423 | ||
| 3214 | 3424 | $string = Str::of('Laravel Framework')->substr(8); |
| 3215 | ||
| 3425 | ||
| 3216 | 3426 | // Framework |
| 3217 | ||
| 3427 | ||
| 3218 | 3428 | $string = Str::of('Laravel Framework')->substr(8, 5); |
| 3219 | ||
| 3429 | ||
| 3220 | 3430 | // Frame |
| 3221 | 3431 | |
| 3222 | 3432 | <a name="method-fluent-str-substrreplace"></a> |
| 3433 | ||
| 3223 | 3434 | #### `substrReplace` {.collection-method} |
| 3224 | 3435 | |
| 3225 | `substrReplace` | |
| 3226 |
| |
| 3227 | use Illuminate\Support\Str;
| |
| 3228 | ||
| 3436 | `substrReplace` 方法在字符串的一部分中替换文本,从第二个参数指定的位置开始替换第三个参数指定的字符数。将 `0` 传递给方法的第三个参数将在指定位置插入字符串,而不替换字符串中的任何现有字符: | |
| 3437 | ||
| 3438 | use Illuminate\Support\Str; | |
| 3439 | ||
| 3229 | 3440 | $string = Str::of('1300')->substrReplace(':', 2); |
| 3230 | ||
| 3441 | ||
| 3231 | 3442 | // 13: |
| 3232 | ||
| 3443 | ||
| 3233 | 3444 | $string = Str::of('The Framework')->substrReplace(' Laravel', 3, 0); |
| 3234 | ||
| 3445 | ||
| 3235 | 3446 | // The Laravel Framework |
| 3236 | 3447 | |
| 3237 | 3448 | <a name="method-fluent-str-swap"></a> |
| 3449 | ||
| 3238 | 3450 | #### `swap` {.collection-method} |
| 3239 | 3451 | |
| 3240 | `swap` | |
| 3241 |
| |
| 3242 | use Illuminate\Support\Str;
| |
| 3243 | ||
| 3452 | `swap` 方法使用 PHP 的 `strtr` 函数替换字符串中的多个值: | |
| 3453 | ||
| 3454 | use Illuminate\Support\Str; | |
| 3455 | ||
| 3244 | 3456 | $string = Str::of('Tacos are great!') |
| 3245 | 3457 | ->swap([ |
| 3246 | 3458 | 'Tacos' => 'Burritos', |
| 3247 | 3459 | 'great' => 'fantastic', |
| 3248 | 3460 | ]); |
| 3249 | ||
| 3461 | ||
| 3250 | 3462 | // Burritos are fantastic! |
| 3251 | 3463 | |
| 3252 | 3464 | <a name="method-fluent-str-tap"></a> |
| 3465 | ||
| 3253 | 3466 | #### `tap` {.collection-method} |
| 3254 | 3467 | |
| 3255 | `tap` | |
| 3468 | `tap` 方法将字符串传递给给定的闭包,允许你在不影响字符串本身的情况下检查字符串并与之交互。`tap` 方法返回原始字符串,而不管闭包返回什么: | |
| 3256 | 3469 | |
| 3257 | 3470 | use Illuminate\Support\Str; |
| 3258 | 3471 | use Illuminate\Support\Stringable; |
| 3259 | ||
| 3472 | ||
| 3260 | 3473 | $string = Str::of('Laravel') |
| 3261 | 3474 | ->append(' Framework') |
| 3262 | 3475 | ->tap(function (Stringable $string) { |
| 3263 | 3476 | dump('String after append: '.$string); |
| 3264 | 3477 | }) |
| 3265 | 3478 | ->upper(); |
| 3266 | ||
| 3479 | ||
| 3267 | 3480 | // LARAVEL FRAMEWORK |
| 3268 | 3481 | |
| 3269 | 3482 | <a name="method-fluent-str-test"></a> | … | … |
| 3271 | 3484 | |
| 3272 | 3485 | #### `test` {.collection-method} |
| 3273 | 3486 | |
| 3274 | `test` | |
| 3275 |
| |
| 3276 | use Illuminate\Support\Str;
| |
| 3277 | ||
| 3487 | `test` 方法确定字符串是否与给定的正则表达式模式匹配: | |
| 3488 | ||
| 3489 | use Illuminate\Support\Str; | |
| 3490 | ||
| 3278 | 3491 | $result = Str::of('Laravel Framework')->test('/Laravel/'); |
| 3279 | ||
| 3492 | ||
| 3280 | 3493 | // true |
| 3281 | 3494 | |
| 3282 | 3495 | <a name="method-fluent-str-title"></a> |
| 3496 | ||
| 3283 | 3497 | #### `title` {.collection-method} |
| 3284 | 3498 | |
| 3285 | `title` | |
| 3286 |
| |
| 3287 | use Illuminate\Support\Str;
| |
| 3288 | ||
| 3499 | `title` 方法将给定字符串转换为 `title Case`: | |
| 3500 | ||
| 3501 | use Illuminate\Support\Str; | |
| 3502 | ||
| 3289 | 3503 | $converted = Str::of('a nice title uses the correct case')->title(); |
| 3290 | ||
| 3504 | ||
| 3291 | 3505 | // A Nice Title Uses The Correct Case |
| 3292 | 3506 | |
| 3293 | 3507 | <a name="method-fluent-str-trim"></a> |
| 3508 | ||
| 3294 | 3509 | #### `trim` {.collection-method} |
| 3295 | 3510 | |
| 3296 | `trim` | |
| 3297 |
| |
| 3298 | use Illuminate\Support\Str;
| |
| 3299 | ||
| 3511 | `trim` 方法修剪给定字符串: | |
| 3512 | ||
| 3513 | use Illuminate\Support\Str; | |
| 3514 | ||
| 3300 | 3515 | $string = Str::of(' Laravel ')->trim(); |
| 3301 | ||
| 3516 | ||
| 3302 | 3517 | // 'Laravel' |
| 3303 | ||
| 3518 | ||
| 3304 | 3519 | $string = Str::of('/Laravel/')->trim('/'); |
| 3305 | ||
| 3520 | ||
| 3306 | 3521 | // 'Laravel' |
| 3307 | 3522 | |
| 3308 | 3523 | <a name="method-fluent-str-ucfirst"></a> |
| 3524 | ||
| 3309 | 3525 | #### `ucfirst` {.collection-method} |
| 3310 | 3526 | |
| 3311 | `ucfirst` | |
| 3312 |
| |
| 3313 | use Illuminate\Support\Str;
| |
| 3314 | ||
| 3527 | `ucfirst` 方法返回第一个字符大写的给定字符串 | |
| 3528 | ||
| 3529 | use Illuminate\Support\Str; | |
| 3530 | ||
| 3315 | 3531 | $string = Str::of('foo bar')->ucfirst(); |
| 3316 | ||
| 3532 | ||
| 3317 | 3533 | // Foo bar |
| 3318 | 3534 | |
| 3319 | 3535 | <a name="method-fluent-str-ucsplit"></a> |
| 3536 | ||
| 3320 | 3537 | #### `ucsplit` {.collection-method} |
| 3321 | 3538 | |
| 3322 | 3539 | `ucsplit` 方法将给定的字符串按大写字符分割为一个集合: |
| 3323 | 3540 | |
| 3324 | 3541 | use Illuminate\Support\Str; |
| 3325 | ||
| 3542 | ||
| 3326 | 3543 | $string = Str::of('Foo Bar')->ucsplit(); |
| 3327 | ||
| 3544 | ||
| 3328 | 3545 | // collect(['Foo', 'Bar']) |
| 3329 | 3546 | |
| 3330 | 3547 | <a name="method-fluent-str-upper"></a> |
| 3548 | ||
| 3331 | 3549 | #### `upper` {.collection-method} |
| 3332 | 3550 | |
| 3333 | `upper` | |
| 3334 |
| |
| 3335 | use Illuminate\Support\Str;
| |
| 3336 | ||
| 3551 | `upper` 方法将给定字符串转换为大写: | |
| 3552 | ||
| 3553 | use Illuminate\Support\Str; | |
| 3554 | ||
| 3337 | 3555 | $adjusted = Str::of('laravel')->upper(); |
| 3338 | ||
| 3556 | ||
| 3339 | 3557 | // LARAVEL |
| 3340 | 3558 | |
| 3341 | 3559 | <a name="method-fluent-str-when"></a> |
| 3560 | ||
| 3342 | 3561 | #### `when` {.collection-method} |
| 3343 | 3562 | |
| 3344 | 如果给定的条件为 | |
| 3563 | 如果给定的条件为 `true`,则 `when` 方法调用给定的闭包。闭包将接收一个流畅字符串实例: | |
| 3345 | 3564 | |
| 3346 | 3565 | use Illuminate\Support\Str; |
| 3347 | 3566 | use Illuminate\Support\Stringable; |
| 3348 | ||
| 3567 | ||
| 3349 | 3568 | $string = Str::of('Taylor') |
| 3350 | 3569 | ->when(true, function (Stringable $string) { |
| 3351 | 3570 | return $string->append(' Otwell'); |
| 3352 | 3571 | }); |
| 3353 | ||
| 3572 | ||
| 3354 | 3573 | // 'Taylor Otwell' |
| 3355 | 如果需要,可以将另一个闭包作为第三个参数传递给 `when` 方法。如果条件参数的计算结果为 `false`,则将执行此闭包。 | |
| 3574 | ||
| 3575 | 如果需要,可以将另一个闭包作为第三个参数传递给 `when` 方法。如果条件参数的计算结果为 `false`,则将执行此闭包。 | |
| 3356 | 3576 | |
| 3357 | 3577 | |
| 3358 | 3578 | |
| 3359 | 3579 | <a name="method-fluent-str-when-contains"></a> |
| 3580 | ||
| 3360 | 3581 | #### `whenContains` {.collection-method} |
| 3361 | 3582 | |
| 3362 | `whenContains` | |
| 3583 | `whenContains` 方法会在字符串包含给定的值的前提下,调用给定的闭包。闭包将接收字符流处理实例: | |
| 3363 | 3584 | |
| 3364 | 3585 | use Illuminate\Support\Str; |
| 3365 | 3586 | use Illuminate\Support\Stringable; |
| 3366 | ||
| 3587 | ||
| 3367 | 3588 | $string = Str::of('tony stark') |
| 3368 | 3589 | ->whenContains('tony', function (Stringable $string) { |
| 3369 | 3590 | return $string->title(); |
| 3370 | 3591 | }); |
| 3371 | ||
| 3592 | ||
| 3372 | 3593 | // 'Tony Stark' |
| 3373 | 如有必要,你可以将另一个闭包作为第三个参数传递给 `whenContains` 方法。如果字符串不包含给定值,则此闭包将执行。 | |
| 3594 | ||
| 3595 | 如有必要,你可以将另一个闭包作为第三个参数传递给 `whenContains` 方法。如果字符串不包含给定值,则此闭包将执行。 | |
| 3374 | 3596 | |
| 3375 | 3597 | 你还可以传递一个值数组来确定给定的字符串是否包含数组中的任何值: |
| 3376 | 3598 | |
| 3377 | 3599 | use Illuminate\Support\Str; |
| 3378 | 3600 | use Illuminate\Support\Stringable; |
| 3379 | ||
| 3601 | ||
| 3380 | 3602 | $string = Str::of('tony stark') |
| 3381 | 3603 | ->whenContains(['tony', 'hulk'], function (Stringable $string) { |
| 3382 | 3604 | return $string->title(); |
| 3383 | 3605 | }); |
| 3384 | ||
| 3606 | ||
| 3385 | 3607 | // Tony Stark |
| 3386 | 3608 | |
| 3387 | 3609 | <a name="method-fluent-str-when-contains-all"></a> |
| 3610 | ||
| 3388 | 3611 | #### `whenContainsAll` {.collection-method} |
| 3389 | 3612 | |
| 3390 | `whenContainsAll` | |
| 3613 | `whenContainsAll` 方法会在字符串包含所有给定的子字符串时,调用给定的闭包。闭包将接收字符流处理实例: | |
| 3391 | 3614 | |
| 3392 | 3615 | use Illuminate\Support\Str; |
| 3393 | 3616 | use Illuminate\Support\Stringable; |
| 3394 | ||
| 3617 | ||
| 3395 | 3618 | $string = Str::of('tony stark') |
| 3396 | 3619 | ->whenContainsAll(['tony', 'stark'], function (Stringable $string) { |
| 3397 | 3620 | return $string->title(); |
| 3398 | 3621 | }); |
| 3399 | ||
| 3622 | ||
| 3400 | 3623 | // 'Tony Stark' |
| 3401 | 3624 | |
| 3402 | 如有必要,你可以将另一个闭包作为第三个参数传递给 | |
| 3625 | 如有必要,你可以将另一个闭包作为第三个参数传递给 `whenContainsAll` 方法。如果条件参数评估为 `false`,则此闭包将执行。 | |
| 3403 | 3626 | |
| 3404 | 3627 | <a name="method-fluent-str-when-empty"></a> |
| 3628 | ||
| 3405 | 3629 | #### `whenEmpty` {.collection-method} |
| 3406 | 3630 | |
| 3407 | 如果字符串为空,`whenEmpty` | |
| 3631 | 如果字符串为空,`whenEmpty` 方法将调用给定的闭包。如果闭包返回一个值,`whenEmpty` 方法也将返回该值。如果闭包不返回值,则将返回字符流处理实例: | |
| 3408 | 3632 | |
| 3409 | 3633 | use Illuminate\Support\Str; |
| 3410 | 3634 | use Illuminate\Support\Stringable; |
| 3411 | ||
| 3635 | ||
| 3412 | 3636 | $string = Str::of(' ')->whenEmpty(function (Stringable $string) { |
| 3413 | 3637 | return $string->trim()->prepend('Laravel'); |
| 3414 | 3638 | }); |
| 3415 | ||
| 3639 | ||
| 3416 | 3640 | // 'Laravel' |
| 3417 | 3641 | |
| 3418 | 3642 | |
| 3419 | 3643 | |
| 3420 | 3644 | <a name="method-fluent-str-when-not-empty"></a> |
| 3645 | ||
| 3421 | 3646 | #### `whenNotEmpty` {.collection-method} |
| 3422 | 3647 | |
| 3423 | 如果字符串不为空,`whenNotEmpty` | |
| 3648 | 如果字符串不为空,`whenNotEmpty` 方法会调用给定的闭包。如果闭包返回一个值,那么 `whenNotEmpty` 方法也将返回该值。如果闭包没有返回值,则返回字符流处理实例: | |
| 3424 | 3649 | |
| 3425 | 3650 | use Illuminate\Support\Str; |
| 3426 | 3651 | use Illuminate\Support\Stringable; |
| 3427 | ||
| 3652 | ||
| 3428 | 3653 | $string = Str::of('Framework')->whenNotEmpty(function (Stringable $string) { |
| 3429 | 3654 | return $string->prepend('Laravel '); |
| 3430 | 3655 | }); |
| 3431 | ||
| 3656 | ||
| 3432 | 3657 | // 'Laravel Framework' |
| 3433 | 3658 | |
| 3434 | 3659 | <a name="method-fluent-str-when-starts-with"></a> |
| 3660 | ||
| 3435 | 3661 | #### `whenStartsWith` {.collection-method} |
| 3436 | 3662 | |
| 3437 | 如果字符串以给定的子字符串开头,`whenStartsWith` | |
| 3663 | 如果字符串以给定的子字符串开头,`whenStartsWith` 方法会调用给定的闭包。闭包将接收字符流处理实例: | |
| 3438 | 3664 | |
| 3439 | 3665 | use Illuminate\Support\Str; |
| 3440 | 3666 | use Illuminate\Support\Stringable; |
| 3441 | ||
| 3667 | ||
| 3442 | 3668 | $string = Str::of('disney world')->whenStartsWith('disney', function (Stringable $string) { |
| 3443 | 3669 | return $string->title(); |
| 3444 | 3670 | }); |
| 3445 | ||
| 3671 | ||
| 3446 | 3672 | // 'Disney World' |
| 3447 | 3673 | |
| 3448 | 3674 | <a name="method-fluent-str-when-ends-with"></a> |
| 3675 | ||
| 3449 | 3676 | #### `whenEndsWith` {.collection-method} |
| 3450 | 3677 | |
| 3451 | 如果字符串以给定的子字符串结尾,`whenEndsWith` | |
| 3678 | 如果字符串以给定的子字符串结尾,`whenEndsWith` 方法会调用给定的闭包。闭包将接收字符流处理实例: | |
| 3452 | 3679 | |
| 3453 | 3680 | use Illuminate\Support\Str; |
| 3454 | 3681 | use Illuminate\Support\Stringable; |
| 3455 | ||
| 3682 | ||
| 3456 | 3683 | $string = Str::of('disney world')->whenEndsWith('world', function (Stringable $string) { |
| 3457 | 3684 | return $string->title(); |
| 3458 | 3685 | }); |
| 3459 | ||
| 3686 | ||
| 3460 | 3687 | // 'Disney World' |
| 3461 | 3688 | |
| 3462 | 3689 | <a name="method-fluent-str-when-exactly"></a> |
| 3690 | ||
| 3463 | 3691 | #### `whenExactly` {.collection-method} |
| 3464 | 3692 | |
| 3465 | 如果字符串与给定字符串完全匹配,`whenExactly` | |
| 3693 | 如果字符串与给定字符串完全匹配,`whenExactly` 方法会调用给定的闭包。闭包将接收字符流处理实例: | |
| 3466 | 3694 | |
| 3467 | 3695 | use Illuminate\Support\Str; |
| 3468 | 3696 | use Illuminate\Support\Stringable; |
| 3469 | ||
| 3697 | ||
| 3470 | 3698 | $string = Str::of('laravel')->whenExactly('laravel', function (Stringable $string) { |
| 3471 | 3699 | return $string->title(); |
| 3472 | 3700 | }); |
| 3473 | ||
| 3701 | ||
| 3474 | 3702 | // 'Laravel' |
| 3475 | 3703 | |
| 3476 | 3704 | <a name="method-fluent-str-when-not-exactly"></a> |
| 3705 | ||
| 3477 | 3706 | #### `whenNotExactly` {.collection-method} |
| 3478 | 3707 | |
| 3479 | 3708 | 如果字符串与给定字符串不完全匹配,`whenNotExactly`方法将调用给定的闭包。闭包将接收字符流处理实例: |
| 3480 | 3709 | |
| 3481 | 3710 | use Illuminate\Support\Str; |
| 3482 | 3711 | use Illuminate\Support\Stringable; |
| 3483 | ||
| 3712 | ||
| 3484 | 3713 | $string = Str::of('framework')->whenNotExactly('laravel', function (Stringable $string) { |
| 3485 | 3714 | return $string->title(); |
| 3486 | 3715 | }); |
| 3487 | ||
| 3716 | ||
| 3488 | 3717 | // 'Framework' |
| 3489 | 3718 | |
| 3490 | 3719 | |
| 3491 | 3720 | |
| 3492 | 3721 | <a name="method-fluent-str-when-is"></a> |
| 3722 | ||
| 3493 | 3723 | #### `whenIs` {.collection-method} |
| 3494 | 3724 | |
| 3495 | 如果字符串匹配给定的模式,`whenIs` | |
| 3725 | 如果字符串匹配给定的模式,`whenIs` 方法会调用给定的闭包。星号可用作通配符值。闭包将接收字符流处理实例: | |
| 3496 | 3726 | |
| 3497 | 3727 | use Illuminate\Support\Str; |
| 3498 | 3728 | use Illuminate\Support\Stringable; |
| 3499 | ||
| 3729 | ||
| 3500 | 3730 | $string = Str::of('foo/bar')->whenIs('foo/*', function (Stringable $string) { |
| 3501 | 3731 | return $string->append('/baz'); |
| 3502 | 3732 | }); |
| 3503 | ||
| 3733 | ||
| 3504 | 3734 | // 'foo/bar/baz' |
| 3505 | 3735 | |
| 3506 | 3736 | <a name="method-fluent-str-when-is-ascii"></a> |
| 3737 | ||
| 3507 | 3738 | #### `whenIsAscii` {.collection-method} |
| 3508 | 3739 | |
| 3509 | 如果字符串是 7 位 ASCII,`whenIsAscii` | |
| 3740 | 如果字符串是 7 位 ASCII,`whenIsAscii` 方法会调用给定的闭包。闭包将接收字符流处理实例: | |
| 3510 | 3741 | |
| 3511 | 3742 | use Illuminate\Support\Str; |
| 3512 | 3743 | use Illuminate\Support\Stringable; |
| 3513 | ||
| 3744 | ||
| 3514 | 3745 | $string = Str::of('laravel')->whenIsAscii(function (Stringable $string) { |
| 3515 | 3746 | return $string->title(); |
| 3516 | 3747 | }); |
| 3517 | ||
| 3748 | ||
| 3518 | 3749 | // 'Laravel' |
| 3519 | 3750 | |
| 3520 | 3751 | <a name="method-fluent-str-when-is-ulid"></a> |
| 3752 | ||
| 3521 | 3753 | #### `whenIsUlid` {.collection-method} |
| 3522 | 3754 | |
| 3523 | 如果字符串是有效的 ULID,`whenIsUlid` | |
| 3524 |
| |
| 3525 | use Illuminate\Support\Str;
| |
| 3526 | ||
| 3755 | 如果字符串是有效的 ULID,`whenIsUlid` 方法会调用给定的闭包。闭包将接收字符流处理实例: | |
| 3756 | ||
| 3757 | use Illuminate\Support\Str; | |
| 3758 | ||
| 3527 | 3759 | $string = Str::of('01gd6r360bp37zj17nxb55yv40')->whenIsUlid(function (Stringable $string) { |
| 3528 | 3760 | return $string->substr(0, 8); |
| 3529 | 3761 | }); |
| 3530 | ||
| 3762 | ||
| 3531 | 3763 | // '01gd6r36' |
| 3532 | 3764 | |
| 3533 | 3765 | <a name="method-fluent-str-when-is-uuid"></a> |
| 3766 | ||
| 3534 | 3767 | #### `whenIsUuid` {.collection-method} |
| 3535 | 3768 | |
| 3536 | 如果字符串是有效的 UUID,`whenIsUuid` | |
| 3769 | 如果字符串是有效的 UUID,`whenIsUuid` 方法会调用给定的闭包。闭包将接收字符流处理实例: | |
| 3537 | 3770 | |
| 3538 | 3771 | use Illuminate\Support\Str; |
| 3539 | 3772 | use Illuminate\Support\Stringable; |
| 3540 | ||
| 3773 | ||
| 3541 | 3774 | $string = Str::of('a0a2a2d2-0b87-4a18-83f2-2529882be2de')->whenIsUuid(function (Stringable $string) { |
| 3542 | 3775 | return $string->substr(0, 8); |
| 3543 | 3776 | }); |
| 3544 | ||
| 3777 | ||
| 3545 | 3778 | // 'a0a2a2d2' |
| 3546 | 3779 | |
| 3547 | 3780 | <a name="method-fluent-str-when-test"></a> |
| 3781 | ||
| 3548 | 3782 | #### `whenTest` {.collection-method} |
| 3549 | 3783 | |
| 3550 | 如果字符串匹配给定的正则表达式,`whenTest` | |
| 3784 | 如果字符串匹配给定的正则表达式,`whenTest` 方法会调用给定的闭包。闭包将接收字符流处理实例: | |
| 3551 | 3785 | |
| 3552 | 3786 | use Illuminate\Support\Str; |
| 3553 | 3787 | use Illuminate\Support\Stringable; |
| 3554 | ||
| 3788 | ||
| 3555 | 3789 | $string = Str::of('laravel framework')->whenTest('/laravel/', function (Stringable $string) { |
| 3556 | 3790 | return $string->title(); |
| 3557 | 3791 | }); |
| 3558 | ||
| 3792 | ||
| 3559 | 3793 | // 'Laravel Framework' |
| 3560 | 3794 | |
| 3561 | 3795 | |
| 3562 | 3796 | |
| 3563 | 3797 | <a name="method-fluent-str-word-count"></a> |
| 3798 | ||
| 3564 | 3799 | #### `wordCount` {.collection-method} |
| 3565 | 3800 | |
| 3566 | `wordCount` | |
| 3801 | `wordCount` 方法返回字符串包含的单词数: | |
| 3567 | 3802 | |
| 3568 | 3803 | ```php |
| 3569 | 3804 | use Illuminate\Support\Str; | … | … |
| 3572 | 3807 | ``` |
| 3573 | 3808 | |
| 3574 | 3809 | <a name="method-fluent-str-words"></a> |
| 3810 | ||
| 3575 | 3811 | #### `words` {.collection-method} |
| 3576 | 3812 | |
| 3577 | `words` | |
| 3578 |
| |
| 3579 | use Illuminate\Support\Str;
| |
| 3580 | ||
| 3813 | `words` 方法限制字符串中的字数。如有必要,可以指定附加到截断字符串的附加字符串: | |
| 3814 | ||
| 3815 | use Illuminate\Support\Str; | |
| 3816 | ||
| 3581 | 3817 | $string = Str::of('Perfectly balanced, as all things should be.')->words(3, ' >>>'); |
| 3582 | ||
| 3818 | ||
| 3583 | 3819 | // Perfectly balanced, as >>> |
| 3584 | 3820 | |
| 3585 | 3821 | <a name="urls"></a> |
| 3822 | ||
| 3586 | 3823 | ## URLs |
| 3587 | 3824 | |
| 3588 | 3825 | <a name="method-action"></a> |
| 3826 | ||
| 3589 | 3827 | #### `action()` {.collection-method} |
| 3590 | 3828 | |
| 3591 | `action` | |
| 3829 | `action` 函数为给定的控制器操作生成 URL: | |
| 3592 | 3830 | |
| 3593 | 3831 | use App\Http\Controllers\HomeController; |
| 3594 | ||
| 3832 | ||
| 3595 | 3833 | $url = action([HomeController::class, 'index']); |
| 3596 | 3834 | |
| 3597 | 3835 | 如果该方法接受路由参数,则可以将它们作为第二个参数传递给该方法: | … | … |
| 3599 | 3837 | $url = action([UserController::class, 'profile'], ['id' => 1]); |
| 3600 | 3838 | |
| 3601 | 3839 | <a name="method-asset"></a> |
| 3840 | ||
| 3602 | 3841 | #### `asset()` {.collection-method} |
| 3603 | 3842 | |
| 3604 | `asset` | |
| 3843 | `asset` 函数使用请求的当前方案(HTTP 或 HTTPS)生成 URL: | |
| 3605 | 3844 | |
| 3606 | 3845 | $url = asset('img/photo.jpg'); |
| 3607 | 3846 | |
| 3608 | 你可以通过在`.env` | |
| 3847 | 你可以通过在`.env` 文件中设置 `ASSET_URL` 变量来配置资产 URL 主机。如果你将资产托管在外部服务(如 Amazon S3 或其他 CDN)上,这将非常有用: | |
| 3609 | 3848 | |
| 3610 | 3849 | // ASSET_URL=http://example.com/assets |
| 3611 | ||
| 3850 | ||
| 3612 | 3851 | $url = asset('img/photo.jpg'); // http://example.com/assets/img/photo.jpg |
| 3613 | 3852 | |
| 3614 | 3853 | <a name="method-route"></a> |
| 3854 | ||
| 3615 | 3855 | #### `route()` {.collection-method} |
| 3616 | 3856 | |
| 3617 | `route` | |
| 3857 | `route` 函数为给定的 [命名路由](/docs/laravel/10.x/routing#named-routes): | |
| 3618 | 3858 | |
| 3619 | 3859 | $url = route('route.name'); |
| 3620 | 3860 | … | … |
| 3629 | 3869 | $url = route('route.name', ['id' => 1], false); |
| 3630 | 3870 | |
| 3631 | 3871 | <a name="method-secure-asset"></a> |
| 3872 | ||
| 3632 | 3873 | #### `secure_asset()` {.collection-method} |
| 3633 | 3874 | |
| 3634 | 3875 | `secure_asset` 函数使用 HTTPS 为静态资源生成 URL: | … | … |
| 3636 | 3877 | $url = secure_asset('img/photo.jpg'); |
| 3637 | 3878 | |
| 3638 | 3879 | <a name="method-secure-url"></a> |
| 3880 | ||
| 3639 | 3881 | #### `secure_url()` {.collection-method} |
| 3640 | 3882 | |
| 3641 | 3883 | `secure_url` 函数生成给定路径的完全限定 HTTPS URL。 可以在函数的第二个参数中传递额外的 URL 段: |
| 3642 | 3884 | |
| 3643 | 3885 | $url = secure_url('user/profile'); |
| 3644 | ||
| 3886 | ||
| 3645 | 3887 | $url = secure_url('user/profile', [1]); |
| 3646 | 3888 | |
| 3647 | 3889 | <a name="method-to-route"></a> |
| 3890 | ||
| 3648 | 3891 | #### `to_route()` {.collection-method} |
| 3649 | 3892 | |
| 3650 | 3893 | `to_route` 函数为给定的[命名路由](/docs/laravel/10.x/routing#named-routes) 生成一个[重定向 HTTP 响应](/docs/laravel/10.x/responses#redirects): | … | … |
| 3656 | 3899 | return to_route('users.show', ['user' => 1], 302, ['X-Framework' => 'Laravel']); |
| 3657 | 3900 | |
| 3658 | 3901 | <a name="method-url"></a> |
| 3902 | ||
| 3659 | 3903 | #### `url()` {.collection-method} |
| 3660 | 3904 | |
| 3661 | 3905 | `url` 函数生成给定路径的完全限定 URL: |
| 3662 | 3906 | |
| 3663 | 3907 | $url = url('user/profile'); |
| 3664 | ||
| 3908 | ||
| 3665 | 3909 | $url = url('user/profile', [1]); |
| 3666 | 3910 | |
| 3667 | 3911 | 如果未提供路径,则返回一个 `Illuminate\Routing\UrlGenerator` 实例: |
| 3668 | 3912 | |
| 3669 | 3913 | $current = url()->current(); |
| 3670 | ||
| 3914 | ||
| 3671 | 3915 | $full = url()->full(); |
| 3672 | ||
| 3916 | ||
| 3673 | 3917 | $previous = url()->previous(); |
| 3674 | 3918 | |
| 3675 | 3919 | <a name="miscellaneous"></a> |
| 3920 | ||
| 3676 | 3921 | ## 杂项 |
| 3677 | 3922 | |
| 3678 | 3923 | <a name="method-abort"></a> |
| 3924 | ||
| 3679 | 3925 | #### `abort()` {.collection-method} |
| 3680 | 3926 | |
| 3681 | 3927 | 使用`abort` 函数抛出一个[HTTP 异常](/docs/laravel/10.x/errors#http-exceptions)交给[异常处理](/docs/laravel/10.x/errors#the-exception-handler "异常处理程序") | … | … |
| 3689 | 3935 | abort(403, 'Unauthorized.', $headers); |
| 3690 | 3936 | |
| 3691 | 3937 | <a name="method-abort-if"></a> |
| 3938 | ||
| 3692 | 3939 | #### `abort_if()` {.collection-method} |
| 3693 | 3940 | |
| 3694 | 3941 | 如果给定的布尔表达式的计算结果为 `true`,则 `abort_if` 函数会抛出 HTTP 异常: | … | … |
| 3698 | 3945 | 与 `abort` 方法一样,你还可以提供异常的响应文本作为函数的第三个参数,并提供自定义响应标头数组作为函数的第四个参数。 |
| 3699 | 3946 | |
| 3700 | 3947 | <a name="method-abort-unless"></a> |
| 3948 | ||
| 3701 | 3949 | #### `abort_unless()` {.collection-method} |
| 3702 | 3950 | |
| 3703 | 3951 | 如果给定的布尔表达式的计算结果为 `false`,则 `abort_unless` 函数会抛出 HTTP 异常: | … | … |
| 3707 | 3955 | 与 `abort` 方法一样,你还可以提供异常的响应文本作为函数的第三个参数,并提供自定义响应标头数组作为函数的第四个参数。 |
| 3708 | 3956 | |
| 3709 | 3957 | <a name="method-app"></a> |
| 3958 | ||
| 3710 | 3959 | #### `app()` {.collection-method} |
| 3711 | 3960 | |
| 3712 | 3961 | `app` 函数返回 [服务容器](/docs/laravel/10.x/container) 实例: | … | … |
| 3718 | 3967 | $api = app('HelpSpot\API'); |
| 3719 | 3968 | |
| 3720 | 3969 | <a name="method-auth"></a> |
| 3970 | ||
| 3721 | 3971 | #### `auth()` {.collection-method} |
| 3722 | 3972 | |
| 3723 | 3973 | `auth` 函数返回一个 [authenticator](/docs/laravel/10.x/authentication) 实例。 你可以将它用作 `Auth` 门面的替代品: | … | … |
| 3729 | 3979 | $user = auth('admin')->user(); |
| 3730 | 3980 | |
| 3731 | 3981 | <a name="method-back"></a> |
| 3982 | ||
| 3732 | 3983 | #### `back()` {.collection-method} |
| 3733 | 3984 | |
| 3734 | 3985 | `back` 函数生成一个 [重定向 HTTP 响应](/docs/laravel/10.x/responses#redirects) 到用户之前的位置: |
| 3735 | 3986 | |
| 3736 | 3987 | return back($status = 302, $headers = [], $fallback = '/'); |
| 3737 | ||
| 3988 | ||
| 3738 | 3989 | return back(); |
| 3739 | 3990 | |
| 3740 | 3991 | |
| 3741 | 3992 | |
| 3742 | 3993 | <a name="method-bcrypt"></a> |
| 3994 | ||
| 3743 | 3995 | #### `bcrypt()` {.collection-method} |
| 3744 | 3996 | |
| 3745 | 3997 | `bcrypt` 函数 [hashes](/docs/laravel/10.x/hashing) 使用 Bcrypt 的给定值。 您可以使用此函数作为 `Hash` 门面的替代方法: | … | … |
| 3747 | 3999 | $password = bcrypt('my-secret-password'); |
| 3748 | 4000 | |
| 3749 | 4001 | <a name="method-blank"></a> |
| 4002 | ||
| 3750 | 4003 | #### `blank()` {.collection-method} |
| 3751 | 4004 | |
| 3752 | 4005 | `blank` 函数确定给定值是否为「空白」: | … | … |
| 3755 | 4008 | blank(' '); |
| 3756 | 4009 | blank(null); |
| 3757 | 4010 | blank(collect()); |
| 3758 | ||
| 4011 | ||
| 3759 | 4012 | // true |
| 3760 | ||
| 4013 | ||
| 3761 | 4014 | blank(0); |
| 3762 | 4015 | blank(true); |
| 3763 | 4016 | blank(false); |
| 3764 | ||
| 4017 | ||
| 3765 | 4018 | // false |
| 3766 | 4019 | |
| 3767 | 4020 | 对于 `blank` 的反转,请参阅 [`filled`](#method-filled) 方法。 |
| 3768 | 4021 | |
| 3769 | 4022 | <a name="method-broadcast"></a> |
| 4023 | ||
| 3770 | 4024 | #### `broadcast()` {.collection-method} |
| 3771 | 4025 | |
| 3772 | 4026 | `broadcast` 函数 [broadcasts](/docs/laravel/10.x/broadcasting) 给定的 [event](/docs/laravel/10.x/events) 给它的听众: |
| 3773 | 4027 | |
| 3774 | 4028 | broadcast(new UserRegistered($user)); |
| 3775 | ||
| 4029 | ||
| 3776 | 4030 | broadcast(new UserRegistered($user))->toOthers(); |
| 3777 | 4031 | |
| 3778 | 4032 | <a name="method-cache"></a> |
| 4033 | ||
| 3779 | 4034 | #### `cache()` {.collection-method} |
| 3780 | 4035 | |
| 3781 | 4036 | `cache` 函数可用于从 [cache](/docs/laravel/10.x/cache) 中获取值。 如果缓存中不存在给定的键,将返回一个可选的默认值: |
| 3782 | 4037 | |
| 3783 | 4038 | $value = cache('key'); |
| 3784 | ||
| 4039 | ||
| 3785 | 4040 | $value = cache('key', 'default'); |
| 3786 | 4041 | |
| 3787 | 4042 | 你可以通过将键/值对数组传递给函数来将项目添加到缓存中。 你应该传递缓存值应被视为有效的秒数或持续时间: |
| 3788 | 4043 | |
| 3789 | 4044 | cache(['key' => 'value'], 300); |
| 3790 | ||
| 4045 | ||
| 3791 | 4046 | cache(['key' => 'value'], now()->addSeconds(10)); |
| 3792 | 4047 | |
| 3793 | 4048 | <a name="method-class-uses-recursive"></a> |
| 4049 | ||
| 3794 | 4050 | #### `class_uses_recursive()` {.collection-method} |
| 3795 | 4051 | |
| 3796 | 4052 | `class_uses_recursive` 函数返回一个类使用的所有特征,包括其所有父类使用的特征: | … | … |
| 3798 | 4054 | $traits = class_uses_recursive(App\Models\User::class); |
| 3799 | 4055 | |
| 3800 | 4056 | <a name="method-collect"></a> |
| 4057 | ||
| 3801 | 4058 | #### `collect()` {.collection-method} |
| 3802 | 4059 | |
| 3803 | 4060 | `collect` 函数根据给定值创建一个 [collection](/docs/laravel/10.x/collections) 实例: | … | … |
| 3807 | 4064 | |
| 3808 | 4065 | |
| 3809 | 4066 | <a name="method-config"></a> |
| 4067 | ||
| 3810 | 4068 | #### `config()` {.collection-method} |
| 3811 | 4069 | |
| 3812 | 4070 | `config` 函数获取 [configuration](/docs/laravel/10.x/configuration) 变量的值。 可以使用「点」语法访问配置值,其中包括文件名和你希望访问的选项。 如果配置选项不存在,可以指定默认值并返回: |
| 3813 | 4071 | |
| 3814 | 4072 | $value = config('app.timezone'); |
| 3815 | ||
| 4073 | ||
| 3816 | 4074 | $value = config('app.timezone', $default); |
| 3817 | 4075 | |
| 3818 | 4076 | 你可以通过传递键/值对数组在运行时设置配置变量。 但是请注意,此函数只会影响当前请求的配置值,不会更新您的实际配置值: | … | … |
| 3820 | 4078 | config(['app.debug' => true]); |
| 3821 | 4079 | |
| 3822 | 4080 | <a name="method-cookie"></a> |
| 4081 | ||
| 3823 | 4082 | #### `cookie()` {.collection-method} |
| 3824 | 4083 | |
| 3825 | 4084 | `cookie` 函数创建一个新的 [cookie](/docs/laravel/10.x/requests#cookies) 实例: | … | … |
| 3827 | 4086 | $cookie = cookie('name', 'value', $minutes); |
| 3828 | 4087 | |
| 3829 | 4088 | <a name="method-csrf-field"></a> |
| 4089 | ||
| 3830 | 4090 | #### `csrf_field()` {.collection-method} |
| 3831 | 4091 | |
| 3832 | 4092 | `csrf_field` 函数生成一个 HTML `hidden` 输入字段,其中包含 CSRF 令牌的值。 例如,使用 [Blade 语法](/docs/laravel/10.x/blade): | … | … |
| 3834 | 4094 | {{ csrf_field() }} |
| 3835 | 4095 | |
| 3836 | 4096 | <a name="method-csrf-token"></a> |
| 4097 | ||
| 3837 | 4098 | #### `csrf_token()` {.collection-method} |
| 3838 | 4099 | |
| 3839 | 4100 | `csrf_token` 函数检索当前 CSRF 令牌的值: | … | … |
| 3841 | 4102 | $token = csrf_token(); |
| 3842 | 4103 | |
| 3843 | 4104 | <a name="method-decrypt"></a> |
| 4105 | ||
| 3844 | 4106 | #### `decrypt()` {.collection-method} |
| 3845 | 4107 | |
| 3846 | 4108 | `decrypt` 函数 [解密](/docs/laravel/10.x/encryption) 给定的值。 你可以使用此函数作为 `Crypt` 门面的替代方法: | … | … |
| 3848 | 4110 | $password = decrypt($value); |
| 3849 | 4111 | |
| 3850 | 4112 | <a name="method-dd"></a> |
| 4113 | ||
| 3851 | 4114 | #### `dd()` {.collection-method} |
| 3852 | 4115 | |
| 3853 | 4116 | `dd` 函数转储给定的变量并结束脚本的执行: |
| 3854 | 4117 | |
| 3855 | 4118 | dd($value); |
| 3856 | ||
| 4119 | ||
| 3857 | 4120 | dd($value1, $value2, $value3, ...); |
| 3858 | 4121 | |
| 3859 | 4122 | 如果你不想停止脚本的执行,请改用 [`dump`](#method-dump) 函数。 |
| 3860 | 4123 | |
| 3861 | 4124 | |
| 3862 | 4125 | <a name="method-dispatch"></a> |
| 4126 | ||
| 3863 | 4127 | #### `dispatch()` {.collection-method} |
| 3864 | 4128 | |
| 3865 | 4129 | `dispatch` 函数将给定的 [job](/docs/laravel/10.x/queues#creating-jobs) 推送到 Laravel [job queue](/docs/laravel/10.x/queues): | … | … |
| 3867 | 4131 | dispatch(new App\Jobs\SendEmails); |
| 3868 | 4132 | |
| 3869 | 4133 | <a name="method-dump"></a> |
| 4134 | ||
| 3870 | 4135 | #### `dump()` {.collection-method} |
| 3871 | 4136 | |
| 3872 | 4137 | `dump` 函数转储给定的变量: |
| 3873 | 4138 | |
| 3874 | 4139 | dump($value); |
| 3875 | ||
| 4140 | ||
| 3876 | 4141 | dump($value1, $value2, $value3, ...); |
| 3877 | 4142 | |
| 3878 | 4143 | 如果要在转储变量后停止执行脚本,请改用 [`dd`](#method-dd) 函数。 |
| 3879 | 4144 | |
| 3880 | 4145 | <a name="method-encrypt"></a> |
| 4146 | ||
| 3881 | 4147 | #### `encrypt()` {.collection-method} |
| 3882 | 4148 | |
| 3883 | 4149 | `encrypt` 函数 [encrypts](/docs/laravel/10.x/encryption) 给定值。 你可以使用此函数作为 `Crypt` 门面的替代方法: | … | … |
| 3885 | 4151 | $secret = encrypt('my-secret-value'); |
| 3886 | 4152 | |
| 3887 | 4153 | <a name="method-env"></a> |
| 4154 | ||
| 3888 | 4155 | #### `env()` {.collection-method} |
| 3889 | 4156 | |
| 3890 | 4157 | `env` 函数检索 [环境变量](/docs/laravel/10.x/configuration#environment-configuration) 的值或返回默认值: |
| 3891 | 4158 | |
| 3892 | 4159 | $env = env('APP_ENV'); |
| 3893 | ||
| 4160 | ||
| 3894 | 4161 | $env = env('APP_ENV', 'production'); |
| 3895 | 4162 | |
| 3896 | 4163 | > **警告** |
| 3897 | 4164 | > 如果你在部署过程中执行 `config:cache` 命令,你应该确保只从配置文件中调用 `env` 函数。 一旦配置被缓存,`.env` 文件将不会被加载,所有对 `env` 函数的调用都将返回 `null`。 |
| 3898 | 4165 | |
| 3899 | 4166 | <a name="method-event"></a> |
| 4167 | ||
| 3900 | 4168 | #### `event()` {.collection-method} |
| 3901 | 4169 | |
| 3902 | 4170 | `event` 函数将给定的 [event](/docs/laravel/10.x/events) 分派给它的监听器: | … | … |
| 3904 | 4172 | event(new UserRegistered($user)); |
| 3905 | 4173 | |
| 3906 | 4174 | <a name="method-fake"></a> |
| 4175 | ||
| 3907 | 4176 | #### `fake()` {.collection-method} |
| 3908 | 4177 | |
| 3909 | 4178 | `fake` 函数解析容器中的 [Faker](https://github.com/FakerPHP/Faker) 单例,这在模型工厂、数据库填充、测试和原型视图中创建假数据时非常有用: | … | … |
| 3927 | 4196 | fake('nl_NL')->name() |
| 3928 | 4197 | |
| 3929 | 4198 | <a name="method-filled"></a> |
| 4199 | ||
| 3930 | 4200 | #### `filled()` {.collection-method} |
| 3931 | 4201 | |
| 3932 | 4202 | `filled` 函数确定给定值是否不是「空白」: | … | … |
| 3934 | 4204 | filled(0); |
| 3935 | 4205 | filled(true); |
| 3936 | 4206 | filled(false); |
| 3937 | ||
| 4207 | ||
| 3938 | 4208 | // true |
| 3939 | ||
| 4209 | ||
| 3940 | 4210 | filled(''); |
| 3941 | 4211 | filled(' '); |
| 3942 | 4212 | filled(null); |
| 3943 | 4213 | filled(collect()); |
| 3944 | ||
| 4214 | ||
| 3945 | 4215 | // false |
| 3946 | 4216 | |
| 3947 | 4217 | 对于 `filled` 的反转,请参阅 [`blank`](#method-blank) 方法。 |
| 3948 | 4218 | |
| 3949 | 4219 | <a name="method-info"></a> |
| 4220 | ||
| 3950 | 4221 | #### `info()` {.collection-method} |
| 3951 | 4222 | |
| 3952 | 4223 | `info` 函数会将信息写入应用程序的 [log](/docs/laravel/10.x/logging): | … | … |
| 3958 | 4229 | info('User login attempt failed.', ['id' => $user->id]); |
| 3959 | 4230 | |
| 3960 | 4231 | <a name="method-logger"></a> |
| 4232 | ||
| 3961 | 4233 | #### `logger()` {.collection-method} |
| 3962 | 4234 | |
| 3963 | 4235 | `logger` 函数可用于将 `debug` 级别的消息写入 [log](/docs/laravel/10.x/logging): | … | … |
| 3973 | 4245 | logger()->error('You are not allowed here.'); |
| 3974 | 4246 | |
| 3975 | 4247 | <a name="method-method-field"></a> |
| 4248 | ||
| 3976 | 4249 | #### `method_field()` {.collection-method} |
| 3977 | 4250 | |
| 3978 | 4251 | `method_field` 函数生成一个 HTML `hidden` 输入字段,其中包含表单 HTTP 谓词的欺骗值。 例如,使用 [Blade 语法](/docs/laravel/10.x/blade): | … | … |
| 3991 | 4264 | $now = now(); |
| 3992 | 4265 | |
| 3993 | 4266 | <a name="method-old"></a> |
| 4267 | ||
| 3994 | 4268 | #### `old()` {.collection-method} |
| 3995 | 4269 | |
| 3996 | 4270 | `old` 函数 [retrieves](/docs/laravel/10.x/requests#retrieving-input) 一个 [old input](/docs/laravel/10.x/requests#old-input) 值闪入Session : |
| 3997 | 4271 | |
| 3998 | 4272 | $value = old('value'); |
| 3999 | ||
| 4273 | ||
| 4000 | 4274 | $value = old('value', 'default'); |
| 4001 | 4275 | |
| 4002 | 4276 | 由于作为 `old` 函数的第二个参数提供的「默认值」通常是 Eloquent 模型的一个属性,Laravel 允许你简单地将整个 Eloquent 模型作为第二个参数传递给 `old` 函数。 这样做时,Laravel 将假定提供给 old 函数的第一个参数是 Eloquent 属性的名称,该属性应被视为「默认值」: |
| 4003 | 4277 | |
| 4004 | 4278 | {{ old('name', $user->name) }} |
| 4005 | ||
| 4006 | // | |
| 4007 | ||
| 4279 | ||
| 4280 | // 相当于... | |
| 4281 | ||
| 4008 | 4282 | {{ old('name', $user) }} |
| 4009 | 4283 | |
| 4010 | 4284 | <a name="method-optional"></a> |
| 4285 | ||
| 4011 | 4286 | #### `optional()` {.collection-method} |
| 4012 | 4287 | |
| 4013 | 4288 | `optional` 函数接受任何参数并允许您访问该对象的属性或调用方法。 如果给定对象为「null」,属性和方法将返回「null」而不是导致错误: |
| 4014 | 4289 | |
| 4015 | 4290 | return optional($user->address)->street; |
| 4016 | ||
| 4291 | ||
| 4017 | 4292 | {!! old('name', optional($user)->name) !!} |
| 4018 | 4293 | |
| 4019 | 4294 | `optional` 函数也接受一个闭包作为它的第二个参数。 如果作为第一个参数提供的值不为空,则将调用闭包: | … | … |
| 4023 | 4298 | }); |
| 4024 | 4299 | |
| 4025 | 4300 | <a name="method-policy"></a> |
| 4301 | ||
| 4026 | 4302 | #### `policy()` {.collection-method} |
| 4027 | 4303 | |
| 4028 | 4304 | `policy` 方法检索给定类的 [policy](/docs/laravel/10.x/authorization#creating-policies) 实例: | … | … |
| 4030 | 4306 | $policy = policy(App\Models\User::class); |
| 4031 | 4307 | |
| 4032 | 4308 | <a name="method-redirect"></a> |
| 4309 | ||
| 4033 | 4310 | #### `redirect()` {.collection-method} |
| 4034 | 4311 | |
| 4035 | 4312 | … | … |
| 4037 | 4314 | `redirect` 函数返回一个[重定向 HTTP 响应](/docs/laravel/10.x/responses#redirects),或者如果不带参数调用则返回重定向器实例: |
| 4038 | 4315 | |
| 4039 | 4316 | return redirect($to = null, $status = 302, $headers = [], $https = null); |
| 4040 | ||
| 4317 | ||
| 4041 | 4318 | return redirect('/home'); |
| 4042 | ||
| 4319 | ||
| 4043 | 4320 | return redirect()->route('route.name'); |
| 4044 | 4321 | |
| 4045 | 4322 | <a name="method-report"></a> |
| 4323 | ||
| 4046 | 4324 | #### `report()` {.collection-method} |
| 4047 | 4325 | |
| 4048 | 4326 | `report` 函数将使用您的 [异常处理程序](/docs/laravel/10.x/errors#the-exception-handler) 报告异常: | … | … |
| 4054 | 4332 | report('Something went wrong.'); |
| 4055 | 4333 | |
| 4056 | 4334 | <a name="method-report-if"></a> |
| 4335 | ||
| 4057 | 4336 | #### `report_if()` {.collection-method} |
| 4058 | 4337 | |
| 4059 | 4338 | 如果给定条件为「真」,「report_if」函数将使用您的[异常处理程序](/docs/laravel/10.x/errors#the-exception-handler) 报告异常: |
| 4060 | 4339 | |
| 4061 | 4340 | report_if($shouldReport, $e); |
| 4062 | ||
| 4341 | ||
| 4063 | 4342 | report_if($shouldReport, 'Something went wrong.'); |
| 4064 | 4343 | |
| 4065 | 4344 | <a name="method-report-unless"></a> |
| 4345 | ||
| 4066 | 4346 | #### `report_unless()` {.collection-method} |
| 4067 | 4347 | |
| 4068 | 4348 | 如果给定条件为 `false`,`report_unless` 函数将使用你的 [异常处理程序](/docs/laravel/10.x/errors#the-exception-handler) 报告异常: |
| 4069 | 4349 | |
| 4070 | 4350 | report_unless($reportingDisabled, $e); |
| 4071 | ||
| 4351 | ||
| 4072 | 4352 | report_unless($reportingDisabled, 'Something went wrong.'); |
| 4073 | 4353 | |
| 4074 | 4354 | <a name="method-request"></a> |
| 4355 | ||
| 4075 | 4356 | #### `request()` {.collection-method} |
| 4076 | 4357 | |
| 4077 | 4358 | `request` 函数返回当前的 [request](/docs/laravel/10.x/requests) 实例或从当前请求中获取输入字段的值: |
| 4078 | 4359 | |
| 4079 | 4360 | $request = request(); |
| 4080 | ||
| 4361 | ||
| 4081 | 4362 | $value = request('key', $default); |
| 4082 | 4363 | |
| 4083 | 4364 | <a name="method-rescue"></a> |
| 4365 | ||
| 4084 | 4366 | #### `rescue()` {.collection-method} |
| 4085 | 4367 | |
| 4086 | 4368 | `rescue` 函数执行给定的闭包并捕获其执行期间发生的任何异常。 捕获的所有异常都将发送到你的[异常处理程序](/docs/laravel/10.x/errors#the-exception-handler); 但是,请求将继续处理: | … | … |
| 4096 | 4378 | return rescue(function () { |
| 4097 | 4379 | return $this->method(); |
| 4098 | 4380 | }, false); |
| 4099 | ||
| 4381 | ||
| 4100 | 4382 | return rescue(function () { |
| 4101 | 4383 | return $this->method(); |
| 4102 | 4384 | }, function () { | … | … |
| 4104 | 4386 | }); |
| 4105 | 4387 | |
| 4106 | 4388 | <a name="method-resolve"></a> |
| 4389 | ||
| 4107 | 4390 | #### `resolve()` {.collection-method} |
| 4108 | 4391 | |
| 4109 | 4392 | `resolve` 函数使用 [服务容器](/docs/laravel/10.x/container) 将给定的类或接口名称解析为实例: | … | … |
| 4111 | 4394 | $api = resolve('HelpSpot\API'); |
| 4112 | 4395 | |
| 4113 | 4396 | <a name="method-response"></a> |
| 4397 | ||
| 4114 | 4398 | #### `response()` {.collection-method} |
| 4115 | 4399 | |
| 4116 | 4400 | `response` 函数创建一个 [response](/docs/laravel/10.x/responses) 实例或获取响应工厂的实例: |
| 4117 | 4401 | |
| 4118 | 4402 | return response('Hello World', 200, $headers); |
| 4119 | ||
| 4403 | ||
| 4120 | 4404 | return response()->json(['foo' => 'bar'], 200, $headers); |
| 4121 | 4405 | |
| 4122 | 4406 | <a name="method-retry"></a> |
| 4407 | ||
| 4123 | 4408 | #### `retry()` {.collection-method} |
| 4124 | 4409 | |
| 4125 | 4410 | `retry` 函数尝试执行给定的回调,直到达到给定的最大尝试阈值。 如果回调没有抛出异常,则返回它的返回值。 如果回调抛出异常,它会自动重试。 如果超过最大尝试次数,将抛出异常: |
| 4126 | 4411 | |
| 4127 | 4412 | return retry(5, function () { |
| 4128 | // | |
| 4413 | // 尝试 5 次,两次尝试之间休息 100 ms... | |
| 4129 | 4414 | }, 100); |
| 4130 | 4415 | |
| 4131 | 4416 | 如果想手动计算两次尝试之间休眠的毫秒数,你可以将闭包作为第三个参数传递给 `retry` 函数: |
| 4132 | 4417 | |
| 4133 | 4418 | use Exception; |
| 4134 | ||
| 4419 | ||
| 4135 | 4420 | return retry(5, function () { |
| 4136 | 4421 | // ... |
| 4137 | 4422 | }, function (int $attempt, Exception $exception) { | … | … |
| 4141 | 4426 | 为方便起见,你可以提供一个数组作为「retry」函数的第一个参数。 该数组将用于确定后续尝试之间要休眠多少毫秒: |
| 4142 | 4427 | |
| 4143 | 4428 | return retry([100, 200], function () { |
| 4144 | // | |
| 4429 | // 第一次重试时休眠 100 ms,第二次重试时休眠 200 ms... | |
| 4145 | 4430 | }); |
| 4146 | 4431 | |
| 4147 | 4432 | … | … |
| 4149 | 4434 | 要仅在特定条件下重试,您可以将闭包作为第四个参数传递给 `retry` 函数: |
| 4150 | 4435 | |
| 4151 | 4436 | use Exception; |
| 4152 | ||
| 4437 | ||
| 4153 | 4438 | return retry(5, function () { |
| 4154 | 4439 | // ... |
| 4155 | 4440 | }, 100, function (Exception $exception) { | … | … |
| 4157 | 4442 | }); |
| 4158 | 4443 | |
| 4159 | 4444 | <a name="method-session"></a> |
| 4445 | ||
| 4160 | 4446 | #### `session()` {.collection-method} |
| 4161 | 4447 | |
| 4162 | 4448 | `session` 函数可用于获取或设置 [session](/docs/laravel/10.x/session) 值: | … | … |
| 4170 | 4456 | 如果没有值传递给函数,会话存储将被返回: |
| 4171 | 4457 | |
| 4172 | 4458 | $value = session()->get('key'); |
| 4173 | ||
| 4459 | ||
| 4174 | 4460 | session()->put('key', $value); |
| 4175 | 4461 | |
| 4176 | 4462 | <a name="method-tap"></a> |
| 4463 | ||
| 4177 | 4464 | #### `tap()` {.collection-method} |
| 4178 | 4465 | |
| 4179 | 4466 | `tap` 函数接受两个参数:一个任意的 `$value` 和一个闭包。 `$value` 将传递给闭包,然后由 `tap` 函数返回。 闭包的返回值是无关紧要的: |
| 4180 | 4467 | |
| 4181 | 4468 | $user = tap(User::first(), function (User $user) { |
| 4182 | 4469 | $user->name = 'taylor'; |
| 4183 | ||
| 4470 | ||
| 4184 | 4471 | $user->save(); |
| 4185 | 4472 | }); |
| 4186 | 4473 | … | … |
| 4200 | 4487 | |
| 4201 | 4488 | |
| 4202 | 4489 | <a name="method-throw-if"></a> |
| 4490 | ||
| 4203 | 4491 | #### `throw_if()` {.collection-method} |
| 4204 | 4492 | |
| 4205 | 4493 | 如果给定的布尔表达式的计算结果为「真」,则 `throw_if` 函数会抛出给定的异常: |
| 4206 | 4494 | |
| 4207 | 4495 | throw_if(! Auth::user()->isAdmin(), AuthorizationException::class); |
| 4208 | ||
| 4496 | ||
| 4209 | 4497 | throw_if( |
| 4210 | 4498 | ! Auth::user()->isAdmin(), |
| 4211 | 4499 | AuthorizationException::class, | … | … |
| 4213 | 4501 | ); |
| 4214 | 4502 | |
| 4215 | 4503 | <a name="method-throw-unless"></a> |
| 4504 | ||
| 4216 | 4505 | #### `throw_unless()` {.collection-method} |
| 4217 | 4506 | |
| 4218 | 4507 | 如果给定的布尔表达式的计算结果为 `false`,则 `throw_unless` 函数会抛出给定的异常: |
| 4219 | 4508 | |
| 4220 | 4509 | throw_unless(Auth::user()->isAdmin(), AuthorizationException::class); |
| 4221 | ||
| 4510 | ||
| 4222 | 4511 | throw_unless( |
| 4223 | 4512 | Auth::user()->isAdmin(), |
| 4224 | 4513 | AuthorizationException::class, | … | … |
| 4226 | 4515 | ); |
| 4227 | 4516 | |
| 4228 | 4517 | <a name="method-today"></a> |
| 4518 | ||
| 4229 | 4519 | #### `today()` {.collection-method} |
| 4230 | 4520 | |
| 4231 | 4521 | 如果给定的布尔表达式的计算结果为 `false`,则 `throw_unless` 函数会抛出给定的异常: | … | … |
| 4233 | 4523 | $today = today(); |
| 4234 | 4524 | |
| 4235 | 4525 | <a name="method-trait-uses-recursive"></a> |
| 4526 | ||
| 4236 | 4527 | #### `trait_uses_recursive()` {.collection-method} |
| 4237 | 4528 | |
| 4238 | 4529 | `trait_uses_recursive` 函数返回特征使用的所有 trait: | … | … |
| 4240 | 4531 | $traits = trait_uses_recursive(\Illuminate\Notifications\Notifiable::class); |
| 4241 | 4532 | |
| 4242 | 4533 | <a name="method-transform"></a> |
| 4534 | ||
| 4243 | 4535 | #### `transform()` {.collection-method} |
| 4244 | 4536 | |
| 4245 | 4537 | 如果值不是 [blank](#method-blank),则 transform 函数会对给定值执行闭包,然后返回闭包的返回值: | … | … |
| 4247 | 4539 | $callback = function (int $value) { |
| 4248 | 4540 | return $value * 2; |
| 4249 | 4541 | }; |
| 4250 | ||
| 4542 | ||
| 4251 | 4543 | $result = transform(5, $callback); |
| 4252 | ||
| 4544 | ||
| 4253 | 4545 | // 10 |
| 4254 | 4546 | |
| 4255 | 4547 | 默认值或闭包可以作为函数的第三个参数传递。 如果给定值为空,将返回此值: |
| 4256 | 4548 | |
| 4257 | 4549 | $result = transform(null, $callback, 'The value is blank'); |
| 4258 | ||
| 4550 | ||
| 4259 | 4551 | // The value is blank |
| 4260 | 4552 | |
| 4261 | 4553 | <a name="method-validator"></a> |
| 4554 | ||
| 4262 | 4555 | #### `validator()` {.collection-method} |
| 4263 | 4556 | |
| 4264 | 4557 | `validator` 函数使用给定的参数创建一个新的 [validator](/docs/laravel/10.x/validation) 实例。 你可以将它用作 `Validator` 门面的替代品: | … | … |
| 4268 | 4561 | |
| 4269 | 4562 | |
| 4270 | 4563 | <a name="method-value"></a> |
| 4564 | ||
| 4271 | 4565 | #### `value()` {.collection-method} |
| 4272 | 4566 | |
| 4273 | 4567 | `value` 函数返回给定的值。 但是,如果将闭包传递给函数,则将执行闭包并返回其返回值: |
| 4274 | 4568 | |
| 4275 | 4569 | $result = value(true); |
| 4276 | ||
| 4570 | ||
| 4277 | 4571 | // true |
| 4278 | ||
| 4572 | ||
| 4279 | 4573 | $result = value(function () { |
| 4280 | 4574 | return false; |
| 4281 | 4575 | }); |
| 4282 | ||
| 4576 | ||
| 4283 | 4577 | // false |
| 4284 | ||
| 4578 | ||
| 4285 | 4579 | 可以将其他参数传递给「value」函数。 如果第一个参数是一个闭包,那么附加参数将作为参数传递给闭包,否则它们将被忽略: |
| 4286 | 4580 | |
| 4287 | 4581 | $result = value(function (string $name) { | … | … |
| 4291 | 4585 | // 'Taylor' |
| 4292 | 4586 | |
| 4293 | 4587 | <a name="method-view"></a> |
| 4588 | ||
| 4294 | 4589 | #### `view()` {.collection-method} |
| 4295 | 4590 | |
| 4296 | 4591 | `view` 函数检索一个 [view](/docs/laravel/10.x/views) 实例: | … | … |
| 4298 | 4593 | return view('auth.login'); |
| 4299 | 4594 | |
| 4300 | 4595 | <a name="method-with"></a> |
| 4596 | ||
| 4301 | 4597 | #### `with()` {.collection-method} |
| 4302 | 4598 | |
| 4303 | 4599 | `with` 函数返回给定的值。 如果将闭包作为函数的第二个参数传递,则将执行闭包并返回其返回值: | … | … |
| 4305 | 4601 | $callback = function (mixed $value) { |
| 4306 | 4602 | return is_numeric($value) ? $value * 2 : 0; |
| 4307 | 4603 | }; |
| 4308 | ||
| 4604 | ||
| 4309 | 4605 | $result = with(5, $callback); |
| 4310 | ||
| 4606 | ||
| 4311 | 4607 | // 10 |
| 4312 | ||
| 4608 | ||
| 4313 | 4609 | $result = with(null, $callback); |
| 4314 | ||
| 4610 | ||
| 4315 | 4611 | // 0 |
| 4316 | ||
| 4612 | ||
| 4317 | 4613 | $result = with(5, null); |
| 4318 | ||
| 4614 | ||
| 4319 | 4615 | // 5 |
| 4320 | 4616 | |
| 4321 | 4617 | <a name="other-utilities"></a> |
| 4618 | ||
| 4322 | 4619 | ## 其他 |
| 4323 | 4620 | |
| 4324 | 4621 | <a name="benchmarking"></a> |
| 4325 | ### Benchmarking | |
| 4622 | ||
| 4623 | ### 基准测试 | |
| 4326 | 4624 | |
| 4327 | 4625 | 有时你可能希望快速测试应用程序某些部分的性能。 在这些情况下,您可以使用 Benchmark 支持类来测量给定回调完成所需的毫秒数: |
| 4328 | 4626 | |
| 4329 | 4627 | <?php |
| 4330 | ||
| 4628 | ||
| 4331 | 4629 | use App\Models\User; |
| 4332 | 4630 | use Illuminate\Support\Benchmark; |
| 4333 | ||
| 4631 | ||
| 4334 | 4632 | Benchmark::dd(fn () => User::find(1)); // 0.1 ms |
| 4335 | ||
| 4633 | ||
| 4336 | 4634 | Benchmark::dd([ |
| 4337 | 4635 | 'Scenario 1' => fn () => User::count(), // 0.5 ms |
| 4338 | 4636 | 'Scenario 2' => fn () => User::all()->count(), // 20.0 ms | … | … |
| 4346 | 4644 | Benchmark::dd(fn () => User::count(), iterations: 10); // 0.5 ms |
| 4347 | 4645 | |
| 4348 | 4646 | <a name="pipeline"></a> |
| 4349 | ### Pipeline | |
| 4647 | ||
| 4648 | ### 管道 | |
| 4350 | 4649 | |
| 4351 | 4650 | Laravel 的 Pipeline 门面提供了一种便捷的方式来通过一系列可调用类、闭包或可调用对象「管道」给定输入,让每个类都有机会检查或修改输入并调用管道中的下一个可调用对象: |
| 4352 | 4651 | … | … |
| 4390 | 4689 | |
| 4391 | 4690 | |
| 4392 | 4691 | <a name="lottery"></a> |
| 4393 | ### Lottery | |
| 4692 | ||
| 4693 | ### 彩票 | |
| 4394 | 4694 | |
| 4395 | 4695 | Laravel 的 Lottery 类可用于根据一组给定的赔率执行回调。 当你只想为一定比例的传入请求执行代码时,这会特别有用: |
| 4396 | 4696 | |
| 4397 | 4697 | use Illuminate\Support\Lottery; |
| 4398 | ||
| 4698 | ||
| 4399 | 4699 | Lottery::odds(1, 20) |
| 4400 | 4700 | ->winner(fn () => $user->won()) |
| 4401 | 4701 | ->loser(fn () => $user->lost()) | … | … |
| 4406 | 4706 | use Carbon\CarbonInterval; |
| 4407 | 4707 | use Illuminate\Support\Facades\DB; |
| 4408 | 4708 | use Illuminate\Support\Lottery; |
| 4409 | ||
| 4709 | ||
| 4410 | 4710 | DB::whenQueryingForLongerThan( |
| 4411 | 4711 | CarbonInterval::seconds(2), |
| 4412 | 4712 | Lottery::odds(1, 100)->winner(fn () => report('Querying > 2 seconds.')), |
| 4413 | 4713 | ); |
| 4414 | 4714 | |
| 4415 | 4715 | <a name="testing-lotteries"></a> |
| 4416 | #### Testing Lotteries | |
| 4716 | ||
| 4717 | #### 测试彩票 | |
| 4417 | 4718 | |
| 4418 | 4719 | Laravel 提供了一些简单的方法来让你轻松测试应用程序的 Lottery 调用: |
| 4419 | 4720 | |
| 4420 | // | |
| 4721 | // 彩票总是取胜... | |
| 4421 | 4722 | Lottery::alwaysWin(); |
| 4422 | ||
| 4423 | // | |
| 4723 | ||
| 4724 | // 彩票总是获败... | |
| 4424 | 4725 | Lottery::alwaysLose(); |
| 4425 | ||
| 4426 | // | |
| 4726 | ||
| 4727 | // 彩票会先赢后输,最后恢复到正常行为... | |
| 4427 | 4728 | Lottery::fix([true, false]); |
| 4428 | ||
| 4429 | // Lottery will return to normal behavior... | |
| 4430 | Lottery::determineResultsNormally(); | |
| 4431 | ||
| 4432 | ||
| 4729 | ||
| 4730 | // 彩票将恢复到正常行为... | |
| 4731 | Lottery::determineResultsNormally(); |
关于 LearnKu