Skip to content

Instantly share code, notes, and snippets.

@khoatran
khoatran / TextUtil.php
Last active April 17, 2024 10:21
Convert Vietnamese string to slug
<?php
class TextUtil {
public static function sanitize($title) {
$replacement = '-';
$map = array();
$quotedReplacement = preg_quote($replacement, '/');
$default = array(
'/à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ|À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ|å/' => 'a',
'/è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ|È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ|ë/' => 'e',
'/ì|í|ị|ỉ|ĩ|Ì|Í|Ị|Ỉ|Ĩ|î/' => 'i',
@khoatran
khoatran / parse-json.sh
Created April 3, 2019 07:07
Bash parse JSON key
#!/bin/bash
function jsonValue() {
KEY=$1
num=$2
awk -F"[,:}]" '{for(i=1;i<=NF;i++){if($i~/'$KEY'\042/){print $(i+1)}}}' | tr -d '"' | sed -n ${num}p
}
KEY_TO_PARSE=$(curl -s -X GET "JSON_URL" | jsonValue KEY_TO_PARSE 1)
echo $KEY_TO_PARSE
@khoatran
khoatran / scaling_php_application.conf
Last active December 28, 2021 13:20
Configuring Nginx to be a Cache for PHP-FPM
# This is the sample from Scaling PHP applications book
#Set the path where the cache is stored; Set the zone name(my_app), totalsize (100m),and max life time(60m)
fastcgi_cache_path /tmp/cachelevels=1:2keys_zone=my_app:100minactive=60m;
#Set the cache key used,in this case: httpsGETtest.com/somepage.html
fastcgi_cache_key "$scheme$request_method$host$request_uri";
server{
listen 80;
@khoatran
khoatran / Plugin.php
Last active December 4, 2020 16:27
Customize OctoberCMS sidebar navigation for your plugin
<?php namespace Author\PluginName;
use System\Classes\PluginBase;
use Backend\Facades\BackendMenu;
class Plugin extends PluginBase
{
public function register() {
BackendMenu::registerContextSidenavPartial('Author.PluginName',
'sidebar-menu',
@khoatran
khoatran / CachableComponent.php
Last active June 24, 2020 09:12
OctoberCMS - CachableComponent
<?php
use Cache;
use Cms\Classes\ComponentBase;
abstract class CachableComponentBase extends ComponentBase
{
/**
* Build cache configuration which is an array: ['cachedKey' => 'The cache key value', 'cachedTimeInMinutes' => 'Cached time in minutes']
* @return mixed
import * as AWSXRay from "aws-xray-sdk";
import { logger } from "./logger";
export function trace(name: string) {
return (target: any, key: string, descriptor: PropertyDescriptor) => {
const originalMethod = descriptor.value;
descriptor.value = async function() {
let segment: AWSXRay.Subsegment | null = null;
try {
try {
@khoatran
khoatran / PhoneNumber.java
Last active May 13, 2019 03:42
Liskov - violation - hashCode example
/*
* From Effective Java book - 2nd Edition - Author: Joshua Bloch
*/
public final class PhoneNumber {
private final short areaCode;
private final short prefix;
private final short lineNumber;
public PhoneNumber(int areaCode, int prefix,
int lineNumber) {
rangeCheck(areaCode, 999, "area code");
@khoatran
khoatran / git-log-with-time.sh
Created March 5, 2019 08:23
Git log one line with time
git log --oneline --pretty=format:"%h%x09%an%x09%ad%x09%s"
@khoatran
khoatran / Plugin.php
Last active January 24, 2019 15:34
OctoberCMS add fields under secondary tabs
<?php
class Plugin extends PluginBase
{
public function boot()
{
parent::boot();
$this->extendRainlabPost();
}
protected function extendRainlabPost() {
@khoatran
khoatran / remember-passphrase.sh
Created June 2, 2017 06:00
Remember passphrase of your ssh key
ssh-add ~/.ssh/id_rsa &>/dev/null