Skip to content

Instantly share code, notes, and snippets.

Avatar

Kerem Güneş k-gun

View GitHub Profile
View get_attrs.php
<?php
$html = '<img class="medium" src="/images/image.jpg" width="100" height="75">';
// get attrs
preg_match_all('~([\w]+?)="(.*?)"~i', $html, $m);
$attrs = array_combine($m[1], $m[2]);
print_r($attrs);
// result
View avg.php
<?php
function avg(){
return array_sum(func_get_args()) / func_num_args();
}
function avgr($a){
return array_sum($a) / count($a);
}
View matchAll.js
if (typeof RegExp.prototype.matchAll !== "function") {
RegExp.prototype.matchAll = function(v) {
var tmp = ""+ this, // So this.toString
src = tmp.lastIndexOf("/"),
pattern = tmp.substring(1, src),
flags = tmp.substring(src + 1, tmp.length);
// Never forget "g", that prepends infinite loops for "while"
if (flags.indexOf("g") == -1) {
flags += "g";
View sublime-text-installation-instructions.md

Sublime Text 2 Installation Guide: Ubuntu 13.04 and Elementary OS Luna

1. Install Sublime Text 2

Open terminal

sudo add-apt-repository ppa:webupd8team/sublime-text-2
sudo apt-get update
sudo apt-get install sublime-text

2. Install Sublime Package Control

@k-gun
k-gun / ctype_print_unicode.php
Last active Apr 24, 2020
PHP "ctype_print" for unicode (UTF-8)...
View ctype_print_unicode.php
<?php
define("CTYPE_PRINT_UNICODE_PATTERN", "~^[\pL\pN\s\"\~". preg_quote("!#$%&'()*+,-./:;<=>?@[\]^_`{|}´") ."]+$~u");
function ctype_print_unicode($input) {
return preg_match(CTYPE_PRINT_UNICODE_PATTERN, $input);
}
print ctype_print_unicode("3 muços?"); // 1
View parseQueryString.js
// Handles also array params well
function parseQueryString(query) {
var pars = (query != null ? query : "").replace(/&+/g, "&").split('&'),
par, key, val, re = /^([\w]+)\[(.*)\]/i, ra, ks, ki, i = 0,
params = {};
while ((par = pars.shift()) && (par = par.split('=', 2))) {
key = decodeURIComponent(par[0]);
// prevent param value going to be "undefined" as string
val = decodeURIComponent(par[1] || "").replace(/\+/g, " ");
View extract_path.php
function extract_array_path($path, $value = null, $base = null) {
if ($base === null) {
static $base = array();
}
$exp = explode('.', $path);
$tmp =& $base;
foreach($exp as $i) {
$tmp =& $tmp[$i];
}
@k-gun
k-gun / .htaccess
Last active Aug 29, 2015
Proper .htaccess configrations for subdir-based virtual server (Apache/2.4.7 (Ubuntu)).
View .htaccess
<VirtualHost *:80>
ServerName foo.com.local
DocumentRoot /var/www/foo.com/public
<Directory /var/www/foo.com/public>
Options +FollowSymLinks
DirectoryIndex index.php
AllowOverride all
Require all granted
View bbcode_convert.php
$str = <<<EOT
Lorem [a href="#"]ipsum[/a] [hr color="red" /] dolor...
[style].foo{color:#fff}[/style]
EOT;
function bbcode_convert($content) {
// remove style|script
$content = preg_replace(
'~(\[(style|sctript)\s?.*\](.*)\[/(\\2)\]|\[(%s)\s?.*/\])~ims', '', $content);
@k-gun
k-gun / reduce_slashes.php
Last active Aug 29, 2015
Reduce duplicated slashes.
View reduce_slashes.php
<?php
$url = "https://foo.com//bar/////ii?a=1";
$url = preg_replace("~(?<!:)/+~", "/", $url);
print($url); # https://foo.com/bar/ii?a=1
You can’t perform that action at this time.