Skip to content

Instantly share code, notes, and snippets.

View vyspiansky's full-sized avatar

Ihor Vyspiansky vyspiansky

View GitHub Profile
@vyspiansky
vyspiansky / soft-word-wrap.php
Last active March 1, 2021 15:58
PHP: "soft" word wrap for URL
<?php
echo str_replace('/', '/<wbr>', $url);
?>
@vyspiansky
vyspiansky / cyrillic-string-comparison.js
Last active March 1, 2021 15:58
JavaScript: good cyrillic string comparison
var str1 = "Ґав не лови";
var str2 = "Єдність людини і природи";
var res = str1.localeCompare(str2); // result: -1;
@vyspiansky
vyspiansky / build-tree.php
Last active October 2, 2023 14:36
PHP: convert object / array to tree structure
<?php
/**
* The below functions take the single level array of items as an argument
* and turn it into a multidimensional array structure (tree),
* where each item has an array of sub-items.
*
* Each item should have at least an `id` and `parent_id`,
* where the `parent_id` is `0` if it's top level.
*
* Source: http://goo.gl/p2GybZ
@vyspiansky
vyspiansky / jquery-password-strength.js
Last active March 1, 2021 15:58
jQuery: password strength
/*
* Source: http://css-tricks.com/snippets/jquery/password-strength/
*/
$('#pass').keyup(function(e) {
var strongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\\W).*$", "g");
var mediumRegex = new RegExp("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g");
var enoughRegex = new RegExp("(?=.{6,}).*", "g");
if (false == enoughRegex.test($(this).val())) {
$('#passstrength').html('More Characters');
} else if (strongRegex.test($(this).val())) {
@vyspiansky
vyspiansky / jquery-fixed-image-resize.js
Last active March 1, 2021 15:59
jQuery: fixed image resize
/*
* Source: http://snipplr.com/view/62552/mage-resize/
*/
$(window).bind("load", function() {
// IMAGE RESIZE
$('#product_cat_list img').each(function() {
var maxWidth = 120;
var maxHeight = 120;
var ratio = 0;
var width = $(this).width();
@vyspiansky
vyspiansky / jquery-sort-alphabetically-a-list-with-anchors.js
Last active March 1, 2021 16:00
jQuery: sort alphabetically a list with anchors
/*
* Source: http://goo.gl/bi40M3
*/
$(function() {
$.fn.sortList = function() {
var mylist = $(this);
var listitems = $('li', mylist).get();
listitems.sort(function(a, b) {
var compA = $(a).text().toUpperCase();
var compB = $(b).text().toUpperCase();
@vyspiansky
vyspiansky / random-record-from-model.php
Last active November 10, 2016 06:44 — forked from hugodotlau/gist:4031109
Yii: how can I get random values from model
<?php
// Source: http://goo.gl/NOCJKM
$max = Table::model()->count(array(
'condition' => 'status<:status',
'params' => array('status' => Table::STATUS_A)
));
$offset = rand(0, $max);
@vyspiansky
vyspiansky / detect-a-handheld-device.js
Last active March 1, 2021 16:01
JavaScript: how to detect a handheld device
if ( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
// some code...
}
@vyspiansky
vyspiansky / javascript-trim-function.js
Created March 26, 2014 09:17
JavaScript: trim function
function trim(str) {
var res = str.replace(/^\s+/, '');
return res.replace(/\s+$/, '');
}
@vyspiansky
vyspiansky / support-posfixed.js
Created March 27, 2014 21:11 — forked from scottjehl/support-posfixed.js
jQuery: test for CSS position: fixed support
/* jQuery-based feature test for CSS position: fixed support.
Modified from Kangax's test (http://kangax.github.com/cft/#IS_POSITION_FIXED_SUPPORTED) in two ways: 1) uses jQuery API, 2) remove document.body dependency.
Method changes aside, the required workaround for dropping document.body use was to set scroll using window.scrollTo instead of body.scrollTop.
//Testing notes:
iOS4.3: false (expected)
iOS5: true (expected)
Chrome latest: true (expected)
...more soon
*/