Skip to content

Instantly share code, notes, and snippets.

@Chengings
Chengings / gist:9597366
Last active August 29, 2015 13:57
Convert number with unit byte to bytes unit & get maximum file upload size of php
<?php
/**
* Convert number with unit byte to bytes unit
* @link https://en.wikipedia.org/wiki/Metric_prefix
* @param string $value a number of bytes with optinal SI decimal prefix (e.g. 7k, 5mb, 3GB or 1 Tb)
* @return integer|float A number representation of the size in BYTES (can be 0). otherwise FALSE
*/
function str2bytes($value) {
// only string
@Chengings
Chengings / gist:9599473
Created March 17, 2014 13:48
Escape php string to javascript string
<?php
/**
* Escape php string to javascript string
* @param string $str
* @return string escaped string
*/
function escape_javascript_string($str){
// if php supports json_encode, use it (support utf-8)
if (function_exists('json_encode')) {
@Chengings
Chengings / gist:9605009
Created March 17, 2014 18:12
Search in array value with partial match
<?php
/**
* Seach in array value, similar to in_array(), but has ability to search with partial match
* @param string $needle searched value
* @param array $haystack target array
* @param string $return_key if third parameter is set to TRUE, the function will return array key
* @return boolean|int|string if found, function will return TRUE, string or int of first founded key (if return_key set to TRUE)
* otherwise FALSE
*/
@Chengings
Chengings / gist:c7a4d4cae5abeb01b782
Last active August 29, 2015 14:15
Insert the string output to log file
<?php
private function _logfile($logName, $str2log)
{
$file= sys_get_temp_dir() . '/mydebug-' . date('Y-m-d') . '.log';
if (!$handle = fopen($file, 'a')) {
throw new Exception('Could not open file!');
}
$str2log = print_r($str2log, true);
$olog = print_r("{$logName}: {$str2log}", true) . PHP_EOL;
@Chengings
Chengings / .editorconfig
Last active July 25, 2025 16:46
Cheatsheets
trim_trailing_whitespace = true
@Chengings
Chengings / generic.md
Last active July 24, 2019 15:13
Settings
@Chengings
Chengings / style.css
Created January 1, 2019 07:11
Generic UI font
{
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
}

Generate an RSA 4096 bit key. Use option "-b 2048" to create 2048 bit

USAGE=''
ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa_"$USAGE"_"$(date +%Y-%m-%d)" -C "$(whoami)@$(hostname)_$(date +%Y-%m-%d)" 

Generate an ed25519 key with the new OpenSSH format rather than the PEM format (-o) options:

USAGE=''
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_"$USAGE"_$(date +%Y-%m-%d) -C "$(whoami)@$(hostname)_$(date +%Y-%m-%d)"