如何编写以指定字符/字符串开头或以指定字符/字符串结尾的字符串返回的两个函数
例如:
$str='| apples}';
echo开始使用($str,“|”)//返回true
echo endsWith($str,'}')//返回true
PHP8.0及更高版本
由于PHP8.0,您可以使用str_以开头,而str_以函数结尾
echo str以($str,“|”)开头;
PHP 8.0之前的版本
函数startsWith($haystack,$needle){
$length=strlen($needle);
返回substr($haystack,0,$length)==$pinder;
}
函数endsWith($haystack,$needle){
$length=strlen($needle);
如果(!$length){
返回true;
}
返回substr($haystack,-$length)==$pinder;
}