thomas-squall/string-utils

字符串管理工具

0.5.5 2018-12-29 16:11 UTC

This package is auto-updated.

Last update: 2024-09-20 03:57:44 UTC


README

可用函数列表

  1. string_starts_with
  2. string_ends_with
  3. string_between
  4. strings_between
  5. string_contains

string_starts_with

描述

如果 $haystack 字符串以 $needle 字符串开头,则返回 true。

定义

string_starts_with($haystack, $needle)

其中

  1. $haystack 是要查找的字符串
  2. $needle 是要检查的字符串,看 $haystack 是否以它开头

用法

$string = "Hello World";

echo "Starts with Hello? " . string_starts_with($string, "Hello");
echo PHP_EOL;
echo "Starts with World? " . string_starts_with($string, "World");

这将打印

Starts with Hello? true
Starts with World? false

string_ends_with

描述

如果 $haystack 字符串以 $needle 字符串结尾,则返回 true。

定义

string_ends_with($haystack, $needle)

其中

  1. $haystack 是要查找的字符串
  2. $needle 是要检查的字符串,看 $haystack 是否以它开头

用法

$string = "Hello World";

echo "Ends with Hello? " . string_ends_with($string, "Hello");
echo PHP_EOL;
echo "Ends with World? " . string_ends_with($string, "World");

这将打印

Ends with Hello? false
Ends with World? true

string_between

描述

从 $string 中返回 $start 和 $end 字符串之间的第一个字符串。如果没有找到,则返回 false。

定义

string_between($string, $start, $end, $empty_string = false)

其中

  1. $string 是要查找的字符串
  2. $start 是要搜索的左字符串
  3. $end 是要搜索的右字符串
  4. $empty_string 决定是否返回空字符串

用法

$string_1 = "|Hello|World|";
$string_2 = "||";

echo "String 1: " . string_between($string_1, "|", "|");
echo PHP_EOL;
echo "String 2 with $empty_string false: " . string_between($string_2, "|", "|");
echo PHP_EOL;
echo "String 2 with $empty_string true: " . string_between($string_2, "|", "|", true);

这将打印

String 1: Hello
String 2: false
String 2: 

strings_between

描述

从 $string 中返回 $start 和 $end 字符串之间的字符串。如果没有找到,则返回空数组。

定义

strings_between($string, $start, $end, $empty_strings = false)

其中

  1. $string 是要查找的字符串
  2. $start 是要搜索的左字符串
  3. $end 是要搜索的右字符串
  4. $empty_strings 决定是否返回空字符串

用法

$string = "|Hello|World||";

echo "With $empty_strings false:" . PHP_EOL;
print_r(strings_between($string, "|", "|"));
echo PHP_EOL;
echo "With $empty_strings true:" . PHP_EOL;
print_r(strings_between($string, "|", "|", true));

这将打印

With $empty_strings false:
Array ( [0] => Hello [1] => World )

With $empty_strings true:
Array ( [0] => Hello [1] => World [2] => )

string_contains

描述

是否 $needle 字符串包含在 $haystack 字符串中。

定义

string_contains($haystack, $needle)

其中

  1. $haystack 是要查找的字符串
  2. $needle 是要检查的字符串,看 $haystack 是否包含它

用法

$string = "Hello";

echo "Contains Hello? " . string_contains($string, "Hello");
echo PHP_EOL;
echo "Contains World? " . string_contains($string, "World");

这将打印

Contains Hello? true
Contains World? false

更多工具即将推出