function strtotime_thai($str){
$str = preg_replace_callback('/\d{4}/',function($match){
return $match[0]-543;
},$str);
setlocale(LC_TIME, 'thai');
$format = '%d %B %Y %H:%M:%S';//d M Y H:i:s
$str = iconv('UTF-8','TIS-620',$str);//comment this line if $str is tis620
$result = strptime($str,$format);
if (empty($result)){
return false;
}
extract($result);
return mktime($tm_hour,$tm_min,$tm_sec,$tm_mon+1,$tm_mday,$tm_year+1900);
}
$phptime = strtotime_thai('19 ธันวาคม 2557 23:30:59');
echo date('d/m/Y H/i/s',$phptime);
แสดงบทความที่มีป้ายกำกับ php function แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ php function แสดงบทความทั้งหมด
วันเสาร์ที่ 8 มีนาคม พ.ศ. 2557
แปลงวันที่ไทยให้เป็นวันที่ของ php
แปลงวันที่ภาษาไทยให้เป็นวันที่ php ครับ
แต่ function นี้มีข้อจำกัดคือไม่สามารถใช้กับ host windows ได้ครับเพราะเรียกใช้ strptime ซึ่งไม่มีใน host windows
วันพุธที่ 30 ตุลาคม พ.ศ. 2556
fairly_rand function
faily_rand function เป็น random function รูปแบบหนึ่ง ที่จะสุ่มตัวเลขขึ้นมาแบบไม่ซ้ำครับ
function fairly_rand($start,$end,$return=1){
//function นี้ $start และ $end ควรจะห่างกันมากๆ
$a = array();
$distance = ($end-$start)/$return;
$a[]=$start;
for($i=$start+$distance;$i<$end-($distance/2);$i+=$distance){
$a[]=($a[]=floor($i))+1;
}
$a[]=$end;
$results = array();
for($i=0;$i<$return*2;$i+=2){
$results[] = rand($a[$i],$a[$i+1]);
}
return $results;
}
$numbers = fairly_rand(1,500,5);
var_dump($numbers);
วันเสาร์ที่ 26 ตุลาคม พ.ศ. 2556
การเรียงลำดับภาษาไทยใน php / sorting thai characters
ในการเรียงลำดับโดย php ถ้าใช้ฟังค์ชั่น sort ธรรมดา จะเรียงทีละ byte หรือเรียงโดยให้ความสำคัญกับสระเท่าพยัญชนะจะทำให้เกิดการเรียงลำดับภาษาไทยที่ผิดพลาดดังนนี้ครับ
การบ้าน,ข้าว,เคย,โค,ไก่,ไข่
(ทำให้คำที่ขึ้นต้นด้วยสระเดียวกันเช่น ไ (สระไอไม้มาลัย) อยู่ติดกัน)
ใน php เราจะใช้การตั้งค่า locale และใช้ usort (User sort) ช่วยให้จัดเรียงลำดับภาษาไทยได้ถูกต้อง ดังนี้ครับ..
function sortthaitis620(&$a){
setlocale(LC_COLLATE, 'thai');
usort ($a, 'strcoll');
}
function sortthai(&$a){//สำหรับ utf8
foreach($a as &$i){
$i = iconv('UTF-8','TIS-620',$i);
}
setlocale(LC_COLLATE, 'thai');
usort ($a, 'strcoll');
foreach($a as &$i){
$i = iconv('TIS-620','UTF-8',$i);
}
}
$a = array('โค','เคย','ไก่','การบ้าน','ข้าว','ไข่');
sortthai($a);
echo implode(',',$a);
sortthaitis620 เป็น function สำหรับ เรียงลำดับข้อความภาษาไทยในตัวอักษรที่เข้ารหัสแบบ tis620 หรือ windows874
sortthai เป็น function สำหรับ เรียงลำดับข้อความภาษาไทยในตัวอักษรที่เข้ารหัสแบบ utf8 ครับ
แต่ถ้าต้องการเรียงลำดับข้อมูลในตารางของฐานข้อมูล mysql ที่ encoding เป็น utf8 ให้ select ดังนี้ครับ
SELECT * FROM tb ORDER BY CONVERT (column_name USING tis620);
วันพฤหัสบดีที่ 5 พฤษภาคม พ.ศ. 2554
php sorting head builder class by num
<?php
/**
* Sorting Head Builder For Dynamic Table
*
* @author Num
* @link http://web-programming-bookmark.blogspot.com
*/
class SortHead
{
protected $ccol = '';
protected $cdir = '';
protected $ncol = '';
protected $ndir = '';
protected $dfcol = '';
protected $dfdir = '';
protected $cols = array();
public $col = '';
public $dir = '';
public function __construct(array $cols,$cname,$dname,$dfcol,$dfdir){
$this->cols = $cols;
$this->dfcol = $dfcol;
$this->dfdir = $dfdir;
$this->ncol = $cname;
$this->ndir = $dname;
$this->ccol = $this->current_col();
$this->cdir = $this->current_dir($this->ccol,$dfdir);
$this->col = $this->ccol;
$this->dir = $this->cdir;
}
public function current_col(){
$ccol = empty($_GET[$this->ncol]) ? $this->dfcol : $_GET[$this->ncol];
$k = array_search($ccol,$this->cols,true);
return ($k === false) ?$this->dfcol :$this->cols[$k];
}
public function current_dir($col,$dfdir='desc'){
$odir = $dfdir == 'desc' ?'asc' :'desc';
if ($this->ccol == $col){
$dir = empty($_GET[$this->ndir]) ?$dfdir :$_GET[$this->ndir];
if ($dir != 'asc' && $dir != 'desc') $dir = 'desc';
$dir = $dir == 'desc' ?'desc' :'asc';
}else{
$dir = $dfdir;
}
return $dir;
}
public function next_dir($col,$dfdir='desc'){
$odir = $dfdir == 'desc' ?'asc' :'desc';
if ($this->ccol == $col){
$dir = empty($_GET[$this->ndir]) ?$dfdir :$_GET[$this->ndir];
if ($dir != 'asc' && $dir != 'desc') $dir = 'desc';
$dir = $dir == 'desc' ?'asc' :'desc';
}else{
$dir = $dfdir;
}
return $dir;
}
public function link($col,$dfdir='desc'){
$dir = $this->next_dir($col,$dfdir);
return '?'.$this->ncol.'='.$col.'&'.$this->ndir.'='.$dir;
}
public function dir($col,$dfdir='desc'){
return $this->current_dir($col,$dfdir);
}
}
$sh = new SortHead(array('name','title','id'),'st','sd','id','desc');
//safe for query
echo ', field=',$sh->col;
echo ', dir=',$sh->dir;
echo ', select * from tb order by '.$sh->col.' '.$sh->dir;
?>
<table border="1" cellpadding="10" cellspacing="0">
<tr>
<td>
<a href="<?php echo $sh->link('name','asc') ?>">name <?php echo $sh->dir('name','asc') ?></a>
</td>
<td>
<a href="<?php echo $sh->link('title') ?>">title <?php echo $sh->dir('title') ?></a>
</td>
<td>
<a href="<?php echo $sh->link('id') ?>">id <?php echo $sh->dir('id') ?></a>
</td>
</tr>
</table>
วันพฤหัสบดีที่ 16 ธันวาคม พ.ศ. 2553
using php array
<?php
function block_split($s){
$pos = strpos($s,'[');
$has_b = is_int($pos);
if ($has_b){
$a = array();
$test = preg_match_all('/(\[[^]]*\])([^[]*)/s',$s,$ms,PREG_SET_ORDER);
foreach($ms as $m){
$a[] = $m[1];
$a[] = $m[2];
}
array_unshift($a, substr($s,0,$pos));
} else {
$a = array($s);
}
$a = array_map('trim',$a);
$a = array_diff($a,array(''));
$a = array_values($a);
return $a;
}
$s = 'xxx [metal] [plating] pine apple [test] test';
$a = block_split($s);
echo '<pre>',var_export($a,true),'</pre>';
?>
วันพฤหัสบดีที่ 9 ธันวาคม พ.ศ. 2553
remove some value from array
<?php
$a = array(11,5,0,2,0,1);
$a = array_diff($a,array(0)); //ลบสมาชิกที่มีค่า 0 ออก .... ใช้คำสั่งอันนี้ หรือ
//$a = array_filter($a); //ลบสมาชิกที่มีค่า 0 ออก .... ใช้คำสั่งอันนี้ ก็ได้
sort($a,SORT_NUMERIC); //เรียงลำดับ
echo '<pre>';print_r($a);echo '</pre>';
?>
วันพุธที่ 10 พฤศจิกายน พ.ศ. 2553
inner new php function
function หา string ที่อยู่ระหว่างคำสองคำที่ระบุ
<?php
function inner($s,$f,$e){
$f = (string)$f; $e = (string)$e;
if (strlen($f)==0 || strlen($e)==0 || ($start = strpos($s, $f))===false) return "";
$s = substr($s, $start+strlen($f));
return substr($s, 0, strrpos($s, $e));
}
function innerutf8($s,$f,$e){ //safe สำหรับ utf8 มากกว่า
foreach(array('s','f','e') as $i) $$i = iconv('UTF-8','TIS-620',$s);
return iconv('TIS-620','UTF-8',inner($s,$f,$e));
}
$s = '[abc][abc] test0 [defg] test1 [/defg] test2 [/abc][/abc]';
echo '<hr/>'; var_dump(inner($s,'[defg]','[/defg]'));
echo '<hr/>'; var_dump(inner($s,'[abc]','[/abc]'));
echo '<hr/>'; var_dump(inner($s,'0','2'));
echo '<hr/>'; var_dump(inner($s,0,2));
echo '<hr/>'; var_dump(inner($s,'[abc]','[/abcd]')); //not found
echo '<hr/>'; var_dump(inner($s,'[abcd]','[/abc]')); //not found
echo '<hr/>'; var_dump(inner($s,'[abcd]','[/abcd]')); //not found
echo '<hr/>'; var_dump(inner($s,'','')); //not found
?>
สมัครสมาชิก:
บทความ (Atom)