Skip to content

Instantly share code, notes, and snippets.

@meglio
meglio / replace-email.php
Created March 21, 2011 19:37
Finds email / name in specific email provided by Segovia, and replaced FROM value with these values
<?php
define('FILE_IN', 'in.txt');
define('FILE_OUT', 'out.txt');
$original = file_get_contents(FILE_IN);
$res = preg_match('/Your Full Name:[^a-z]*(?P<fullName>[a-z ]+)[^a-z]*Email:[^a-z]*(?P<email>[1-9a-z@ \.]+)/is', $original, $matches);
if (!$res) exit;
@meglio
meglio / closures.php
Created August 17, 2011 22:49
Cancellable and transactionable behaviors for PHP
<?php
namespace core\utils;
/**
* @throws \Exception
* @param mixed $prepare
* @param mixed $doJob
* @param mixed $finalize
* @param mixed $cancel
@meglio
meglio / references-and-arrays.php
Created September 6, 2011 16:12
Unexpected behavior of arrays with referenced elements, in PHP
<?php
$arr = array(1, 5);
$a =& $arr[0]; //$a and $arr[0] are in the same reference set
$arr2 = $arr; //not an assignment-by-reference!
$arr2[0]++;
$arr2[1]++;
var_dump($a);
var_dump($arr);
@meglio
meglio / yeld.php
Created September 7, 2011 08:13
yeld in php (for arrays only)
<?php
namespace core\utils;
/**
* Loops over an array, calls expression on each its element and collects results into new array.
* @param mixed $array
* @param callback $expression Expression function taking $key and $value parameters.
* @return array
*/
@meglio
meglio / PSR-0-final-proposal.md
Created October 12, 2011 06:35 — forked from Thinkscape/PSR-0-final-proposal.md
PSR-0 Final Proposal (PHP Standards Working Group)

PSR-0 Final Proposal (PHP Standards Working Group)

The following describes the mandatory requirements that must be adhered to for autoloader interoperability.

Mandatory:

  • A fully-qualified namespace and class must have the following structure \ <Vendor Name> \ (<Namespace>)* \ <Class Name>
  • Each namespace must have a top-level namespace ("Vendor Name").
  • Each namespace can have as many sub-namespaces as it wishes.
  • Each namespace separator is converted to a DIRECTORY_SEPARATOR when loading from the file system.
  • Each "_" character in the CLASS NAME is converted to a DIRECTORY_SEPARATOR. The "_" character has no special meaning in the namespace.
@meglio
meglio / pmmedia-01.php
Created October 19, 2011 20:40
PMMEDIA Task 1
<?php
header('content-type: text/html; charset: utf-8');
/**
* Created by JetBrains PhpStorm.
* User: meglio
* Date: 19.10.11
* Time: 19:51
*
* TASK 1:
@meglio
meglio / pmmedia-10.html
Created October 19, 2011 21:24
PMMEDIA Task 10
<style type="text/css">
.test
{
background: url(http://www.google.com/intl/en_com/images/srpr/logo1w.png) no-repeat;
font-size: 26px;
font-weight: bold;
height: 200px;
border-left: 1px solid red;
border-top: 1px solid blue;
@meglio
meglio / StripJS.php
Created April 19, 2012 15:41
PHP class to strip javascripts from html
<?php
class StripJS {
const JS_ALLOW_HREF = 1;
const JS_ALLOW_SCRIPT = 2;
const JS_ALLOW_ATTR_ONX = 4;
/**
* Strips javascript from page. $allowed is bitwise combination of JS_ALLOW_HREF, JS_ALLOW_SCRIPT, JS_ALLOW_ATTR_ONX
* 0 - strip everything (no js allowed)
static function detect($path1, $path2, $colorPrecision = null, $gaussianBlur = false)
{
$img1 = imagecreatefrompng($path1);
$img2 = imagecreatefrompng($path2);
$w1 = imagesx($img1); $h1 = imagesy($img1);
$w2 = imagesx($img2); $h2 = imagesy($img2);
# Stop early if the size of images such that images can not be part of each other
if (($w1 > $w2 && $h2 > $h1))
@meglio
meglio / highlight-words.js
Created September 13, 2012 18:36
Function to highlight one or more words in a string
function highlight(str, words)
{
var before = '<strong class="highlight">', beforeLen = before.length,
after = '</strong>', afterLen = after.length
if (typeof words == "string")
words = [words]
if (words == null || words.length == 0)
return str