2014年3月7日 星期五

PHP 取得原始字串中的部分字串

如果想在 PHP 中擷取某字串的部分字串,就是要使用 substr 這個 function 了,它與其他語言不同的是多了可以傳負值,若是傳負值他會從結尾開始計算擷取的字串位置範圍。以下就附上幾個範例程式:
  1. <?php

  2.     echo substr ("abcde", 1);  //  result "bcde"

  3.     echo substr ("abcde", 1, 3);  //  result "bcd"

  4.     echo substr ("abcde", -2);  //  result "de"

  5.     echo substr ("abcde", -3, 1);  //  result "c"

  6.     echo substr ("abcde", 1, -1);  //  result "bcd"

  7. ?>

沒有留言:

張貼留言