在 PHP 中检查字符串是否以给定单词开头
server side programmingprogrammingphp更新于 2025/9/8 22:09:00
创建一个函数来检查字符串是否以指定字符串开头。该函数应在成功时返回 TRUE,在失败时返回 FALSE。
以下是语法 −
begnWith(str, begnStr)
考虑其中的以下参数来检查 −
str − 要测试的字符串
begnStr − 要在指定字符串开头搜索的文本。
示例
以下是示例 −
<?php
function begnWith($str, $begnString) {
$len = strlen($begnString);
return (substr($str, 0, $len) = = = $begnString);
}
if(begnWith("pqrstuvwxyz","p"))
echo "True! It begins with p!";
else
echo "False! It isn't beginning with p!";
?>
输出
以下是输出 −
True! It begins with p!

