Skip to content

Instantly share code, notes, and snippets.

View gchacaltana's full-sized avatar
🎯
Focusing

Gonzalo Chacaltana gchacaltana

🎯
Focusing
  • Perú
View GitHub Profile
@gchacaltana
gchacaltana / jsonpp.php
Last active August 29, 2015 13:59
JSON Pretty Print in PHP 5.2
<?php
function jsonpp($json, $istr = ' ')
{
$result = '';
for($p=$q=$i=0; isset($json[$p]); $p++)
{
$json[$p] == '"' && ($p>0?$json[$p-1]:'') != '\\' && $q=!$q;
if(strchr('}]', $json[$p]) && !$q && $i--)
{
@gchacaltana
gchacaltana / generateGuidv4.php
Last active January 2, 2016 12:08
Get GUID v4
//Generate GUID v4
public static function generateGuidv4()
{
return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
// 32 bits for "time_low"
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
// 16 bits for "time_mid"
mt_rand(0, 0xffff),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 4
@gchacaltana
gchacaltana / randomString.php
Last active January 2, 2016 12:09
Get random string
<?php
//declaramos la funcion : randomString
function randomString($length, $type = '') {
// Seleccionamos el tipo de caracteres que deseas que devuelva el string
switch ($type) {
case 'num':
// Solo cuando deseas que devuelva numeros.
$salt = '1234567890';
break;
case 'lower':
#Newbie programmer
def factorial(x):
if x == 0:
return 1
else:
return x * factorial(x - 1)
print factorial(6)
#First year programmer, studied Pascal
@gchacaltana
gchacaltana / configuration nginx file with php-fpm
Last active January 3, 2016 14:19
configuration nginx file with php-fpm
server {
listen 443;
server_name api.project_name.pe;
ssl on;
ssl_certificate /etc/nginx/ssl/server.crt;
ssl_certificate_key /etc/nginx/ssl/server.key;
access_log /var/log/nginx/api.project_name.access.log;
error_log /var/log/nginx/api.project_name.error.log;
@gchacaltana
gchacaltana / checkDateTimeIsoFormat.php
Last active January 3, 2016 14:39
checking datetime format ISO 8601
<?php
/**
* return true if datetime format is valid.
* @author Gonzalo Chacaltana Buleje <gchacaltanab@outlook.com>
* @param datime $datetime, ISO 8601 format: YYYY-mm-dd HH:mm:ss
*/
public static function checkDatetimeIsoFormat($datetime)
{
if (preg_match("/^(\d{4})-(\d{2})-(\d{2}) ([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/", $datetime)) {
return true;
@gchacaltana
gchacaltana / getSizeStringBytes.php
Last active January 4, 2016 04:09
Count the number of bytes of a given string. Input string is expected to be ASCII or UTF-8 encoded. Warning: the function doesn't return the number of chars in the string, but the number of bytes.
<?php
/**
*
* @param string $str The string to compute number of bytes
*
* @return The length in bytes of the given string.
*/
function getSizeStringBytes($str){
// STRINGS ARE EXPECTED TO BE IN ASCII OR UTF-8 FORMAT
// apache_request_headers replicement for nginx
if (!function_exists('apache_request_headers')) {
function apache_request_headers() {
foreach($_SERVER as $key=>$value) {
if (substr($key,0,5)=="HTTP_") {
$key=str_replace(" ","-",ucwords(strtolower(str_replace("_"," ",substr($key,5)))));
$out[$key]=$value;
}else{
$out[$key]=$value;
}
@gchacaltana
gchacaltana / checkTimeZone.php
Last active April 6, 2017 10:28
checking timezone
<?php
/**
* return true if timezone exist.
* @author Gonzalo Chacaltana Buleje <gchacaltanab@outlook.com>
* @param string $timezone, timezone. example: America/Lima
*/
public static function checkTimeZone($timezone)
{
if (!empty($timezone)) {
$listTimeZone = DateTimeZone::listIdentifiers(DateTimeZone::ALL);
@gchacaltana
gchacaltana / example_http_response_head_method.txt
Created June 28, 2017 04:23
Example HTTP Response HEAD Method
HTTP/1.1 200 OK
Date: Tue, 27 Jun 2017 12:18:53 GMT
Server: Apache/2.2.14 (Win64)
Last-Modified: Mon, 26 Jun 2017 19:15:56 GMT
ETag: "34aa387-d-1568eb00"
Vary: Authorization,Accept
Accept-Ranges: bytes
Content-Length: 88
Content-Type: text/html
Connection: Closed