Skip to content

Instantly share code, notes, and snippets.

View ichiriac's full-sized avatar
🚀

Ioan CHIRIAC ichiriac

🚀
View GitHub Profile
@ichiriac
ichiriac / jPrototype.php
Last active December 21, 2015 20:49
An experiment to implement the Javascript Prototype principle in PHP 5.3
<?php
class jPrototype {
protected static $prototype = array();
public static function prototype() {
if ( !self::$prototype ) {
self::$prototype = new self();
}
return self::$prototype;
}
public function __set($property, $value) {
<?php
function renderTemplate($template, $data) {
$data_keys = array_map(function($key) {
return '%' . $key . '%';
}, array_keys($data));
$data_values = array_values($data);
return str_replace($data_keys, $data_values, $template);
}
@ichiriac
ichiriac / sanitize.php
Created December 16, 2012 13:29
This function sanitize strings by removing accentuation, converting UTF8 to ASCII - helpfull for encoding URLs
/**
* Function: sanitize
* Returns a sanitized string, typically for URLs.
*
* Parameters:
* $string - The string to sanitize.
* $lowercase - Force the string to lowercase?
* $alnum - If set to *true*, will remove all non-alphanumeric characters.
*/
function sanitize($string, $lowercase = true, $alnum = false) {
@ichiriac
ichiriac / short-fibo.php
Created December 9, 2012 12:11
fibonnaci short version
<?php
/**
* Fibonnaci short algorithm
* @link http://fr.wikipedia.org/wiki/Suite_de_Fibonacci
* @author I. CHIRIAC
*/
function fibo( $i ) {
return (
$i <= 1 ? $i :
(
@ichiriac
ichiriac / hash.php
Created August 21, 2012 21:30
A strong hashing function
<?php
/**
* This function is distributed under the MIT Open Source License.
* @author Ioan CHIRIAC
* @link https://github.com/ichiriac
*/
function generate_password($password, $salt = 'your-secret-salt', $algo = 'sha256')
{
// split the password
$password = str_split($password, ceil(strlen($password)/2));
@ichiriac
ichiriac / beaba-sample-rest.php
Created August 15, 2012 18:48
A beaba REST sample
<?php
// include the beaba framework before ...
$app = new beaba\core\WebApp(array(
'layouts' => array(
'profile/view' => function( $app, $data ) {
echo '<h1>Profile</h1>';
echo '<p> Name : ' . $data['name'] . '</p>';
echo '<p> Email : ' . $data['email'] . '</p>';
}
),
@ichiriac
ichiriac / merge-array.php
Created August 15, 2012 09:00
Clean way to recursivelly merge an array (secure & fast way)
<?php
/**
* An array merging helper
* @params array $original
* @params array $additionnal
* @return array
*/
function merge_array( $original, $additionnal )
{
if ( empty($additionnal) ) return $original;
@ichiriac
ichiriac / command-line.php
Created August 13, 2012 17:25
Defines an alternative function for *HANDY* getopts PHP native function ....
<?php
/**
* Reads automatically arguments options
* @return array
*/
function get_opts() {
$result = array();
$key = null;
$value = null;
$asize = count($_SERVER['argv']);