basename
function basename(str) {
return str.split('/').reverse()[0];
}
in_array
function in_array(elem, arr, i) {// 代码来自 jquery 1.11.3,返回索引或-1
var len;
if (arr) {
len = arr.length;
i = i ? i < 0 ? Math.max(0, len + i) : i : 0;
for (; i < len; i++) {
// Skip accessing in sparse arrays
if (i in arr && arr[i] === elem) {
return i;
}
}
}
return -1;
}
time
function time(){
return (new Date()).getTime() / 1000;
}
date
/**
* @param format 只支持 'Y-m-d H:i:s','Y-m-d','H:i:s' 三种调用方式
* @param time 为空时,取当前时间
* @return 日期格式化的字符串
*/
function date(format,time){
var _temp = (time != null) ? new Date(time*1000) : new Date();
var _return = '';
if(/Y-m-d/.test(format)){
var _day = [_temp.getFullYear(),addzero(1 + _temp.getMonth()),addzero(_temp.getDate())];
_return = _day.join('-');
}
if(/H:i:s/.test(format)){
var _time = [addzero(_temp.getHours()),addzero(_temp.getMinutes()),addzero(_temp.getSeconds())];
_return += ' ' +_time.join(':');
}
return _return;
function addzero(i){
if(i<=9){
return '0' + i;
}else{
return i;
}
}
}
strtotime
function strtotime(str){
var _arr = str.split(' ');
var _day = _arr[0].split('-');
_arr[1] = (_arr[1] == null) ? '0:0:0' :_arr[1];
var _time = _arr[1].split(':');
for (var i = _day.length - 1; i >= 0; i--) {
_day[i] = isNaN(parseInt(_day[i])) ? 0 :parseInt(_day[i]);
};
for (var i = _time.length - 1; i >= 0; i--) {
_time[i] = isNaN(parseInt(_time[i])) ? 0 :parseInt(_time[i]);
};
var _temp = new Date(_day[0],_day[1]-1,_day[2],_time[0],_time[1],_time[2]);
return _temp.getTime() / 1000;
}
implode
function implode(glue, pieces){
return pieces.join(glue);
}
explode
function explode(separator, string){
return string.split(separator);
}
$_GET
function $_GET(name){
var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
return results[1] || 0;
}
验证身份证号是否正确
// 是否身份证号
// 15位用正则验证,18位用校验码
// https://wapbaike.baidu.com/item/身份证校验码
// 用法 '101101199912316789'.isIdCard() ? '正确' : '错误'
String.prototype.isIdCard = function () {
var id = this;
var a = id.toUpperCase().split('');
var c = '7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2'.split(',');
var d = '10X98765432'.split('');
var s = 0;
var l = a.length;
var v = a[l - 1];
var n = /^\d{6}(18|19|20)\d{2}[01]\d[0123]\d\d{3}(\d|X|x)$/i.test(id); // 18位验证
var f = /^\d{6}\d{2}[01]\d[0123]\d\d{3}$/i.test(id); // 15位验证
for (var i = 0; i < l - 1; i++) {
s += a[i] * c[i];
}
r = s % 11;
r = d[r];
return f || (n && r == v); // 通过15位正则 或者 (通过18位正则并且校验码正确)
}
截取字符串后添加省略号
var cutstr = function (string, start, length, usetail) {
var newString = "";
var suffix = "";
if ('string' == typeof (string)) {
newString = string.substr(start, length);
// 结尾的省略号
var idx = string.indexOf(newString);
if (newString.length < string.substr(idx).length) {
suffix = "…";
}
// 是否关闭了“添加省略号功能”
if (false == usetail) {
suffix = "";
} else {
if ("" != suffix) { // 为省略号留个位置
newString = newString.substr(0, newString.length - 1);
}
}
// 结果
newString = newString + suffix;
}
return newString;
}