Skip to content

Instantly share code, notes, and snippets.

@Chengings
Chengings / .editorconfig
Last active April 18, 2024 11:30
Cheatsheets
trim_trailing_whitespace = true
@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 / 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: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: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