Skip to content

Instantly share code, notes, and snippets.

View Stichoza's full-sized avatar
💻
Coding...

Levan Velijanashvili Stichoza

💻
Coding...
View GitHub Profile
@Stichoza
Stichoza / strip_strings.php
Created October 25, 2012 13:47
A simple PHP function to strip strings and numbers.
<?php
function strip_string($str, $case) {
switch ($case) {
case "az": return preg_replace("/[^a-zA-Z]+/", "", $str);
case "num": return preg_replace("/[^0-9]+/", "", $str);
case "num+az": default: return preg_replace("/[^a-zA-Z0-9]+/", "", $str);
}
}
?>
@Stichoza
Stichoza / email_username_validation.php
Last active October 12, 2015 01:48
PHP E-mail and Username Validation
<?php
function is_valid_email($s) {
return preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/', $s);
}
function is_valid_username($s) {
return preg_match('/^[A-Za-z0-9-_]+$/', $s);
}
?>
@Stichoza
Stichoza / substr_count_for_arrays.php
Created October 25, 2012 13:57
PHP substr_count for arrays (needle array)
<?php
function substr_count_array($haystack, $needle) {
$count = 0;
$haystack = strtolower($haystack);
foreach ($needle as $substring) {
$count += substr_count($haystack, strtolower($substring));
}
return $count;
}
?>
@Stichoza
Stichoza / .htaccess
Last active October 12, 2015 01:48
HTAccess RewriteEngine (Handle URLs)
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /controller.php [L]
@Stichoza
Stichoza / simple_php_pagination.php
Created November 9, 2012 15:05
Simple PHP Pagination (one function)
<?php
/**
*
* Simple PHP Pagination
*
* @author Stichoza
* @link http://stichoza.com/
* @email admin@stichoza.com
* @version 1.0
@Stichoza
Stichoza / array_sort_2d.php
Created November 26, 2012 10:51
Sort 2D Arrays in PHP
<?php
/* Sort 2D Arrays
*
* Example:
* $user_array = Array(
* Array("id" => 521, "name" => "Aboriginal Anemia"),
* Array("id" => 281, "name" => "Chemical Crap"),
* Array("id" => 596, "name" => "Bloody Baphomet")
* );
@Stichoza
Stichoza / fbShare.js
Created February 13, 2013 06:56
Facebook share popup JS function, popup window in screen center.
function fbShare(url, title, descr, image, winWidth, winHeight) {
var winTop = (screen.height / 2) - (winHeight / 2);
var winLeft = (screen.width / 2) - (winWidth / 2);
window.open('http://www.facebook.com/sharer.php?s=100&p[title]=' + title + '&p[summary]=' + descr + '&p[url]=' + url + '&p[images][0]=' + image, 'sharer', 'top=' + winTop + ',left=' + winLeft + ',toolbar=0,status=0,width=' + winWidth + ',height=' + winHeight);
}
@Stichoza
Stichoza / php_parse_request.php
Last active December 14, 2015 02:19
Parse POST/GET requests by one simple function.
<?php
function parse_request($vars, $encode_html = false, $return = false) {
$vars = explode(",", $vars);
if ($return) {
foreach ($vars as $value) {
$value = trim($value); // trim extra spaces
$result[$value] = get_var($value, $encode_html);
}
return $result; // returns associative array
} else {
@Stichoza
Stichoza / php_replace_identifiers.php
Last active December 14, 2015 07:09
Easily replace variable-like tags in string using associative array's keys and values.
<?php
# usage: $array = array("name" => "Stichoza", "website" => "stichoza.com");
# echo replace_identifiers("Visit {{name}}'s website at {{website}}", $array);
function replace_identifiers($string, $assoc_array) {
foreach ($assoc_array as $key => $value) {
$string = str_replace("{{".$key."}}", $value, $string);
}
return $string;
}
?>
@Stichoza
Stichoza / (Debian) Preferences.sublime-settings
Last active January 1, 2016 23:59
My custom settings for Sublime Text 3 on Linux (Debian Wheezy) and Mac OSX (Yosemite)
{
"color_scheme": "Packages/Color Scheme - Default/Monokai Bright.tmTheme",
"font_face": "Consolas",
"font_options":
[
"subpixel_antialias",
"gray_antialias"
],
"font_size": 12,
"highlight_line": true,