几个常用的PHP方法或语句
PHP匹配 UTF-8 中文的正则
preg_match('@[\x{4e00}-\x{9fa5}]+@u', $string, $tmp);
删除非空文件夹
function remove_directory($dir) {
if ($handle = opendir("$dir")) {
while (false !== ($item = readdir($handle))) {
if ($item != "." && $item != "..") {
if (is_dir("$dir/$item")) {
remove_directory("$dir/$item");
} else {
unlink("$dir/$item");
echo " removing $dir/$item<br>\n";
}
}
}
closedir($handle);
rmdir($dir);
echo "removing $dir<br>\n";
}
}
PHP 获取文件扩展名正确的方法
echo pathinfo('/www/htdocs/your_image.jpg', PATHINFO_EXTENSION);
具体看下:
<?php
$file_path = pathinfo('/htdocs/roy_image.jpg');
echo "$file_path ['dirname']\n";
echo "$file_path ['basename']\n";
echo "$file_path ['extension']\n";
echo "$file_path ['filename']\n"; // only in PHP 5.2+
?>
一次性获取字符串中的需要的子串
$str = 'foo,bar,zip';
list(,,$var) = explode(',', $str);
echo $var;
使用sscanf函数
$mandate = "January 01 2000";
list($month, $day, $year) = sscanf($mandate, "%s %d %d");
php匹配中文
$str = '中华人民共和国';
preg_match('/^[\x{4e00}-\x{9fa5}]+$/u',$str);
全角转半角
$str = preg_replace('/\xa3([\xa1-\xfe])/e', 'chr(ord(\1)-0x80)', $str);
echo $str;