Skip to content

Instantly share code, notes, and snippets.

var url = 'http://sample.com'
var ws = new WebSocket(url);
var msg = "hi, this is simple message.";
ws.onopen = function(evt) {
ws.send(msg);
};
ws.onmessage = function(evt) {
// handle this message
console.log(evt.data);
};
// Fastest way to move first element to the end of an Array
var array = [8,1,2,3,4,5,6,7];
array.push(array.shift()); // results in [1, 2, 3, 4, 5, 6, 7, 8]
// https://stackoverflow.com/a/20385895/2618535
// jQuery Add Class, Delay, Remove Class
$('.animated-selector').addClass('hidden').stop().delay(500).queue(function(){
$(this).removeClass('hidden');
});
// https://gist.github.com/randyjensen/6223653

PDO loop for updating multiple rows

UPDATE website
    SET http_code = CASE id_website
        WHEN 1 THEN 200
        WHEN 2 THEN 201
        WHEN 3 THEN 202
    END,
 link_exists = CASE id_website

polyfill for curl_file_create

if (!function_exists('curl_file_create')) {
    function curl_file_create($filename, $mimetype = '', $postname = '') {
        return "@$filename;filename="
            . ($postname ?: basename($filename))
            . ($mimetype ? ";type=$mimetype" : '');
    }
}

How to json_encode array with cyrilic accents?

// преобразование utf8 в cp1251
$current_charset = 'windows-1251';
array_walk_recursive($m_post, function(&$value) use ($current_charset) {
  $value = iconv($current_charset, 'UTF-8', $value);
});

$str_post = array('str_post' => json_encode($m_post));
/**
* close all open xhtml tags at the end of the string
*
* @author Milian Wolff <[url]http://milianw.de[/url]>
* @param string $html
* @return string
*/
function closetags($html){
#put all opened tags into an array
preg_match_all("#<([a-z]+)( .*)?(?!/)>#iU",$html,$result);

PHP json_encode encoding numbers as strings

$arr = array( 'row_id' => '1', 'name' => 'George' );
echo json_encode( $arr, JSON_NUMERIC_CHECK ); // {"row_id":1,"name":"George"}

https://stackoverflow.com/a/6608413/2618535