Karim's Blog

Un peu de tout sur CSS, HTML, AngularJS, JavaScript, Php et le reste

Quelques Fonctions PHP en Javascript

Fonction : in_array

function in_array(needle, haystack, argStrict) {
// example 1: in_array('js', ['jquery', 'php', 'js']);
// returns 1: true
// example 2: in_array('php', {0: 'js', xyd: 'jquery', 1: 'css'});
// returns 2: false
// example 3: in_array(1, ['1', '2', '3']);
// example 3: in_array(1, ['1', '2', '3'], false);
// returns 3: true
// returns 3: true
// example 4: in_array(1, ['1', '2', '3'], true);
// returns 4: false

var key = '',
strict = !! argStrict;
//we prevent the double check (strict && arr[key] === ndl) || (!strict && arr[key] == ndl)
//in just one for, in order to improve the performance
//deciding wich type of comparation will do before walk array
if (strict) {
for (key in haystack) {
if (haystack[key] === needle) {
return true;
}
}
} else {
for (key in haystack) {
if (haystack[key] == needle) {
return true;
}
}
}

return false;
}

Fonction : checkdate

function checkdate(m, d, y) {

// example 1: checkdate(12, 31, 2000);
// returns 1: true
// example 2: checkdate(2, 29, 2001);
// returns 2: false
// example 3: checkdate(3, 31, 2008);
// returns 3: true
// example 4: checkdate(1, 390, 2000);
// returns 4: false

return m > 0 && m < 13 && y > 0 && y < 32768 && d > 0 && d <= (new Date(y, m, 0))
.getDate();
}

Fonction : time

function time() {

// example 1: timeStamp = time();
// example 1: timeStamp > 1000000000 && timeStamp < 2000000000
// returns 1: true
return Math.floor(new Date()
.getTime() / 1000);
}

Fonction : nl2br

function nl2br(str, is_xhtml) {

var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br ' + '/>' : '<br>';
return (str + '')
.replace(/([^>rn]?)(rn|nr|r|n)/g,
        '$1' + breakTag + '$2');
}

Fonction : wordwrap

function wordwrap(str, int_width, str_break, cut) {

// example 1: wordwrap('The quick brown fox jumped over the lazy dog.', 20, '<br />n');
// returns 1: 'The quick brown fox <br />njumped over the lazy<br />n dog.'
// example 2: wordwrap('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor.');
// returns 2: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod ntempor.'


var m = ((arguments.length >= 2) ? arguments[1] : 75);
var b = ((arguments.length >= 3) ? arguments[2] : 'n');
var c = ((arguments.length >= 4) ? arguments[3] : false);

var i, j, l, s, r;

str += '';

if (m < 1) {
return str;
}

for (i = -1, l = (r = str.split(/rn|n|r/))
.length; ++i < l; r[i] += s) {
for (s = r[i], r[i] = ''; s.length > m; r[i] += s.slice(0, j) + ((s = s.slice(j))
.length ? b : '')) {
j = c == 2 || (j = s.slice(0, m + 1)
.match(/S*(s)?$/))[1] ? m : j.input.length - j[0].length || c == 1 && m || j.input.length + (j = s.slice(m)
.match(/^S*/))[0].length;
}
}

return r.join('n');
}

Fonction :serialize

