Skip to content

Instantly share code, notes, and snippets.

@phamquocbuu
Last active December 22, 2015 21:19
Show Gist options
  • Save phamquocbuu/6532401 to your computer and use it in GitHub Desktop.
Save phamquocbuu/6532401 to your computer and use it in GitHub Desktop.
Text manipulation
<?php
/**
* Xử lý chuỗi
* @author phamquocbuu
* @version 1.0
*/
class TextMan {
/**
* Chuyển sang tiếng Việt không dấu
* @author phamquocbuu
* @version 2013-09-12 08:49
* @param string @str - chuỗi cần chuyển sang không dấu
* @return string - chuỗi tiếng Việt không dấu
*/
public static function khongdau($str) {
$str = preg_replace("/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/", "a", $str);
$str = preg_replace("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/", "e", $str);
$str = preg_replace("/(ì|í|ị|ỉ|ĩ)/", "i", $str);
$str = preg_replace("/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/", "o", $str);
$str = preg_replace("/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/", "u", $str);
$str = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ)/", "y", $str);
$str = preg_replace("/(đ)/", "d", $str);
$str = preg_replace("/(À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ)/", "A", $str);
$str = preg_replace("/(È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ)/", "E", $str);
$str = preg_replace("/(Ì|Í|Ị|Ỉ|Ĩ)/", "I", $str);
$str = preg_replace("/(Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ)/", "O", $str);
$str = preg_replace("/(Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ)/", "U", $str);
$str = preg_replace("/(Ỳ|Ý|Ỵ|Ỷ|Ỹ)/", "Y", $str);
$str = preg_replace("/(Đ)/", "D", $str);
//$str = str_replace(" ", "-", str_replace("&*#39;","",$str));
return $str;
}
/**
* Tạo chuỗi ngẫu nhiên
* @author phamquocbuu
* @version 2013-09-12 09:20
* @param int $length - độ dài chuỗi ngẫu nhiên muốn tạo - mặc định = 10
* @return string - chuỗi ngẫu nhiên
*/
public static function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment