php比较字符串的函数怎么使用

发布时间:2022-09-01 11:14:05 作者:iii
来源:亿速云 阅读:140

PHP比较字符串的函数怎么使用

在PHP中,字符串比较是一个常见的操作。无论是验证用户输入、排序数据,还是进行条件判断,字符串比较都扮演着重要的角色。PHP提供了多种函数来比较字符串,每种函数都有其特定的用途和适用场景。本文将详细介绍PHP中常用的字符串比较函数,并通过示例代码展示它们的使用方法。

1. strcmp() 函数

strcmp() 函数用于比较两个字符串,区分大小写。它返回一个整数,表示两个字符串的比较结果。

语法

int strcmp ( string $str1 , string $str2 )

返回值

示例

$str1 = "apple";
$str2 = "banana";

$result = strcmp($str1, $str2);

if ($result < 0) {
    echo "$str1 小于 $str2";
} elseif ($result > 0) {
    echo "$str1 大于 $str2";
} else {
    echo "$str1 等于 $str2";
}

输出

apple 小于 banana

2. strcasecmp() 函数

strcasecmp() 函数与 strcmp() 类似,但它不区分大小写。

语法

int strcasecmp ( string $str1 , string $str2 )

返回值

示例

$str1 = "Apple";
$str2 = "apple";

$result = strcasecmp($str1, $str2);

if ($result < 0) {
    echo "$str1 小于 $str2";
} elseif ($result > 0) {
    echo "$str1 大于 $str2";
} else {
    echo "$str1 等于 $str2";
}

输出

Apple 等于 apple

3. strncmp() 函数

strncmp() 函数用于比较两个字符串的前 n 个字符,区分大小写。

语法

int strncmp ( string $str1 , string $str2 , int $length )

返回值

示例

$str1 = "apple";
$str2 = "apricot";

$result = strncmp($str1, $str2, 3);

if ($result < 0) {
    echo "$str1 小于 $str2";
} elseif ($result > 0) {
    echo "$str1 大于 $str2";
} else {
    echo "$str1 等于 $str2";
}

输出

apple 等于 apricot

4. strncasecmp() 函数

strncasecmp() 函数与 strncmp() 类似,但它不区分大小写。

语法

int strncasecmp ( string $str1 , string $str2 , int $length )

返回值

示例

$str1 = "Apple";
$str2 = "apricot";

$result = strncasecmp($str1, $str2, 3);

if ($result < 0) {
    echo "$str1 小于 $str2";
} elseif ($result > 0) {
    echo "$str1 大于 $str2";
} else {
    echo "$str1 等于 $str2";
}

输出

Apple 等于 apricot

5. substr_compare() 函数

substr_compare() 函数用于比较两个字符串的子串。

语法

int substr_compare ( string $main_str , string $str , int $offset [, int $length [, bool $case_insensitivity = false ]] )

参数

返回值

示例

$main_str = "Hello, world!";
$str = "world";

$result = substr_compare($main_str, $str, 7, 5);

if ($result < 0) {
    echo "子串小于 $str";
} elseif ($result > 0) {
    echo "子串大于 $str";
} else {
    echo "子串等于 $str";
}

输出

子串等于 world

6. similar_text() 函数

similar_text() 函数用于计算两个字符串的相似度。

语法

int similar_text ( string $str1 , string $str2 [, float &$percent ] )

参数

返回值

返回两个字符串的相似字符数。

示例

$str1 = "Hello, world!";
$str2 = "Hello, PHP!";

similar_text($str1, $str2, $percent);

echo "相似度: $percent%";

输出

相似度: 66.666666666667%

7. levenshtein() 函数

levenshtein() 函数用于计算两个字符串之间的编辑距离(Levenshtein 距离)。编辑距离是指将一个字符串转换为另一个字符串所需的最少编辑操作次数,包括插入、删除和替换字符。

语法

int levenshtein ( string $str1 , string $str2 )

返回值

返回两个字符串之间的编辑距离。

示例

$str1 = "kitten";
$str2 = "sitting";

$distance = levenshtein($str1, $str2);

echo "编辑距离: $distance";

输出

编辑距离: 3

8. strcoll() 函数

strcoll() 函数用于根据当前区域设置比较两个字符串。

语法

int strcoll ( string $str1 , string $str2 )

返回值

示例

setlocale(LC_COLLATE, 'en_US.UTF-8');

$str1 = "apple";
$str2 = "banana";

$result = strcoll($str1, $str2);

if ($result < 0) {
    echo "$str1 小于 $str2";
} elseif ($result > 0) {
    echo "$str1 大于 $str2";
} else {
    echo "$str1 等于 $str2";
}

输出

apple 小于 banana

9. strnatcmp() 函数

strnatcmp() 函数使用“自然排序”算法比较两个字符串。

语法

int strnatcmp ( string $str1 , string $str2 )

返回值

示例

$str1 = "img12.png";
$str2 = "img10.png";

$result = strnatcmp($str1, $str2);

if ($result < 0) {
    echo "$str1 小于 $str2";
} elseif ($result > 0) {
    echo "$str1 大于 $str2";
} else {
    echo "$str1 等于 $str2";
}

输出

img12.png 大于 img10.png

