Skip to content

Instantly share code, notes, and snippets.

View alemohamad's full-sized avatar

Ale Mohamad ⌘ alemohamad

View GitHub Profile
@alemohamad
alemohamad / zero_pad.php
Created December 14, 2013 17:37
Zero Padded Numbers
<?php
function setZeroPaddedNumber($value, $padding) {
return str_pad($value, $padding, "0", STR_PAD_LEFT);
}
/**
* Usage:
*
* echo setZeroPaddedNumber(123, 4);
@alemohamad
alemohamad / time_ago.php
Created December 14, 2013 19:36
Time Ago Function
<?php
// This can be used for comments and other from of communication to tell the time ago
// instead of the exact time which might not be correct to some one in another time zone.
// The function only uses unix time stamp like the result of time();
function _ago($tm, $rcs = 0) {
$cur_tm = time(); $dif = $cur_tm-$tm;
@alemohamad
alemohamad / password_strength.php
Created December 14, 2013 19:45
Password Strength
<?php
/**
* @param String $string
* @return float
*
* Returns a float between 0 and 100. The closer the number is to 100 the
* the stronger password is; further from 100 the weaker the password is.
*/
@alemohamad
alemohamad / geolocation_ip.php
Created December 14, 2013 20:11
Get Geo-IP Information
<?php
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
@alemohamad
alemohamad / hex2rgb.php
Created December 14, 2013 20:24
Convert HEX to RGB
<?php
function hex2rgb($colour)
{
if ($colour[0] == '#') {
$colour = substr($colour, 1);
}
if (strlen($colour) == 6) {
list($r, $g, $b) = array(
@alemohamad
alemohamad / text2bbc.php
Created December 14, 2013 20:27
Bulletin Board Code (BBC)
<?php
function text2bbc($text)
{
$find = array(
'~\[b\](.*?)\[/b\]~s',
'~\[i\](.*?)\[/i\]~s',
'~\[u\](.*?)\[/u\]~s',
'~\[size=(.*?)\](.*?)\[/size\]~s',
'~\[color=(.*?)\](.*?)\[/color\]~s'
@alemohamad
alemohamad / br2nl.php
Created December 14, 2013 20:28
Convert BR to Newline
<?php
function br2nl( $input ) {
return preg_replace('/<br(\s+)?\/?>/i', "\n", $input);
}
@alemohamad
alemohamad / data_uri.php
Created December 14, 2013 20:30
Create Data URI's
<?php
// These can be useful for embedding images into HTML/CSS/JS
// to save on HTTP requests, at the cost of maintainability.
function data_uri($file, $mime)
{
$contents = file_get_contents($file);
$base64 = base64_encode($contents);
echo "data:$mime;base64,$base64";
@alemohamad
alemohamad / metatags.php
Created December 14, 2013 20:41
Discover and Display Meta Tags
<?php
$tags = get_meta_tags('http://clarin.com/');
echo $tags["language"]; // es
echo $tags["distribution"] // Global
echo $tags["robots"] // all
echo $tags["author"] // Clarin.com
echo $tags["classification"] // noticias, información, videos, diario, newspaper
echo $tags["description"] // Clarin.com. Noticias de la Argentina y el mundo. Información actualizada las 24 horas y en español.
@alemohamad
alemohamad / available.php
Created December 14, 2013 20:42
Check if a website is available
<?php
ini_set("default_socket_timeout", "05");
set_time_limit(5);
$f = fopen("http://github.com", "r");
$r = fread($f, 1000);
fclose($f);
if (strlen($r) > 1) {