當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


PHP strlen()用法及代碼示例

strlen()是PHP中的內置函數,該函數返回給定字符串的長度。它以字符串作為參數並返回其長度。它計算包括所有空格和特殊字符的字符串的長度。

用法:

strlen($string)

參數:strlen()函數僅接受一個必需的參數$string。此參數表示需要返回其長度的字符串。


返回值:該函數返回$string的長度,包括所有空格和特殊字符。

例子:

Input : "abc"
Output : 3

Input : "\n chetna ;"
Output : 10
Explanation : '\n' is considered as a single character
              as it is an escape sequence.

Input : "geeks for geeks" 
Output :15

以下示例程序旨在說明PHP中的strlen()函數:

程序1:下麵的程序演示了PHP中strlen()函數的用法。

<?php 
    // PHP program to find the  
    // length of a given string  
      
    $str = "geeks for geeks"; 
      
    // prints the length of the string  
    // including the space  
    echo strlen($str); 
?> 

輸出:

15

程序2:下麵的程序演示了PHP中strlen()函數的使用,其中字符串具有特殊字符和轉義序列。

<?php 
    // PHP program to find the  
    // length of a given string which has  
    // special characters 
      
    $str = "\n chetna ;"; 
      
    // here '\n' has been counted as 1  
    echo strlen($str); 
?>

輸出:

10

參考:
http://php.net/manual/en/function.strlen.php



相關用法


注:本文由純淨天空篩選整理自ChetnaAgarwal大神的英文原創作品 PHP | strlen() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。