elphis/string-helpers

PHP 有用的字符串帮助函数。

v1.0 2020-04-09 11:39 UTC

This package is not auto-updated.

Last update: 2024-09-29 05:29:46 UTC


README

此库包含一些基本的PHP帮助函数,可以使字符串操作更加方便和高效。此库中每个函数都使用命名空间 Elphis\Helpers\Str,以避免与您可能使用的任何其他库冲突。

安装

composer require elphis/string-helpers:dev-master

以下是该软件包提供的字符串帮助函数列表。

  • str_contains($haystack, $needle, $caseInsensitive = false): 检查 $needle 字符串是否存在于 $haystack 字符串中,并相应地返回 truefalse。当 $caseInsensitivetrue 时,检查将是不区分大小写的。
    Eg: 
    $fullName   = 'John Joseph Doe';
    $fatherName = 'Joseph';
    str_contains($fullName, $fatherName); // *true*
    $fatherName = 'joseph';
    str_contains($fullName, $fatherName); // *false*
    str_contains($fullName, $fatherName, true); // *true*
  • str_equals($string1, $string2, $caseInsensitive = false): 检查给定的字符串是否相等。当 $caseInsensitive 为 true 时,检查将是不区分大小写的。
    Eg: 
    $string1 = 'In the bleak mid-winter';
    $string2 = 'In the bleak mid-winter';
    str_equals($string1, $string2); // *true*
    $string2 = 'In the bleak MID-WINTER';
    str_equals($string1, $string2); // *false*
    str_equals($string1, $string2, true); // *true*
  • str_starts_with($haystack, $needle, $caseInsensitive = false): 检查给定的 $haystack 字符串是否以给定的 $needle 开头。$caseInsensitive = true 用于不区分大小写的检查。
    Eg:
    $fullName  = 'John Joseph Doe';
    $firstName = 'John';
    str_starts_with($fullName, $firstName); // *true*
    $firstName = 'john';
    str_starts_with($fullName, $firstName); // *false*
    str_starts_with($fullName, $firstName, true); // *true*
    $firstName = 'joseph';
    str_starts_with($fullName, $firstName); // *false*
    str_starts_with($fullName, $firstName, true); // *false*
  • str_ends_with($haystack, $needle, $caseInsensitive = false): 检查给定的 $haystack 字符串是否以给定的 $needle 结尾。$caseInsensitive = true 用于不区分大小写的检查。
    Eg:
    $fullName = 'John Joseph Doe';
    $lastName = 'Doe';
    str_ends_with($fullName, $lastName); // *true*
    $lastName = 'doe';
    str_ends_with($fullName, $lastName); // *false*
    str_ends_with($fullName, $lastName, true); // *true*
    $lastName = 'joseph';
    str_ends_with($fullName, $lastName); // *false*
    str_ends_with($fullName, $lastName, true); // *false*
  • str_limit($string, $length, $suffix = '...'): 此函数将 $string 在给定的 $length 处截断/拆分,并使用给定的 $suffix 后缀。
    Eg:
    $title = 'This is a very long string that cannot be displayed in the list section of your blog, so you have to limit it.';
    str_limit($title, 20); // *This is a very long ...*
    str_limit($title, 20, '---'); // *This is a very long ---*
  • str_after($haystack, $needle, bool $caseInsensitive = false): 返回从 $haystack$needle 之后的字符串。当 $caseSensitive = true 时,$needle 将以不区分大小写的方式搜索。
    Eg:
    $title = 'This is a very long string that cannot be displayed in the list section of your blog, so you have to break it.';
    str_after($title, 'This is a very long string that cannot be displayed'); // *in the list section of your blog, so you have to break it.*
    str_after($title, 'Yolo'); // *''*
    str_after($title, 'this'); // *''*
    str_after($title, 'this', true); // *is a very long string that cannot be displayed in the list section of your blog, so you have to break it.*
  • str_before($haystack, $needle, bool $caseInsensitive = false): 返回从 $haystack$needle 之前的字符串。当 $caseSensitive = true 时,$needle 将以不区分大小写的方式搜索。
    Eg:
    $title = 'This is a very long string that cannot be displayed in the list section of your blog, so you have to break it.';
    str_before($title, 'a very long'); // *This is *
    str_before($title, 'Yolo'); // *''*
    str_before($title, 'this'); // *''*
    str_before($title, 'this', true); // *''*
    str_before($title, 'is a very', true); // *This *
  • str_between(string $haystack, string $from, string $to, bool $caseInsensitive = false): 返回从给定的 $haystack 中找到的介于给定的 $from 子字符串和 $to 子字符串之间的字符串。当 $caseInsensitive = true 时,$from$to 将以不区分大小写的方式搜索。
    Eg:
    $title = 'This is a very long string that cannot be displayed in the list section of your blog, so you have to break it.';
    str_between($title, 'This', 'long'); // * is a very *
    str_between($title, 'is', 'This'); // *''*
    str_between($title, 'is', 'This', true); // *''*
    str_between($title, 'this', 'is', true); // *' is a very '*
    str_between($title, 'sO YoU hAve', 'it', true); // *' to break '*
  • str_strip_special_chars(string $string, string $replaceWith = ''): 删除给定的 $string 中的任何特殊字符,并返回只包含字母或数字的字符串。传递 $replaceWith 将用 $replaceWith 替换字符串中的特殊字符。
    Eg:
    $title = '^&%This*&(*is)(^%^#@_=0it.';
    str_strip_special_chars($title); // *Thisis0it*
    str_strip_special_chars($title, '-'); // *---This----is---------0it-*
  • str_camel_case($string): 删除给定的 $string 中的任何特殊字符和空格,并将 $string 转换为 "CamelCase"。
    Eg:
    $title = 'This is a normal string';
    str_camel_case($title); // *thisIsANormalString*
    $title = 'This is$a#normal%string';
    str_camel_case($title); // *thisIsANormalString*
  • str_studly_case($string): 删除给定的 $string 中的任何特殊字符和空格,并将其转换为 "studlyCase"。
    Eg:
    $title = 'This is a normal string';
    str_studly_case($title); // *ThisIsANormalString*
    $title = 'This is$a#normal%string';
    str_studly_case($title); // *ThisIsANormalString*
  • str_kebab_case($string): 删除给定的 $string 中的任何特殊字符和空格,并将其转换为 "kebab-case"。
    Eg:
    $title = 'This is a normal string';
    str_kebab_case($title); // *this-is-a-normal-string*
    $title = 'This is$a#normal%string';
    str_kebab_case($title); // *this-is-a-normal-string*
  • str_snake_case($string): 删除给定的 $string 中的任何特殊字符和空格,并将其转换为 "snake_case"。
    Eg:
    $title = 'This is a normal string';
    str_snake_case($title); // *this_is_a_normal_string*
    $title = 'This is$a#normal%string';
    str_snake_case($title); // *this_is_a_normal_string*
  • str_strip_numbers($string): 删除 $string 中找到的任何数字值。
    Eg:
    $title = 'great here is my contact no: 12346732';
    str_strip_numbers($title); // *great here is my contact no:*
  • str_chunks($string, $length = 100): 在每 $length 处拆分给定的 $string,并返回一个字符串数组,每个字符串都包含长度为 $length 的字符串。
    Eg:
    $title = 'hope ya'll like this small helper library';
    str_chunk($title, 10); // *['hope ya'll', ' like this', ' small hel', 'per librar', 'y']:*

##待办事项 ### 添加多字节支持。 ### 在辅助函数周围添加包装类。