当前位置: 首页> 函数类别大全> str_starts_with

str_starts_with

检查字符串是否以给定的子字符串开头
名称:str_starts_with
分类:字符串
所属语言:php
一句话介绍:检查字符串是否以给定的子字符串开头

str_starts_with 函数

适用 PHP 版本

PHP 8.0.0 及以上版本

函数说明

str_starts_with 函数用于检查一个字符串是否以指定的子字符串开头。

函数语法

bool str_starts_with(string $haystack, string $needle)

参数

  • $haystack (string): 要检查的主字符串。
  • $needle (string): 要检查的子字符串。

返回值

如果 $haystack 以 $needle 开头,函数返回 true;否则返回 false

示例

以下是使用 str_starts_with 函数的示例:

$haystack = "Hello, world!";
$needle = "Hello";
<p>if (str_starts_with($haystack, $needle)) {<br>
echo "字符串以 '$needle' 开头";<br>
} else {<br>
echo "字符串不以 '$needle' 开头";<br>
}<br>

示例代码的说明

在这个示例中,$haystack 是要检查的字符串 "Hello, world!",$needle 是我们希望它以 "Hello" 开头的子字符串。因为 "Hello, world!" 确实以 "Hello" 开头,函数将返回 true,因此输出 "字符串以 'Hello' 开头"。

同类函数
  • 二进制安全比较字符串(从偏移位置比较指定长度) substr_compare

    substr_compare

    二进制安全比较字符串(从偏移位置比较指定
  • rtrim的别名 chop

    chop

    rtrim的别名
  • 将字符串转换为数组 str_split

    str_split

    将字符串转换为数组
  • strstr的别名 strchr

    strchr

    strstr的别名
  • 转义元字符集 quotemeta

    quotemeta

    转义元字符集
  • 单向字符串散列 crypt

    crypt

    单向字符串散列
  • 返回格式化的字符串 vsprintf

    vsprintf

    返回格式化的字符串
  • 使用“自然顺序”算法进行字符串比较 strnatcmp

    strnatcmp

    使用“自然顺序”算法进行字符串比较
热门文章