Skip to content

Instantly share code, notes, and snippets.

View vhdm's full-sized avatar

Vahid vhdm

  • Iran
View GitHub Profile
@vhdm
vhdm / character_limiter.php
Created September 6, 2015 21:53
Character limiter
<?php
function character_limiter($str, $n = 500, $end_char = '&#8230;')
{
if (strlen($str) < $n)
{
return $str;
}
$str = preg_replace("/\s+/", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str));
if (strlen($str) <= $n)
{
@vhdm
vhdm / file_extension.php
Created September 6, 2015 21:46
Get file extension
<?php
$file = '/path/to/file.php';
$info = pathinfo($file);
echo $info['extension'];
?>
@vhdm
vhdm / get_image_size.php
Created September 6, 2015 21:42
Get image size
<?php
list($width, $height, $type, $attr) = getimagesize("img.jpg");
echo $width."<br>".$height."<br>".$type."<br>".$attr."<br>";
?>
@vhdm
vhdm / last_inserted_id.php
Created September 6, 2015 21:32
Get last inserted id
<?php
// Insert into database
mysql_query("INSERT INTO my_table (`column1`, `column2`) VALUES ('one', 'two')");
// Next Get what the auto incremented number was that was inserted
$id = mysql_insert_id();
// Display it...
echo $id;
?>
@vhdm
vhdm / hex_to_rgb_to_hex.php
Created September 6, 2015 21:29
Hex To RGB and RGB To Hex
<?php
error_reporting(0);
function toHex($N) {
if ($N==NULL) return "00";
if ($N==0) return "00";
$N=max(0,$N);
$N=min($N,255);
$N=round($N);
$string = "0123456789ABCDEF";
$val = (($N-$N%16)/16);
@vhdm
vhdm / human_size.php
Created September 6, 2015 21:25
Human readable file size
<?php
function human_size ($size) {
if ($size <= 1024) return $size.' Bytes';
else if ($size <= (1024*1024)) return sprintf('%d KB',(int)($size/1024));
else if ($size <= (1024*1024*1024)) return sprintf('%.2f MB',($size/(1024*1024)));
else return sprintf('%.2f Gb',($size/(1024*1024*1024)));
}
echo human_size(32768);
?>
@vhdm
vhdm / random_string.php
Last active September 6, 2015 21:33
Generate random string
<?php
function random_string($len=20){
$randstr = '';
srand((double)microtime()*1000000);
for($i=0;$i<$len;$i++){
$n = rand(48,120);
while (($n >= 58 && $n <= 64) || ($n >= 91 && $n <= 96)){
$n = rand(48,120);
}
$randstr .= chr($n);
@vhdm
vhdm / country_from_ip.php
Last active September 6, 2015 21:34
Get country from ip
<?
// Let me start off by saying I do not take full credit for this!
// I needed a way to get my traffic's country for Adverts...
// for some reason, json_decode wasn't working, so this is an alternative.
// json_decoder function from PHP.net
// file_get_contents_curl basic data retrieval function
// how to use:
// include('country.php');
// $userCountry=getTheCountry();
// output is 2 character country code, US, CA, etc...
@vhdm
vhdm / _pcalendar.php
Last active September 6, 2015 21:35
Persian calendar
<?php
require_once("pcalendar.php");
$datex = new jDateTime(true, true, 'Asia/Tehran');
echo $datex->date('Y-m-d',time());
?>
@vhdm
vhdm / date_and_time.txt
Created September 6, 2015 20:43
PHP provides over thirty-five case-sensitive characters that are used to format the date and time. These characters are
Day j Day of the Month, No Leading Zeros 1 - 31
Day d Day of the Month, 2 Digits, Leading Zeros 01 - 31
Day D Day of the Week, First 3 Letters Mon - Sun
Day l (lowercase 'L') Day of the Week Sunday - Saturday
Day N Numeric Day of the Week 1 (Monday) - 7 (Sunday)
Day w Numeric Day of the Week 0 (Sunday) - 6 (Saturday)
Day S English Suffix For Day of the Month st, nd, rd or th
Day z Day of the Year 0 - 365
Week W Numeric Week of the Year (Weeks Start on Mon.) 1 - 52
Month M Textual Representation of a Month, Three Letters Jan - Dec