码迷,mamicode.com
首页 > Web开发 > 详细

PHP startsWith and endsWith

时间:2015-12-26 16:47:46      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

function startsWith($haystack, $needle) {
    // search backwards starting from haystack length characters from the end
    return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== FALSE;
}
function endsWith($haystack, $needle) {
    // search forward starting from end minus needle length characters
    return $needle === "" || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== FALSE);
}
startsWith("abcdef", "ab") -> true
startsWith("abcdef", "cd") -> false
startsWith("abcdef", "ef") -> false
startsWith("abcdef", "") -> true
startsWith("", "abcdef") -> false

endsWith("abcdef", "ab") -> false
endsWith("abcdef", "cd") -> false
endsWith("abcdef", "ef") -> true
endsWith("abcdef", "") -> true
endsWith("", "abcdef") -> false

 


Source page: http://stackoverflow.com/a/10473026

PHP startsWith and endsWith

标签:

原文地址:http://www.cnblogs.com/Jim-william/p/5078311.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!