您的位置 首页 php

php 检查字符串是否以指定的字符开头或结尾

/**
 * 检查 字符串 是否以某个字符串开头
 * @param string $haystack 被检查的字符串
 * @param string $needles 需要包含的字符串
 * @param bool $strict 为true 则检查时区分大小写
 */
 static function startsWith($haystack, $needles, $strict = true)
 {
 // 不区分大小写的情况下 全部转为小写
 if (!$strict) $haystack = mb_strtolower($haystack);

 // 支持以数组方式传入 needles 检查多个字符串
  foreach  ((array)$needles as $needle) {
 if (!$strict) $needle = mb_strtolower($needle);
 if ($needle != '' && mb_strpos($haystack, $needle) === 0) {
 return true;
 }
 }
 return false;
 }

 /**
 * 检查字符串是否以某个字符串结尾
 * @param string $haystack 被检查的字符串
 * @param string $needles 需要包含的字符串
 * @param bool $strict 为true 则检查时区分大小写
 */
 static function endsWith($haystack, $needles, $strict = true)
 {
 // 不区分大小写的情况下 全部转为小写
 if (!$strict) $haystack = mb_strtolower($haystack);

 // 支持以数组方式传入 needles 检查多个字符串
 foreach ((array)$needles as $needle) {
 if (!$strict) $needle = mb_strtolower($needle);
 if ((string)$needle === mb_substr($haystack, -mb_strlen($needle))) {
 return true;
 }
 }
 return false;
 } 

文章来源:智云一二三科技

文章标题:php 检查字符串是否以指定的字符开头或结尾

文章地址:https://www.zhihuclub.com/77021.shtml

关于作者: 智云科技

热门文章

网站地图