10. strnatcasecmp() 函数

strnatcasecmp() 函数与 strnatcmp() 类似,但它不区分大小写。

语法

int strnatcasecmp ( string $str1 , string $str2 )

返回值

示例

$str1 = "Img12.png";
$str2 = "img10.png";

$result = strnatcasecmp($str1, $str2);

if ($result < 0) {
    echo "$str1 小于 $str2";
} elseif ($result > 0) {
    echo "$str1 大于 $str2";
} else {
    echo "$str1 等于 $str2";
}

输出

Img12.png 大于 img10.png

11. strspn() 函数

strspn() 函数用于计算字符串中完全由指定字符组成的子串的长度。

语法

int strspn ( string $str , string $mask [, int $start [, int $length ]] )

参数

返回值

返回字符串中完全由 mask 中的字符组成的子串的长度。

示例

$str = "123abc456";
$mask = "1234567890";

$length = strspn($str, $mask);

echo "完全由数字组成的子串长度: $length";

输出

完全由数字组成的子串长度: 3

12. strcspn() 函数

strcspn() 函数用于计算字符串中不包含指定字符的子串的长度。

语法

int strcspn ( string $str , string $mask [, int $start [, int $length ]] )

参数

返回值

返回字符串中不包含 mask 中的字符的子串的长度。

示例

$str = "123abc456";
$mask = "abc";

$length = strcspn($str, $mask);

echo "不包含字母的子串长度: $length";

输出

不包含字母的子串长度: 3

13. strpos() 函数

strpos() 函数用于查找字符串中首次出现指定子串的位置。

语法

int strpos ( string $haystack , string $needle [, int $offset = 0 ] )

参数

返回值

返回子串首次出现的位置,如果未找到则返回 false

示例

$haystack = "Hello, world!";
$needle = "world";

$position = strpos($haystack, $needle);

if ($position !== false) {
    echo "子串首次出现的位置: $position";
} else {
    echo "未找到子串";
}

输出

子串首次出现的位置: 7

14. stripos() 函数

stripos() 函数与 strpos() 类似,但它不区分大小写。

语法

int stripos ( string $haystack , string $needle [, int $offset = 0 ] )

参数

返回值

返回子串首次出现的位置,如果未找到则返回 false

示例

$haystack = "Hello, world!";
$needle = "WORLD";

$position = stripos($haystack, $needle);

if ($position !== false) {
    echo "子串首次出现的位置: $position";
} else {
    echo "未找到子串";
}

输出

子串首次出现的位置: 7

15. strrpos() 函数

strrpos() 函数用于查找字符串中最后一次出现指定子串的位置。

语法

int strrpos ( string $haystack , string $needle [, int $offset = 0 ] )

参数

返回值

返回子串最后一次出现的位置,如果未找到则返回 false

示例

$haystack = "Hello, world! world!";
$needle = "world";

$position = strrpos($haystack, $needle);

if ($position !== false) {
    echo "子串最后一次出现的位置: $position";
} else {
    echo "未找到子串";
}

输出

子串最后一次出现的位置: 14

16. strripos() 函数

strripos() 函数与 strrpos() 类似,但它不区分大小写。

语法

int strripos ( string $haystack , string $needle [, int $offset = 0 ] )

参数

返回值

返回子串最后一次出现的位置,如果未找到则返回 false

示例

$haystack = "Hello, world! WORLD!";
$needle = "world";

$position = strripos($haystack, $needle);

if ($position !== false) {
    echo "子串最后一次出现的位置: $position";
} else {
    echo "未找到子串";
}

输出

子串最后一次出现的位置: 14

17. strstr() 函数

strstr() 函数用于查找字符串中首次出现指定子串的位置,并返回从该位置到字符串末尾的部分。

语法

string strstr ( string $haystack , string $needle [, bool $before_needle = false ] )

参数

返回值

返回从子串首次出现的位置到字符串末尾的部分,如果未找到则返回 false

示例

$haystack = "Hello, world!";
$needle = "world";

$result = strstr($haystack, $needle);

if ($result !== false) {
    echo "找到子串: $result";
} else {
    echo "未找到子串";
}

输出

找到子串: world!

18. stristr() 函数

stristr() 函数与 strstr() 类似,但它不区分大小写。

语法

string stristr ( string $haystack , string $needle [, bool $before_needle = false ] )

参数

返回值

返回从子串首次出现的位置到字符串末尾的部分,如果未找到则返回 false

示例

$haystack = "Hello, world!";
$needle = "WORLD";

$result = stristr($haystack, $needle);

if ($result !== false) {
    echo "找到子串: $result";
} else {
    echo "未找到子串";
}

输出

找到子串: world!

19. strchr() 函数

strchr() 函数是 strstr() 的别名,功能完全相同。

语法

string strchr ( string $haystack , string $needle [, bool $before_needle = false ] )

参数

返回值

返回从子串首次出现的位置到字符串末尾的部分,如果未找到则返回 false

示例

”`php

推荐阅读:
  1. php中常用的字符串比较函数strcmp()实例解释
  2. 比较php中的各种排序函数

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php

上一篇:PHP八大设计模式怎么实现

下一篇:php十六进制值如何转换成字符串

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》