php字串比較與尋找方法詳解

來源:互聯網
上載者:User
  1. var_dump(0 == 'test');
  2. var_dump(0 == '');
  3. var_dump(5 > 't');
  4. 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的字串。 如下代碼:

  1. // 子串

  2. var_dump(substr('1234567890', 8)); // 90
  3. var_dump(substr('1234567890', 0, 2)); // 12
  4. // 反方向子串
  5. var_dump(substr('1234567890', -8)); // 34567890
  6. var_dump(substr('1234567890', -8, -2)); // 345678
  7. var_dump(substr('1234567890', -8, 2)); // 34

  8. // 插入

  9. var_dump(substr_replace('1234567890', 'a', 0, 0)); // a1234567890
  10. // 刪除
  11. var_dump(substr_replace('1234567890', '', 8)); // 12345678
  12. // 反方向刪除
  13. var_dump(substr_replace('1234567890', '', -2, -1)); // 123456780
  14. // 替換
  15. var_dump(substr_replace('1234567890', 'a', 0, 1)); // a234567890
  16. // 反方向替換
  17. var_dump(substr_replace('1234567890', 'a', -2, -1)); // 12345678a0

  18. // 字串反轉

  19. var_dump(strrev('1234567890')); // 0987654321

  20. // 重複字串

  21. var_dump(str_repeat('12', 3)); // 121212

  22. // 填充字串

  23. var_dump(str_pad('a', 10, '12')); // a121212121
  24. var_dump(str_pad('a', 10, '12', str_pad_left)); // 121212121a
  25. 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)來繼續取值。代碼:

  1. $str = '1,2,3';

  2. $arr1 = explode(',', $str); // array('1', '2', '3')
  3. $arr2 = explode(',', $str, 2); // array('1', '2,3')

  4. $str1 = implode(',', $arr1); // '1,2,3'

  5. $str2 = strtok($str, ','); // 1

  6. $str3 = strtok(','); // 2
  7. $str4 = strtok(','); // 3

  8. // array(86, 10, 88888888, 'beijin')

  9. $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表示從左邊計數,第一次出現掩碼之前的子串的字元數。代碼:

  1. $pos = strpos('this a hello world program', ' '); // 4

  2. $pos = strpos('this a hello world program', 32); // 4

  3. $pos = strrpos('this a hello world program', ' '); // 18

  4. $pos = strrpos('this a hello world program', 32); // 18

  5. $str = strstr('this a hello world program', ' '); // " a hello world program"

  6. $str = strstr('this a hello world program', 32); // " a hello world program"

  7. $str = stristr('this a hello world program', ' a'); // "a hello world program"

  8. $str = stristr('this a hello world program', 65); // "a hello world program"

  9. $str = strrchr('this a hello world program', ' '); // " program"

  10. $str = strrchr('this a hello world program', 32); // " program"

  11. $str1 = "12345 12345 12345";

  12. $len = strspn($str1, '12345'); // 5
  13. $len = strcspn($str1, ' '); // 5

複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.