PHP程式,用於查詢字串中的字元數


要查詢字串中的字元數,PHP 程式碼如下所示 -

示例

 現場演示

<?php
   $my_string = "Hi there, this is a sample ";
   $len = mb_strlen($my_string);
   print_r("The number of characters in the string is ");
   echo $len;
?>

輸出

The number of characters in the string is 27

上面定義了一個字串,它在中間有多於所需的空格 -

$my_string = "Hi there, this is a sample ";

接下來,使用“strlen”函式計算字串的長度。此長度被分配給一個變數。這將提供字串中存在的字元,包括空格 -

$len = mb_strlen($my_string);

然後將計算出的長度列印在螢幕上 -

print_r("The number of characters in the string is ");
echo $len;

上次更新的時間: 17-8-2020

834瀏覽

開啟你的 職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.