一、字串函數
1.格式化字串,使用printf()
輸出到螢幕:printf()
輸出到變數:sprintf()
提示:兩個函數使用方法一樣。
1.1 資料類型轉換
printf("This is my number:%o",55);//This is my number:67
轉換類型以%開頭,後面緊跟字母代表的類型。
注意:在格式控制項字串中可以有多個%,但第二個參數的數目個數必須與%的個數一致。
類型指定符
| 指定符 |
說明 |
| d |
以十進位數顯示參數 |
| b |
以二進位顯示一個整數 |
| c |
以對等ASCII顯示一個整數 |
| f |
以浮點數顯示一個整數 |
| o |
以8進位顯示一個整數 |
| s |
以字串顯示參數 |
| x |
以一個小寫十六進位顯示一個整數 |
| X |
以一個大寫十六進位數顯示一個整數 |
1.2 填充字元(指定長度,長度不足用特定輸入鍵台)
printf("%04d",36);//輸出少於4位,將在前面添加0//prints 0036printf("% 4d",36);//輸出少於4位,將在前面加空格//prints 36printf("%'x4d",36);//除0和空格外,其它填充字元前面必須加單引號//prints "xx36"//提示:瀏覽器不會顯示多個空格,可以在輸出的外圍添加<pre>標記來強制顯示空格和新行。echo "<pre>The spaces will be visible</pre>";
1.3 指定字串輸出長度
echo "<pre>\n";printf("%20s\n","Books"); //預設靠右對齊,不足20位,前置補空格。printf("%20s\n","CDs");//靠左對齊:在長度前加減號靠左對齊printf("%-20s\n","Left aligned");echo "</pre>";1.4 指定精度(四捨五入)
printf("%.2f",5.3333);//prints "5.33"echo "\n";printf("%.2f",5.3353);//prints "5.34"1.5 指定參數顯示順序
$dates=array(array('mon'=>12,'mday'=>25,'year'=>2011), array('mon'=>1,'mday'=>23,'year'=>2012));$format=include("local_format.php");//如果要改變顯示格式,只需改變此檔案foreach ($dates as $date){ printf("$format",$date['mon'],$date['mday'],$date['year']);}
//local_format.php 檔案內容
//return "%02d/%02d/%d<br/>";//mm/dd/yyyyreturn "%2\$02d/%1\$02d/%3\$d<br/>";//dd/mm/yyyy//2\$:第二個參數預留位置
1.6 將格式化的結果儲存到變數
保留2位小數,並將結果儲存到$cash變數中
$cash=sprintf("%.2f",21.334454);echo "You have \$$cash to spend.";// prints "You have $21.33 to spend."
2.1 字串就是字元的一個數組,我們可以象訪問數組的元素一樣訪問單個字元。
$test='phpcoder';echo $test[0];//prints "p"echo $test[4];//prints "o"
2.2 strlen()擷取字串的長度。一個漢字佔3個長度
2.3 strstr(源字串,尋找的子字串),擷取從子符串開始到結尾的全部字串,找不到則返回false
//比較時區分大寫小echo strstr("pAB7","AB");//prints "AB7"//不區分大小寫echo stristr("pAB7","ab");//prints "AB7"
//是否包含子字串$membership="pAB7";if(strstr($membership,"AB")){ echo "<p>Your membership expires soon!</p>";}
2.4 strpos(源字串,尋找的子字串) ,擷取子字串的索引
//擷取子字串的索引,找不到則返回falseecho strpos($membership,"xy");//prints 4
2.5 substr(字串,開始索引,長度) 截取子字串
$test="phpcoder";echo substr($test,3)."<br/>";//prints "coder"echo substr($test,3,2)."<br/>";//prints "co"//注意:如果第二個參數為負數,則索引從末尾開始計算,即從末尾開始取字元$test="pierre@wanadoo.fr";if($test=substr($test,-3)==".fr"){ echo ".fr";}
2.5 strtok(源字串,分隔字串),拆分字串
$test="http://www.google.com/search?";$test.="hl=en&ie=UTF-8&q=php+development+books&btnG=Google+Search";$delims="?&";//分隔字元可包含多個任一字元$word=strtok($test,$delims);//第一次調用,並返回找到的第一個分解while (is_string($word)){//為什麼測試傳回型別。因為有多個分隔字元,//如果字串中兩個分隔字元在一起時,當遇到第一個分隔字元時,會導致返回一個Null 字元串。 if($word){ echo $word."<br/>"; } $word=strtok($delims);//第二次調用只需傳分隔字元字串}2.6 trim(),ltrim(),rtrim(),刪除字串空格
2.7 strip_tags(源字串,需要保留的標記),刪除字串中的標記
$string="<p>\"I <em>simple</em> will not have it,\" <br/>said Mr Dean.</p><p><strong>The end.</strong></p>";echo strip_tags($string,"<br/><p>");//僅保留"<br/><p>"標記,其它標記全部刪除echo strip_tags($string);//刪除全部HTML標記。
2.8 substr_replace(源字串,插入的新字串,刪除開始索引,刪除的字元長度) ,用新的字元替換源指定位置的字元
$membership="mz11xyz";echo substr_replace($membership,"89",2,2);//prints "mz89xyz" 替換2個字元echo "\n";echo substr_replace($membership,"89",2,1);//prints "mz891xyz" 只替換源字串中一個字元echo "\n";echo substr_replace($membership,"89",2);//prints "mz89" 不帶長度參數,為刪除位元置後的全部字元
2.9 str_replace($search,$replace,$source) 替換字串
注意:參數可以是數組或字串。
3.0 大小寫轉換
strtolower() 全部小寫
strtoupper() 全部大寫
ucwords() 每個單字首大寫
ucfirst() 第一個字串的首字母大寫
3.1 字串換行
//將字串中分行符號\n轉換成<br/>標記$str="one line\n";$str="another line\n";echo nl2br($str);//手動將文本換行$string="As usual you will find me at http://www.witteringonaboutit.com/";$string.="chat/eating_green_cheese/forum.php. Hope to see you there!";echo wordwrap($string,24,"<br/>\n");echo wordwrap($string,24,"<br/>\n",1);//強制按24個字元換行//注意:必須帶第三個參數,否則在瀏覽器中不會換行。
3.2 explode()將字串拆分到數組中
$start_date="2012-02-19";$date_arrary=explode("-",$start_date);echo($date_arrary[0]);//prints "2012"
二、日期和時間函數
2.1 time()得到當前日期的時間戳記
2.2 getdate(時間戳記) 返回日期資訊的關聯陣列。不帶參數為目前時間戳的日期。
注意:設定預設時區,如果不設定,則得到的時間為錯誤。
修改php.ini檔案
date.timezone=PRC
修改php.ini後重啟Apache
或者在代碼獲時間前增加代碼:
date_default_timezone_set(‘PRC’);
2.3 date(格式字串,時間戳記) 格式化時間戳記
2.4 mktime(時,分,秒,月,日,年) 建立時間戳記
2.5 checkdate() 檢測日期。UNIX時間戳記是從1970年1月1日開始,此前的日期都是無效的(負的)時間戳記