function serialize(mixed_value) {

var val, key, okey,
ktype = '',
vals = '',
count = 0,
_utf8Size = function(str) {
var size = 0,
i = 0,
l = str.length,
code = '';
for (i = 0; i < l; i++) {
code = str.charCodeAt(i);
if (code < 0x0080) {
size += 1;
} else if (code < 0x0800) {
size += 2;
} else {
size += 3;
}
}
return size;
};

_getType = function(inp) {
var match, key, cons, types, type = typeof inp;

if (type === 'object' && !inp) {
return 'null';
}
if (type === 'object') {
if (!inp.constructor) {
return 'object';
}

cons = inp.constructor.toString();
match = cons.match(/(w+)(/);
if (match) {
cons = match[1].toLowerCase();
}
types = ['boolean', 'number', 'string', 'array'];
for (key in types) {
if (cons == types[key]) {
type = types[key];
break;
}
}
}
return type;
};

type = _getType(mixed_value);

switch (type) {
case 'function':
val = '';
break;
case 'boolean':
val = 'b:' + (mixed_value ? '1' : '0');
break;
case 'number':
val = (Math.round(mixed_value) == mixed_value ? 'i' : 'd') + ':' + mixed_value;
break;
case 'string':
val = 's:' + _utf8Size(mixed_value) + ':"' + mixed_value + '"';
break;
case 'array':
case 'object':
val = 'a';

/*
if (type === 'object') {
var objname = mixed_value.constructor.toString().match(/(w+)()/);
if (objname == undefined) {
return;
} objname[1] = this.serialize(objname[1]); val = 'O' + objname[1].substring(1, objname[1].length - 1); }
*/

for (key in mixed_value) {
if (mixed_value.hasOwnProperty(key)) {
ktype = _getType(mixed_value[key]);
if (ktype === 'function') {
continue;
}

okey = (key.match(/^[0-9]+$/) ? parseInt(key, 10) : key);
vals += this.serialize(okey) + this.serialize(mixed_value[key]);
count++;
}
}
val += ':' + count + ':{' + vals + '}';
break;
case 'undefined':
// Fall-through
default:
// if the JS object has a property which contains a null value, the string cannot be unserialized by PHP
val = 'N';
break;
}
if (type !== 'object' && type !== 'array') {
val += ';';
}
return val;
}

Fonction : is_float

function is_float(mixed_var) {

// example 1: is_float(186.31);
// returns 1: true

return +mixed_var === mixed_var && (!isFinite(mixed_var) || !! (mixed_var % 1));
}

Fonction : empty

function empty(mixed_var) {

// example 1: empty(null);
// returns 1: true
// example 2: empty(undefined);
// returns 2: true
// example 3: empty([]);
// returns 3: true
// example 4: empty({});
// returns 4: true
// example 5: empty({'aFunc' : function () { alert('hello'); } });
// returns 5: false

var undef, key, i, len;
var emptyValues = [undef, null, false, 0, '', '0'];

for (i = 0, len = emptyValues.length; i < len; i++) {
if (mixed_var === emptyValues[i]) {
return true;
}
}
if (typeof mixed_var === 'object') {
for (key in mixed_var) {

//if (mixed_var.hasOwnProperty(key)) {
return false;
//}
}
return true;
}

return false;
}

Fonction : create_function

function create_function(args, code) {
// example 1: f = create_function('a, b', "return (a + b);");
// example 1: f(1, 2);
// returns 1: 3

try {
return Function.apply(null, args.split(',')
.concat(code));
} catch (e) {
return false;
}
}

Fonction : ip2long

function ip2long(IP) {
// example 1: ip2long('192.0.34.166');
// returns 1: 3221234342
// example 2: ip2long('0.0xABCDEF');
// returns 2: 11259375
// example 3: ip2long('255.255.255.256');
// returns 3: false

var i = 0;
IP = IP.match(
/^([1-9]d*|0[0-7]*|0x[da-f]+)(?:.([1-9]d*|0[0-7]*|0x[da-f]+))?(?:.([1-9]d*|0[0-7]*|0x[da-f]+))?(?:.([1-9]d*|0[0-7]*|0x[da-f]+))?$/i
);
if (!IP) {
return false;
}

IP[0] = 0;
for (i = 1; i < 5; i += 1) {
IP[0] += !! ((IP[i] || '').length);
IP[i] = parseInt(IP[i]) || 0;
}

IP.push(256, 256, 256, 256);
IP[4 + IP[0]] *= Math.pow(256, 4 - IP[0]);
if (IP[1] >= IP[5] || IP[2] >= IP[6] || IP[3] >= IP[7] || IP[4] >= IP[8]) {
return false;
}
return IP[1] * (IP[0] === 1 || 16777216) + IP[2] * (IP[0] <= 2 || 65536) + IP[3] * (IP[0] <= 3 || 256) + IP[4] * 1;
}

nb : il y aura d'autres mis à jours et de nouvelles fonctions !