<?php function array_sort($array, $on, $order=SORT_ASC) { $new_array = array(); $sortable_array = array(); if (count($array) > 0) { foreach ($array as $k => $v) { if (is_array($v)) { foreach ($v as $k2 => $v2) { if ($k2 == $on) { $sortable_array[$k] = $v2; } } } else { $sortable_array[$k] = $v; } } switch ($order) { case SORT_ASC: asort($sortable_array); break; case SORT_DESC: arsort($sortable_array); break; } foreach ($sortable_array as $k => $v) { $new_array[$k] = $array[$k]; } [...]
PHP
function copydir($source,$destination){ if(!file_exists($destination)){ mkdir($destination, 01777); // so you get the sticky bit set } $dir_handle = @opendir($source) or die(“Unable to open”); while ($file = readdir($dir_handle)){ if($file!=”.” && $file!=”..” && $file!=”.svn”){ if(is_dir(“$source/$file”)){ copydir(“$source/$file”,”$destination/$file”); }else{ copy(“$source/$file”,”$destination/$file”); } } } closedir($dir_handle); }
ใช้ตัดข้อความจากฐานข้อมูลที่ต้องการแสดงบางส่วนเช่น 30 ตัวอักษร กรณีบางครั้งภาษาไทยที่ตัดจากฐานข้อมูล จะแสดงเป็นรูปสี่เหลี่ยม ใช้ฟังก์ชันนี่แทน substr ใน php แก้ปัญหาที่เกิดได้ แบบที่ 1 <?php function substr_utf8( $str, $start_p , $len_p){ return preg_replace( '#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$start_p.'}'. '((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len_p.'}).*#s', '$1' , $str ); }; // การใช้งาน // $start_p คือตำแหน่งเริ่มต้นตัดข้อความ // $len_p คือจำนวนตัวอักษรที่ต้องการแสดง // $data="ข้อความทดสอบ ข้อความทดสอบ ข้อความทดสอบ ข้อความทดสอบข้อความทดสอบ "; // echo substr_utf8($data,0,30); แบบที่ 2 <?php function utf8_substr($str,$start_p,$len_p) { preg_match_all("/./u", $str, $ar); if(func_num_args() >= 3) { $end = func_get_arg(2); return join("",array_slice($ar[0],$start_p,$len_p)); } else { return join("",array_slice($ar[0],$start_p)); } } // การใช้งาน // $start_p คือตำแหน่งเริ่มต้นตัดข้อความ [...]
<?php function remove_dir($dir) { if(is_dir($dir)) { $dir = (substr($dir, -1) != “/”)? $dir.”/”:$dir; $openDir = opendir($dir); while($file = readdir($openDir)) { if(!in_array($file, array(“.”, “..”))) { if(!is_dir($dir.$file)) { unlink($dir.$file); } else { remove_dir($dir.$file); } } } closedir($openDir); rmdir($dir); } } ?> ที่มา http://php.deeserver.net/phpbb/viewtopic.php?p=2529&sid=96e5445f9ddd72919ae74a289515e9b4#p2529
