PHP - substr() 函式



PHP 的 substr() 函式用於從給定的字串中提取一部分字串,並將該提取的(子部分)字串作為新字串返回。

它接受兩個可選引數 startlength。以下是列出的重要要點

  • 如果 start 為正數,則返回的字串將從 start 位置開始(從 0 開始)。
  • 如果 start 為負數,則返回的字串將從字串末尾的指定位置開始。
  • 如果字串長度小於 start 引數值,則將返回空字串。

以下是關於 length 引數的關鍵要點:

  • 如果 length 為正數,則返回的字串將包含從“start”引數開始的那麼多字元。
  • 如果 length 為負數,則將從字串末尾省略那麼多字元。
  • 如果 length 給定且為 0,則將返回空字串。
  • 如果省略 length 或為“null”,則子字串將從“start”位置開始,並繼續到字串的末尾。

注意:您不能向其傳遞單個 length 引數,它將始終將其視為提取字串的起始位置,而不是長度。

語法

以下是 PHP substr() 函式的語法:

substr(string $str, int $start, int $length): string

引數

此函式接受三個引數,如下所示:

  • str - 指定輸入字串。
  • start - 指定字串提取應從何處開始。
  • length - 指定要提取的新字串的長度。

返回值

此函式返回原始字串的一部分字串。

示例 1

如果給定 start 引數且為 正數,則 PHP substr() 函式從給定的“start index”開始提取字串,並繼續到字串的末尾:

<?php
   $str = "Tutorialspoint";
   echo "The given string is: $str";
   $start = 2;
   echo "\nThe extraction will start at: $start";
   echo "\nThe extracted string is: ";
   #using substr() function
   echo substr($str, $start);
?>

輸出

以上程式產生以下輸出:

The given string is: Tutorialspoint
The extraction will start at: 2
The extracted string is: torialspoint

示例 2

如果同時給出 startlength 引數且均為 正數,則此函式從給定的“start index”開始提取字串,並繼續到給定的長度:

<?php
   $str = "Hey!, I Love PHP!";
   echo "The given string is: $str";
   $start = 6;
   $length = 10;
   echo "\nThe extraction will start at: $start";
   echo "\nThe length of the extracted string: $length";
   echo "\nThe extracted string is: ";
   #using substr() function
   echo substr($str, $start, $length);
?>

輸出

執行上述程式後,將顯示以下輸出:

The given string is: Hey!, I Love PHP!
The extraction will start at: 6
The length of the extracted string: 10
The extracted string is: I Love PHP

示例 3

如果給定的長度為 0,則將返回一個 字串:

<?php
   $str = "Hello World!";
   echo "The given string is: $str";
   $start = 1;
   $length = 0;
   echo "\nThe extraction will start at: $start";
   echo "\nThe length of the extracted string: $length";
   echo "\nThe extracted string is: ";
   #using substr() function
   echo substr($str, $start, $length);
?>

輸出

以下是上述程式的輸出:

The given string is: Hey, I Love PHP!
The length of the extracted string: 0
The extracting will start at: 1
The extracted string is:

示例 4

如果給定的 start 引數為 負數,則字串將從字串“末尾”的指定位置開始:

<?php
   $str = "Hello World";
   echo "The given string is: $str";
   #if you pass start = -1, only "d" will be returned, because no more element to extract,
   #so it will return only d even you pass length as 5
   $start = -5;
   $length = 5;
   echo "\nThe extraction will start at: $start";
   echo "\nThe length of the extracted string: $length";
   echo "\nThe extracted string is: ";
   #using substr() function
   echo substr($str, $start, $length);
?>

輸出

執行上述程式後,將顯示以下輸出:

The given string is: Hello World
The extraction will start at: -5
The length of the extracted string: 5
The extracted string is: World

示例 5

如果給定的長度為 負數,則將從字串末尾“省略”那麼多元素:

<?php
   $str = "Hello From TP!!";
   echo "The given string is: $str";
   $start = 0;
   $length = -3;
   echo "\nThe extraction will start at: $start";
   echo "\nThe length of the extracted string: $length";
   echo "\nThe extracted string is: ";
   #using substr() function
   echo substr($str, $start, $length);
?>

輸出

上述程式生成以下輸出:

The extraction will start at: 0
The length of the extracted string: -3
The extracted string is: Hello From T

示例 6

如果省略了“length”引數,則字串將從指定的 start index 開始,並繼續提取到給定字串的“末尾”:

<?php
   $str = "Hey!, Welcome to TP";
   echo "The given string is: $str";
   $start = 6;
   echo "\nThe extraction will start at: $start";
   echo "\nThe extracted string is: ";
   #using substr() function
   echo substr($str, $start);
?>

輸出

這將產生以下結果:

The given string is: Hey!, Welcome to TP
The extraction will start at: 6
The extracted string is: Welcome to TP

示例 7

如果字串“length”小於 start 引數值,則將返回一個 字串:

<?php
   $str = "I Love Coding!";
   echo "The given string is: $str";
   #an empty string will be returned because start > string length
   $start = 20;
   echo "\nThe extraction will start at: $start";
   echo "\nThe extracted string is: ";
   #using substr() function
   echo substr($str, $start);
?>

輸出

以下是上述程式的輸出:

The given string is: I Love Coding!
The extraction will start at: 20
The extracted string is:
php_function_reference.htm
廣告