Skip to content

Instantly share code, notes, and snippets.

View hongster's full-sized avatar

Hong hongster

View GitHub Profile
@hongster
hongster / build_number_gen.php
Last active April 12, 2019 07:40
Generating build number from git branch name and commit count.
<?php
/**
* Generate a build number based on Git commit count and current branch.
* @return string Format: "{$branch}.{$commitCount}"
*/
public function buildNumber()
{
$branch = exec('git rev-parse --abbrev-ref HEAD');
$commitCount = exec('git rev-list HEAD --count');
@hongster
hongster / easier_password.php
Last active October 2, 2015 11:32
Generate password/key that avoids user mistakes.
<?php
/**
* Generate random password of given length.
*
* @param int $length
* @return string
*/
private function randomPassword($length = 6) {
// Non-ambiguous chars
$alphabet = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
@hongster
hongster / alias.sh
Last active June 13, 2022 10:11
Yet another set of aliases.
alias ..='cd ..'
alias grep='grep --color=auto'
alias mkdir='mkdir -p -v'
alias more='less'
alias ping='ping -c 5'
alias view='vim -R'
alias scat='sudo cat'
alias sudo='sudo '
alias svim='sudo vim'
@hongster
hongster / non-ambiguous_password.php
Last active October 2, 2015 11:33
Avoid using characters like (0,o,O,1,i,l) in generating random passwords.
<?php
function randomPassword($length = 6) {
// Non-ambiguous chars
$alphabet = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
$alphamax = strlen($alphabet) - 1;
$password = '';
for ($i = 0; $i < $length; $i++) {
$password .= $alphabet[mt_rand(0, $alphamax)];
}
@hongster
hongster / Change Character Encoding And Represent In Hexadecimal
Last active August 29, 2015 14:17
PHP encodes string in UTF-8. This code converts it to Unicode, and then represent it as hexadecimal.
<?php
function strToHex($string){
$hex = '';
for ($i=0; $i<strlen($string); $i++){
$ord = ord($string[$i]);
$hexCode = dechex($ord);
$hex .= substr('0'.$hexCode, -2);
}
return $hex;
}
@hongster
hongster / Unicode Hexadecimal To UTF-8 String Conversion
Created March 23, 2015 06:40
Combine hex-to-string function with encoding conversion. In this example, the source string Unicode string encoded in hexadeciaml. The following code decodes it and convert it to UTF-8 string.
<?php
function hexToStr($hex){
$string='';
for ($i=0; $i < strlen($hex)-1; $i+=2){
$string .= chr(hexdec($hex[$i].$hex[$i+1]));
}
return $string;
}
$s = '6df157335e0253575c71533a53576d7759279053003100300037003953f7002000200068007400740070003a002f002f007700770077002e006700700073002e0063006f006d002f006d00610070002e0061007300700078003f006c00610074003d00320033002e0031003200330026006c006e0067003d003100310033002e003100320033';
@hongster
hongster / PHP.sublime-build
Created December 19, 2014 01:34
PHP Syntax Check In Sublime Text 2
/**
* Create a custom [build system](http://bit.ly/17IKkU9) to provide PHP syntax checking.
* Save this file as ~/.config/sublime-text-2/Packages/User/PHP.sublime-build
*
* Usage
* - In the editor, select a tab with PHP source code.
* - Press F7 or CTRL + B to run PHP syntax check.
* - The status will be be displayed in the console at the bottom.
* - If it shows error, press F4, and the editor cursor will be brought to the relevant line in the source code.
*/
@hongster
hongster / API Helper
Last active August 29, 2015 14:01
Simple AJAX call helper, with lock mechanism to prevent duplicated concurrent connections. It sends data in JSON format, you can easily adapt it to to your need. Dependencies: jQuery ($.ajax) and UnderscoreJS. You can easily remove UnderscoreJS code.
/**
* Simple AJAX call helper, with lock mechanism to prevent duplicated concurrent connections.
* It sends data in JSON format, you can easily adapt it to to your need.
* Dependencies: jQuery ($.ajax) and UnderscoreJS. You can easily remove UnderscoreJS code.
*/
var API = new function() {
var self = this;
// Lock to prevent duplicated concurrent API calls.
self.locks = {};