11/08/2018, 20:53
Trim space fullsize và halfsize bằng Regex trong PHP
Hàm số trim space fullsize và halfsize trong PHP /** * Remove space fullsize * * @param string $str string before remove * @return string after removed */ public static function mb_trim($str, $chars = 's ') { $str = preg_replace("/^[$chars]+/u", ', $str); $str = preg_replace("/[$chars] ...
Hàm số trim space fullsize và halfsize trong PHP
/**
* Remove space fullsize
*
* @param string $str string before remove
* @return string after removed
*/
public static function mb_trim($str, $chars = 's ') {
$str = preg_replace("/^[$chars]+/u", ', $str);
$str = preg_replace("/[$chars]+$/u", ', $str);
return $str;
}
HOẶC hàm số sau
/**
* Trim space, tab
* @param string $value
* @return string
*/
public function trimSpace($value) {
mb_internal_encoding('UTF-8');
mb_regex_encoding('UTF-8');
$value = mb_ereg_replace("^[
s ]+", ', $value);
$value = mb_ereg_replace("[
s ]+$", ', $value);
$value = trim($value);
return $value;