- var_dump(0 == 'test');
- var_dump(0 == '');
- var_dump(5 > 't');
- var_dump(strcmp(5, 't'));
複製代碼結果為(第1~3結果是不對的,只有第4個是對的):4bool(true)bool(true)bool(true)int(-1)2. 字串處理1. 子串$sub = substr(string, start[, length]);2. 子串替換$newstring = substr_replace(string, new, start[, length]);用這個函數可以實現字串的插入,刪除操作。這個函數的start和length可以為負數。分別表示從後開始計算以及保留最後幾位不替換。 3. 字串反序$newstring = strrev(string);4. 重複字串$newstring = str_repeat(string, count);返回一個重複count次string的新字串。5. 填充字串$newstring = str_pad(to_pad, length[, with[, type]]);其中type有:str_pad_right(預設)、str_pad_left和str_pad_both三種;with預設為空白格。函數表示把to_pad字串用with填充為一個長度為length的字串。 如下代碼:
// 子串
- var_dump(substr('1234567890', 8)); // 90
- var_dump(substr('1234567890', 0, 2)); // 12
- // 反方向子串
- var_dump(substr('1234567890', -8)); // 34567890
- var_dump(substr('1234567890', -8, -2)); // 345678
- var_dump(substr('1234567890', -8, 2)); // 34
// 插入
- var_dump(substr_replace('1234567890', 'a', 0, 0)); // a1234567890
- // 刪除
- var_dump(substr_replace('1234567890', '', 8)); // 12345678
- // 反方向刪除
- var_dump(substr_replace('1234567890', '', -2, -1)); // 123456780
- // 替換
- var_dump(substr_replace('1234567890', 'a', 0, 1)); // a234567890
- // 反方向替換
- var_dump(substr_replace('1234567890', 'a', -2, -1)); // 12345678a0
// 字串反轉
- var_dump(strrev('1234567890')); // 0987654321
// 重複字串
- var_dump(str_repeat('12', 3)); // 121212
// 填充字串
- var_dump(str_pad('a', 10, '12')); // a121212121
- var_dump(str_pad('a', 10, '12', str_pad_left)); // 121212121a
- var_dump(str_pad('a', 10, '12', str_pad_both)); // 1212a12121
-
複製代碼3. 分解字串在php中,字串的分解用explode,合并用implode(join是implode的別名),標記用strtok。還有另一個函數slipt也可以分解(正則分解),但5.3以後版本已經不推介了。 另外php中還有一個sscanf()函數,用於讀取字串。strtok標記時,用strtok($str, $token)來初始化,用strtok($token)來繼續取值。代碼:
$str = '1,2,3';
- $arr1 = explode(',', $str); // array('1', '2', '3')
- $arr2 = explode(',', $str, 2); // array('1', '2,3')
$str1 = implode(',', $arr1); // '1,2,3'
$str2 = strtok($str, ','); // 1
- $str3 = strtok(','); // 2
- $str4 = strtok(','); // 3
// array(86, 10, 88888888, 'beijin')
- $arr3 = sscanf('+86(10)88888888 beijin', '+%d(%d)%d %s');
-
複製代碼4.字串尋找在php中,字串的尋找有三個系列。返回位置的、返回字串的、掩碼個數匹配。其中,返回位置的的函數一共有兩個,strpos()和strrpos();返回字串的也有兩個strstr()和strchr();返回掩碼匹配數的函數有strspn()和strcspn()。strpos表示從左邊開始計數,返回要尋找的字串第一次出現的位置;strrpos表示從右邊計數,返回要尋找的字串第一次出現的位置。strstr表示從左邊計數,返回要尋找字串第一次到結尾的子串(包括尋找字串),當尋找的是字元時,可以用ascii碼數字來表示字元;stristr表示不區分大小尋找;strchr是strstr的別名;strrchr返回字元最後出現到結尾的子串。strspn表示從左邊計數,第一次出現非掩碼之前的子串的字元數;strcspn表示從左邊計數,第一次出現掩碼之前的子串的字元數。代碼:
$pos = strpos('this a hello world program', ' '); // 4
- $pos = strpos('this a hello world program', 32); // 4
$pos = strrpos('this a hello world program', ' '); // 18
- $pos = strrpos('this a hello world program', 32); // 18
$str = strstr('this a hello world program', ' '); // " a hello world program"
- $str = strstr('this a hello world program', 32); // " a hello world program"
$str = stristr('this a hello world program', ' a'); // "a hello world program"
- $str = stristr('this a hello world program', 65); // "a hello world program"
$str = strrchr('this a hello world program', ' '); // " program"
- $str = strrchr('this a hello world program', 32); // " program"
$str1 = "12345 12345 12345";
- $len = strspn($str1, '12345'); // 5
- $len = strcspn($str1, ' '); // 5
複製代碼 |