首页 > Other > 判断某字符串是否以给定字符串开头或结尾的PHP函数

判断某字符串是否以给定字符串开头或结尾的PHP函数

//使用: startsWith($req_sql, ‘delete’); // $req_sql字符串是否以delete开头
function startsWith($haystack,$needle,$case=false) {
if($case){return (strcmp(substr($haystack, 0, strlen($needle)),$needle)===0);}
return (strcasecmp(substr($haystack, 0, strlen($needle)),$needle)===0);
}

//使用: endsWith($req_sql, ‘limit 1’); // $req_sql字符串是否以limit 1结尾
function endsWith($haystack,$needle,$case=false) {
if($case){return (strcmp(substr($haystack, strlen($haystack) – strlen($needle)),$needle)===0);}
return (strcasecmp(substr($haystack, strlen($haystack) – strlen($needle)),$needle)===0);
}

分类:Other
  1. 还没有评论。
  1. No trackbacks yet.

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: