Skip to content

Instantly share code, notes, and snippets.

<?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
@krmgns
krmgns / avg.php
Last active December 19, 2015 09:58
<?php
function avg(){
return array_sum(func_get_args()) / func_num_args();
}
function avgr($a){
return array_sum($a) / count($a);
}
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";

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

@krmgns
krmgns / ctype_print_unicode.php
Last active December 3, 2020 16:17
PHP "ctype_print" for unicode (UTF-8)...
<?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
// 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, " ");
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];
}
@krmgns
krmgns / .htaccess
Last active August 29, 2015 14:13
Proper .htaccess configrations for subdir-based virtual server (Apache/2.4.7 (Ubuntu)).
<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
$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);
@krmgns
krmgns / reduce_slashes.php
Last active August 29, 2015 14:13
Reduce duplicated slashes.
<?php
$url = "https://foo.com//bar/////ii?a=1";
$url = preg_replace("~(?<!:)/+~", "/", $url);
print($url); # https://foo.com/bar/ii?a=1