星期四, 10月 14, 2010

Zend PHP 5 Certification Study Guide 筆記 (二)


Chapter 4 Strings And Patterns


String Basics
單引號用在"Simple strings" //almost all characters are used literally
雙引號用在"Complex strings" //alow for special escape sequences
Variable Interpolation
//單、雙引號差別
$who = "World";
echo "Hello $who\n"; // Shows "Hello World" for followd by new line
echo 'Hello $who\n'; // shows "Hello $who\n"

//無空格連接
$me = 'Davey';
echo "There cannot be more than tw {$me}s!"; //s緊連接在$me後

//array用法 - 利用braces
$names = array('Smith', 'Jones', 'Jackson');
echo "Citation: {$names[1]}";


The Heredoc Syntax
利用<<<關鍵字 可以接受一段string
$who = "World";
echo $lt;$lt;$lt;TEXT
So I said, "Hello $who"
TEXT;
//印出So I said, "Hello World,在opening token及closing token的斷行符號皆忽略

*例外 在物件中屬性無法利用Heredoc方法給值

Escaping Literal Values
Determining the Length of a String
echo strlen("te"); //2
echo strlen("dd"); //3
echo strlen("我的"); //3


Simple Searching Functionality
//找字串的起始位置
$haystack = "abcdefg";
$needle = 'abc';
if(strpos($haystack, $needle) !== false){
echo 'Found';
}


因為array從0開始,所以當在第一格即match時,會回傳0,但0會當false,所以利用!==

strstr()與strpos的差異
functionpurpose指定搜尋的index
回傳找到符合字串後的值不能
回傳符合字串的起始位置
strstr()比較慢,所以如果只是找位置的話,用strpos()比較恰當
以上兩者皆case-insensitive,要分大小寫要改用stripos(),stristr()

Matching Against a Mask
strspan($string1,$string2[,index][,length])
看好久還是看不太懂
似乎是「從index開始,利用遮罩A字串去遮B字串中的字元,至到不符合即中止」
echo strspn('133445abcdef','12345') //輸出6(13345),a開始即無法遮罩
echo strspn("PHP wzd","PHPwzd"); // 輸出3(PHP),空格處遮罩
echo strspn("faooaaol","ao",1,4); // 輸出4(aooa),跳過f開始找4個
echo strspn("foo","o",1,2); // 輸出2(oo)

Simple Search and Replace Operations
  • 取代 substr_replace(mixed $string, string $replacement )
    echo str_replace("World", "Reader", "Hello World"); //大小寫沒差,所以outputs Hello Reader
    echo str_replace("world", "Reader", "Hello World"); //大小寫有差,所以outputs Hello World

  • 一次取代多個
    echo str_replace(array("Hello", "World"),"Bye", "Hello World"); //Bye Bye
    當取代及被取代皆為array,再依array indexes對應
    echo str_replace(array("Hello", "World"), array("Bonjour", "Monde"), "Hello World"); //Bonjour Monde
  • 取代部份 substr_replace(mixed $string, string $replacement , int $start [, int $length ])
    echo substr_replace("Hello World", "Reader", 6); //Hello World
    echo substr_replace("Canned tomatoes are good", "potatoes", 7 ,8); //Canned potatoes are good

substr_replace加強版(搭配strpos())
從email取得帳號
$user = "davey@php.net";
$name = substr_replace($user, "", strpos($user, '@');
echo "Hello " . $name;


Formatting Strings
主要是在print數量及金錢單位時,會因不同國家而有不同的顯示
所以在print前 最好都先確認及設定國別
setlocale(LC_MONETARY, 'en_US');

  • Formatting Numbers
    echo unmber_format("100000.698"); //Shows 100,001 (美國會在千用逗號)
    echo unmber_format("100000.698", 3, ",", " "); //Shows 100 000,698 (改成用空格)

  • Formatting Currency Values
    會印出該國的金幣符號,ex $100,¥100
    setlocale(LC_MONETARY, 'en_US');
    echo money_format('%.2n', "100000.698"); //$100,000.70
    setlocale(LC_MONETARY, "ja_JP.UTF-8");
    echo money_format('%.2n', "100000.698"); //¥100,000.70

    setlocale(LC_MONETARY, 'en_US');
    echo money_format('%i', "100000.698"); //USD 100,000.70 //雖沒設定小數位,但美元習慣有2位小數
    setlocale(LC_MONETARY, "ja_JP");
    echo money_format('%i', "100000.698"); //JPY 100,001 沒小數位,所以併成整數

    money_format()無法在Windows平台上用,某些UNIX也無法使用

沒有留言: