jameslevi/string

这是一个简单的库,提供PHP原生不提供的常用字符串函数。

v1.0.1 2021-05-03 15:42 UTC

This package is not auto-updated.

Last update: 2024-10-01 07:45:22 UTC


README

这是一个简单的库,提供PHP原生不提供的常用字符串函数。

安装

  1. 您可以通过composer进行安装。
composer require jameslevi/string
  1. 如果您没有使用任何PHP框架,只需将此代码粘贴到代码的顶部,以便将composer自动加载机制包含到您的项目中。
<?php

if(file_exists(__DIR__.'/vendor/autoload.php'))
{
    require __DIR__.'/vendor/autoload.php';
}
  1. 现在您可以使用此库中包含的所有函数。

str_equals(string $string, mixed $match)

比较两个字符串。

// Returns true because "hello" matches one of the word inside array.
str_equals("hello", array(
  "hello",
  "world",
))

str_starts_with(string $string, mixed $match)

判断字符串是否以匹配字符串开头。

str_starts_with("hello", "h") // Returns true because hello starts with an h.

str_ends_with(string $string, mixed $match)

判断字符串是否以匹配字符串结尾。

// Returns false because @email.com is not included from the array.
str_ends_with("foo@email.com", array(
  "@gmail.com",
  "@yahoo.com",
))

str_remove(string $string, mixed $search)

从字符串中删除所有匹配的字符。

str_remove("hello world", "l") // Returns "heo word".

str_move(string $string, int $start, int $end = 0)

从字符串的一侧或两侧删除一个或多个字符。

str_move("hello world", 1, 2) // Returns "ello wor".

str_move_left(string $string, int $count)

从字符串的左侧删除一个或多个字符。

str_move_left("hello world", 3) // Returns "lo world".

str_move_right(string $string, int $count)

从字符串的右侧删除一个或多个字符。

str_move_right("hello world", 2) // Returns "hello wor".

str_count_numeric(string $string)

计算字符串中数字字符的出现次数。

str_count_numeric("hello world 123") // Returns "3".

str_count_uppercase_letter(string $string)

计算字符串中大写字母的出现次数。

str_count_uppercase_letter("Hello World") // Returns "2".

str_count_lowercase_letter(string $string)

计算字符串中小写字母的出现次数。

str_count_lowercase_letter("Hello World") // Returns "8".

str_count_letter(string $string)

计算字符串中字母的出现次数。

str_count_letter("Hello World") // Returns "10".

str_count_line(string $string)

计算字符串中的行数。

str_count_line("Hello World") // Returns "1".

str_count_spaces(string $string)

计算字符串中的空格数。

str_count_spaces("Hello World") // Returns "1".

str_count_special_chars(string $string)

计算字符串中特殊字符的数量。

str_count_special_chars("Hello World!!!") // Returns "3".

str_count_words(string $string)

计算字符串中的单词数量。

str_count_words("Hello World") // Returns "2".

str_words(string $string)

从字符串中返回单词列表。

str_words("Hello World") 

此函数将返回

array(2)
(
  [0] => "Hello"
  [1] => "World"
)

str_contains(string $string, mixed $keywords)

测试字符串是否包含一个或多个单词或字符。

str_contains("Hello World", "Hello") // Returns true.

str_break(string $string, string $delimeter)

将字符串分割成两个部分。

str_break("Hello World", " ")

此函数将返回

array(2)
(
  [0] => "Hello"
  [1] => "World"
)

str_is_upper(string $string)

判断字符串是否为大写。

str_is_upper("hello world") // Returns false.
str_is_upper("Hello World") // Returns false.
str_is_upper("HELLO WORLD") // Returns true.

str_is_lower(string $string)

判断字符串是否为小写。

str_is_lower("hello world") // Returns true.
str_is_lower("Hello World") // Returns false.
str_is_lower("HELLO WORLD") // Returns false.

str_uppercase(string $string, int $position)

通过位置号将字母转换为大写。

str_uppercase("hello world", 0) // Returns "Hello world".

str_lowercase(string $string, int $position)

通过位置号将字母转换为小写。

