Skip to content

Instantly share code, notes, and snippets.

@minhphong306
Created February 6, 2018 14:50
Show Gist options
  • Save minhphong306/17abf9fd8745c9a47286e3e667b6eca6 to your computer and use it in GitHub Desktop.
Save minhphong306/17abf9fd8745c9a47286e3e667b6eca6 to your computer and use it in GitHub Desktop.
PHP Useful function
// Remove special character, only Allow alphabets and numbers
function vn_to_str($str) {
$unicode = array(
'a' => 'á|à|ả|ã|ạ|ă|ắ|ặ|ằ|ẳ|ẵ|â|ấ|ầ|ẩ|ẫ|ậ',
'd' => 'đ',
'e' => 'é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ',
'i' => 'í|ì|ỉ|ĩ|ị',
'o' => 'ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ',
'u' => 'ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự',
'y' => 'ý|ỳ|ỷ|ỹ|ỵ',
'A' => 'Á|À|Ả|Ã|Ạ|Ă|Ắ|Ặ|Ằ|Ẳ|Ẵ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ',
'D' => 'Đ',
'E' => 'É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ',
'I' => 'Í|Ì|Ỉ|Ĩ|Ị',
'O' => 'Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ',
'U' => 'Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự',
'Y' => 'Ý|Ỳ|Ỷ|Ỹ|Ỵ'
);
foreach ($unicode as $nonUnicode => $uni) {
$str = preg_replace("/($uni)/i", $nonUnicode, $str);
}
$str = str_replace(' ', '_', $str);
$str = preg_replace("/[^A-Za-z0-9 ]/", '', $str);
return $str;
}
// Check variable isset
function isset_request($request_param){
if(isset($_REQUEST[$request_param])){
return $_REQUEST[$request_param];
}
return '';
}
function isset_object($object, $field){
if(isset($object[$field])){
return $object[$field];
}
return '';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment