Skip to content

Latest commit

 

History

History
151 lines (140 loc) · 4.61 KB

code_php.md

File metadata and controls

151 lines (140 loc) · 4.61 KB

Table of Content

Code get string next value

ตัวอย่างโปรแกรมในการหาค่าถัดไปของข้อมูล

Code get next value number only

function get_value($value,$length_limit=0){
    $value = trim($value);
    if($value == ""){
        return "";
    }
    if(!preg_match("/[0-9]/",$value)){
        return "";
    }
    if($length_limit == 0) {
        $length_limit = strlen($value) + 1;
    }
    return substr(++$value,-$length_limit);
}

Code get next value a to z only

function get_value($value,$length_limit=0){
    $value = trim($value);
    if($value == ""){
        return "";
    }
    if(!preg_match("/[a-z]/",$value)){
        return "";
    }
    if($length_limit == 0) {
        $length_limit = strlen($value) + 1;
    }
    return substr(++$value,-$length_limit);
}

Code get next value A to Z uppercase only

function get_value($value,$length_limit=0){
    $value = trim($value);
    if($value == ""){
        return "";
    }
    if(!preg_match("/[A-Z]/",$value)){
        return "";
    }
    if($length_limit == 0) {
        $length_limit = strlen($value) + 1;
    }
    return substr(++$value,-$length_limit);
}

Code get next value thai language only

function get_value($value, $length_limit = 0)
{
    $value = trim($value);
    if ($value == "") {
        return "";
    }
    if (!preg_match("/[ก-ฮ]/", $value)) {
        return "";
    }
    if ($length_limit == 0) {
        $length_limit = mb_strlen($value, 'utf-8') + 1;
    }
    $array_thai = array(
        '', '', '', '', '', '', '', '', '', '',
        '', '', '', '', '', '', '', '', '', '',
        '', '', '', '', '', '', '', '', '', '',
        '', '', '', '', '', '', '', '', ''
    );

    $size_array_thai = count($array_thai);
    $size_value = mb_strlen($value, 'utf-8');

    $is_last_ch_in_array = false;
    $next_value = "";
    for ($index = 1; $index <= $size_value; $index++) {
        $value_last_str = mb_substr($value, -$index, 1, 'utf-8');
        $pos_last_str = array_search($value_last_str, $array_thai);

        if ($index == 1 || $is_last_ch_in_array) {
            $next_value = $array_thai[($pos_last_str + 1) % $size_array_thai] . $next_value;
            $is_last_ch_in_array = false;
        } else {
            $next_value = $value_last_str . $next_value;
        }
        if ($pos_last_str == $size_array_thai - 1) {
            $is_last_ch_in_array = true;
        }
    }

    if ($is_last_ch_in_array) {
        $next_value = $array_thai[0] . $next_value;
    }

    return mb_substr($next_value, -$length_limit, null, 'utf-8');
}

Code Ramdom String

ตัวอย่างโปรแกรมในการสุ่มค่าข้อมูลซึ่งทำได้ทั้งตัวเลข ตัวอักษรภาษาอังกฤษทั้งใหญ่ เล็ก และตัวอักษรไทย

Code generate ramdom string

function get_value($length = 64, $is_use_number = true, $is_use_lowercase = true, $is_use_uppercase = true, $is_use_thai_str = false)  
{  
  if ($length < 1) {  
  return "";  
  }  
  $number = "0123456789";  
  $lowercase = "abcdefghijklmnopqrstuvwxyz";  
  $uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";  
  $thai_str = "กขคงจฉชซณญฐฑฒณดตถทธนบปผฝพฟภมยรลวศษสหฬอฮ";  
  $characters = '';  
 if ($is_use_number) {  
  $characters .= $number;  
  }  
  if ($is_use_lowercase) {  
  $characters .= $lowercase;  
  }  
  if ($is_use_uppercase) {  
  $characters .= $uppercase;  
  }  
  if ($is_use_thai_str) {  
  $characters .= $thai_str;  
  }  
  
  $charactersLength = mb_strlen($characters, 'utf-8');  
  $randomString = '';  
 for ($i = 0; $i < $length; $i++) {  
  $randomString .= mb_substr($characters, rand(0, $charactersLength - 1), 1, 'utf-8');  
  }  
  return $randomString;  
}