str_lowercase("Hello World", 6) // Returns "Hello world".

str_to_camel(string $string)

将单词组转换为驼峰式。

str_to_camel("Hello World") // Returns "helloWorld".

str_to_snake(string $string)

将单词组转换为蛇形。

str_to_snake("Hello World") // Returns "hello_world".

str_to_kebab(string $string)

将单词组转换为中划线式。

str_to_kebab("Hello World") // Returns "hello-world".

str_to_pascal(string $string)

将单词组转换为帕斯卡式。

str_to_pascal("Hello World") // Returns "HelloWorld".

str_camel_to_snake(string $string)

将字符串从驼峰式转换为蛇形。

str_camel_to_snake("helloWorld") // Returns "hello_world".

str_camel_to_kebab(string $string)

将字符串从驼峰式转换为中划线式。

str_camel_to_kebab("helloWorld") // Returns "hello-world".

str_camel_to_pascal(string $string)

将字符串从驼峰式转换为帕斯卡式。

str_camel_to_pascal("helloWorld") // Returns "HelloWorld".

str_snake_to_camel(string $string)

将字符串从蛇形转换为驼峰式。

str_snake_to_camel("hello_world") // Returns "helloWorld".

str_snake_to_kebab(string $string)

将字符串从蛇形转换为中划线式。

str_snake_to_kebab("hello_world") // Returns "hello-world".

str_snake_to_pascal(string $string)

将字符串从蛇形转换为帕斯卡式。

str_snake_to_pascal("hello_world") // Returns "HelloWorld".

str_kebab_to_camel(string $string)

将字符串从短横线命名法转换为驼峰命名法。

str_kebab_to_camel("hello-world") // Returns "helloWorld".

str_kebab_to_snake(string $string)

将字符串从短横线命名法转换为蛇形命名法。

str_kebab_to_snake("hello-world") // Returns "hello_world".

str_kebab_to_pascal(string $string)

将短横线命名法字符串转换为帕斯卡命名法。

str_kebab_to_pascal("hello-world") // Returns "HelloWorld".

str_pascal_to_camel(string $string)

将帕斯卡命名法字符串转换为驼峰命名法。

str_pascal_to_camel("HelloWorld") // Returns "helloWorld".

str_pascal_to_snake(string $string)

将帕斯卡命名法转换为蛇形命名法。

str_pascal_to_snake("HelloWorld") // Returns "hello_world".

str_pascal_to_kebab(string $string)

将帕斯卡命名法转换为短横线命名法。

str_pascal_to_kebab("HelloWorld") // Returns "hello-world".

str_camel_to_words(string $string)

将驼峰命名法字符串转换为单词。

str_camel_to_words("helloWorld") // Returns "hello world".

str_snake_to_words(string $string)

将蛇形命名法字符串转换为单词。

str_snake_to_words("hello_world") // Returns "hello world".

str_kebab_to_words(string $string)

将短横线命名法字符串转换为单词。

str_kebab_to_words("hello-world") // Returns "hello world".

str_pascal_to_words(string $string)

将帕斯卡命名法字符串转换为单词。

str_pascal_to_words("HelloWorld") // Returns "hello world".

str_truncate(string $string, int $max)

如果字符串超过最大字符数,则截断字符串并自动在末尾追加"..."。

str_truncate("Hello World!!!", 8) // Returns "Hello...".

str_random(int $length = 10, int $pool = 0)

生成一个随机字符串。

str_random(10, STR_RANDOM_DEFAULT); // Return random alphanumeric characters.
str_random(10, STR_RANDOM_NUMBERS); // Return random numbers.
str_random(10, STR_RANDOM_LETTERS); // Return random letters.
str_random(10, STR_RANDOM_LETTERS_UPPERCASE); // Return random uppercase letters.
str_random(10, STR_RANDOM_LETTERS_LOWERCASE); // Return random lowercase letters.

贡献

对于问题、关注和建议,您可以通过nerdlabenterprise@gmail.com联系James Crisostomo。

许可

本软件包是一个开源软件,许可协议为MIT 许可。