Skip to content

Instantly share code, notes, and snippets.

View vyspiansky's full-sized avatar

Ihor Vyspiansky vyspiansky

View GitHub Profile
@vyspiansky
vyspiansky / remote-file-exists.php
Last active March 1, 2021 16:01
PHP: file_exists for remote URL
<?php
// More details here:
// https://vyspiansky.github.io/2017/11/09/remote-file-exists-using-get-headers/
$file_headers = @get_headers($url);
if ($file_headers[0] == 'HTTP/1.0 404 Not Found'){ // or "HTTP/1.1 404 Not Found" etc.
$file_exists = false;
} else {
$file_exists = true;
@vyspiansky
vyspiansky / decode-html-entities.js
Created April 17, 2014 08:32
jQuery: decode html-entities
var decoded = $("<div/>").html(encodedStr).text();
@vyspiansky
vyspiansky / js-str-repeat-analogue.js
Created April 17, 2014 09:58
JavaScript: str_repeat analogue
// Source: http://goo.gl/xMnHZj
function str_repeat($string, $multiplier) {
return new Array($multiplier+1).join($string)
}
@vyspiansky
vyspiansky / dynamically-load.js
Last active March 1, 2021 16:02
JavaScript: dynamically loading (include) JavaScript and CSS
// Code snippets for dynamically loading JavaScript and CSS
// Copyright : This script has been placed in the public domain.
// Gist : https://gist.github.com/livibetter/5211293
// Blog post : http://blog.yjl.im/2013/03/dynamically-loading-javascript-and-css.html
// Last update : 2013-03-21T07:35:25Z
// Author : Yu-Jie Lin
// Website : http://yjl.im
function load_CSS(src, cb) {
var link = document.createElement('link');
@vyspiansky
vyspiansky / calculate-hours-since-row-created.sql
Last active March 1, 2021 16:02
MySQL: calculate hours since row created
# Source: http://goo.gl/aFVBWn
SELECT DATEDIFF(now(), created_at) * 24 AS hours_passed FROM table_name;
@vyspiansky
vyspiansky / image-src-regexpr.php
Last active April 2, 2024 19:37
PHP: get image src attribute (regular expression)
<?php
// Source: http://goo.gl/qyLFbg
$html = '<img border="0" src="/images/image.jpg" alt="Image" width="100" height="100" />';
preg_match( '@src="([^"]+)"@' , $html, $match );
$src = array_pop($match);
// will return /images/image.jpg
@vyspiansky
vyspiansky / form-serialize-without-empty-fields.js
Created April 28, 2014 08:36
jQuery: how to use form.serialize but exclude empty fields
// http://goo.gl/uO78lh
$("#myForm :input[value!='']").serialize();
@vyspiansky
vyspiansky / yii-parameterized-named-scopes.php
Last active August 29, 2015 14:00
Yii: parameterized named scopes
<?php
// Source: http://goo.gl/ZZrUWw
// See also: http://goo.gl/PCxJmO
public function relations() {
return array(
'rel' => array(
self::MANY_MANY, 'MyClass', 'table(id_1, id_2)',
'condition' => 'some conditions'
)
@vyspiansky
vyspiansky / jquery-select-first-last-list-item.js
Created May 5, 2014 14:44
jQuery: select first and/or last list items
$(function() {
// Add first and last menu item classes
$('ul li:first-child').addClass('first-item');
$('ul li:last-child').addClass('last-item');
});
@vyspiansky
vyspiansky / jquery-textarea-maxlength.js
Created May 7, 2014 07:46
jQuery: maxlength for textarea
// Source: http://goo.gl/Afq20G
$(document).ready(function() {
$('textarea[maxlength]').keyup(function(){
//get the limit from maxlength attribute
var limit = parseInt($(this).attr('maxlength'));
//get the current text inside the textarea
var text = $(this).val();
//count the number of characters in the text