Skip to content

Instantly share code, notes, and snippets.

@bg5sbk
bg5sbk / utf8_length.php
Created December 9, 2013 06:34
Get UTF-8 length of the text.
<?php
//
// Get UTF-8 length of the text.
// Return false when the text not UTF-8 encoding.
//
function utf8_length($text)
{
$text_len = strlen($text);
$utf8_len = 0;
@bg5sbk
bg5sbk / utf8_check.php
Last active December 30, 2015 16:29
Test whether the text is UTF-8 encoding by PHP.
<?php
//
// Test whether the text is UTF-8 encoding
//
// Result:
// 1 - Has BOM head
// 2 - Pure UTF-8 context
// 3 - More likely to be UTF-8 content
// 4 - Less likely to be UTF-8 content
//
@bg5sbk
bg5sbk / utf8_util.erl
Created December 8, 2013 08:39
Get UTF-8 character by Erlang.
-module(utf8_util).
-export([get_utf8_char/1]).
get_utf8_char ([]) ->
{0, []};
get_utf8_char ([Char1 | String]) ->
get_utf8_char(Char1, String).
get_utf8_char (Char1, String) when Char1 < 16#80 ->
{Char1, String};
@bg5sbk
bg5sbk / file_cache.php
Created December 8, 2013 08:29
A file system based cache class.
<?php
class file_cache
{
private $root_dir;
public function __construct($root_dir) {
$this->root_dir = $root_dir;
if (FALSE == file_exists($this->root_dir)) {
mkdir($this->root_dir, 0700, true);
@bg5sbk
bg5sbk / html_util.js
Last active December 30, 2015 16:28
HTML encode and decode in JavaScript.
function html_encode(str) {
var s = "";
if (str.length == 0) return "";
s = str.replace(/&/g, "&amp;");
s = s.replace(/</g, "&lt;");
s = s.replace(/>/g, "&gt;");
s = s.replace(/\'/g, "&#39;");
s = s.replace(/\"/g, "&quot;");
return s;
}
@bg5sbk
bg5sbk / cookie.js
Last active December 30, 2015 16:28
Cookie operations in JavaScript.
@bg5sbk
bg5sbk / simple_ajax.js
Last active December 30, 2015 16:28
A simple AJAX class.
SimpleAjax = function(url, method, content){
this.r = null;
this.url = url;
this.method = method;
this.content = content;
this.header = {};
this.header["Connection"] = "close";
this.header["Content-type"] = "application/x-www-form-urlencoded";
var self = this;
@bg5sbk
bg5sbk / querystring.js
Last active December 30, 2015 16:28
A query string parser for JavaScript.
QueryString = function(qs){
this.p={};
if(!qs)
qs=location.search;
if(qs) {
var b = qs.indexOf('?');
var e = qs.indexOf('#');
@bg5sbk
bg5sbk / svn-backup.sh
Last active December 30, 2015 16:19
A Subversion full or differential backup script.
#!/bin/bash
if [ $# != 3 ]; then
echo "This script backup all of SVN repositories under the [svn root] directory, to the [backup root] directory."
echo "usage: svn-backup ['full'|'part'] [svn root] [backup root]"
echo "example: svn-backup full /opt/svn-repos /opt/svn-backup"
exit
fi
mode=$1