字符函數
strlen 獲取字符長度
$str = 'Hello';
echo strlen($str); //5
strtolower和strtoupper 大小寫轉換
$str = 'Hello';
echo strtoupper($str); //HELLO
ucfirst函數將句子首字母大寫
ucwords函數將每個單詞首字母大寫
$str = 'hello world';
echo ucfirst($str); //Hello world
echo ucwords($str); //Hello World
字符串替換函數
str_replace實現字符替換,區(qū)分大小寫
str_ireplace不區(qū)分大小寫
$str = 'javascript';
str_replace('a','b',$str); //將$str中的a替換成b
htmlspecialchars 將> < 等HTML標記轉為實體
$str = "A>B, <C";
echo htmlspecialchars($str); //A>B, <C
刪除空格 trim ltrim rtrim
$str = ' Hello world ';
echo trim($str); //Hello world
strpos返回字符串位置
$str1 = 'Hello world';
$str2 = 'e';
echo strpos($str1, $str2); //1
stripos返回一個字符在另一個字符中第一次出現的位置,忽略大小寫
strrpos返回一個字符串在另一個字符串最后一次出現的位置
strripos返回一個字符在另一個字符串中最后一次出現的位置
substr 字符串截取函數
string substr(string $string,int $start, [,int $length ])
strrev 反轉字符串
$str = 'hello';
echo strrev($str); //olleh
md5 字符串加密函數
實現計算字符串的md5哈希值,長度32位。通常用于密碼加密。
str_shuffle函數 隨機打亂字符串
可用于驗證碼的產生
$str = 'abcdefghijklmnopqrstuvwxyz';
$str = str_shuffle($str); // 每次產生的值都不一樣
echo substr($str, 0, 4);
explode函數 使用一個字符串分割另一個字符串
得到一個數組
$str = 'A|B|C|D';
$arr = explode('|', $str); //['A','B','C','D']
implode 數組的值轉化為字符串
將一個一維數組轉為字符串
$arr = ['A','B','C','D'];
$str = implode(';',$arr);
格式化字符串 sprintf
string sprintf(string $format [,mixed $args [,mixed $...]])
數學函數
取整
floor和ceil函數
冪函數pow
pow(2,3); //8
平方根sqrt
sqrt(9); //3
最大值最小值
max和min
隨機數
rand(int $min,int $max)
mt_rand(int $min, int $max)
四舍五入函數
round(float $val)
number_format
以千分位分隔符方式格式化數字
number_format(float $number)
fmod 浮點數余數
將返回出發(fā)的浮點數余數
fmod(float $x, float $y)
日期時間函數
date 格式化日期
格式化一個本地時間/日期
date(string format[,int timestamp])
date_default_timezone_set函數
設置默認時區(qū)
date_default_timezone_set("Asia/Shanghai");
date_default_timezone_get函數獲取當前時區(qū)
time
返回當前Unix時間戳
strtotime 將字符串轉為時間戳
strtotime("-3 weeks"); //3周前的時間戳
microtime 返回當前Unix時間戳和微秒數
uniqid 生成唯一ID
uniqid();
md5(uniqid(microtime() . mt_rand())); //唯一值
getdate獲取日期時間信息
array getdate([int timestamp])