国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

PHP substr()函數的幾個程序應用

2024-05-04 23:24:12
字體:
來源:轉載
供稿:網友

substr()函數介紹

substr() 函數返回字符串的一部分。

語法:substr(string,start,length)。

  • string:必需。規定要返回其中一部分的字符串。
  • start:必需。規定在字符串的何處開始。正數 - 在字符串的指定位置開始;負數 - 在從字符串結尾的指定位置開始;0 - 在字符串中的第一個字符處開始。
  • charlist:可選。規定要返回的字符串長度。默認是直到字符串的結尾。正數 - 從 start 參數所在的位置返回;負數 - 從字符串末端返回。

注釋:如果 start 是負數且 length 小于等于 start,則 length 為 0。

Program List:負值的start參數

1 <?php
2     $rest = substr("abcdef", -1);    // returns "f"
3     echo $rest.'<br />';
4     $rest = substr("abcdef", -2);    // returns "ef"
5     echo $rest.'<br />';
6     $rest = substr("abcdef", -3, 1); // returns "d"
7     echo $rest.'<br />';
8 ?>

程序運行結果:

1 f
2 ef
3 d

Program List:負值的length參數

就是從start位置開始,若length為負值的話,就從字符串的末尾開始數。substr("abcdef", 2, -1)的話,就是從c開始,然后-1說明截取到e,就是要截取cde。

01 <?php
02     $rest = substr("abcdef", 0, -1);  // returns "abcde"
03     echo $rest.'<br />';
04     $rest = substr("abcdef", 2, -1);  // returns "cde"
05     echo $rest.'<br />';
06     $rest = substr("abcdef", 4, -4);  // returns ""
07     echo $rest.'<br />';
08     $rest = substr("abcdef", -3, -1); // returns "de"
09     echo $rest.'<br />';
10 ?>

程序運行結果:

1 abcde
2 cde
3 de

Program List:基本的substr()函數用法

01 <?php
02 echo substr('abcdef', 1);     // bcdef
03 echo '<br />';
04 echo substr('abcdef', 1, 3);  // bcd
05 echo '<br />';
06 echo substr('abcdef', 0, 4);  // abcd
07 echo '<br />';
08 echo substr('abcdef', 0, 8);  // abcdef
09 echo '<br />';
10 echo substr('abcdef', -1, 1); // f
11 echo '<br />';
12 // Accessing single characters in a string
13 // can also be achieved using "square brackets"
14 $string = 'abcdef';
15 echo $string[0];                 // a
16 echo '<br />';
17 echo $string[3];                 // d
18 echo '<br />';
19 echo $string[strlen($string)-1]; // f
20 echo '<br />';
21 ?>

程序運行結果:

1 bcdef
2 bcd
3 abcd
4 abcdef
5 f
6 a
7 d
8 f

Program List:移除后綴

01 <?php
02 //removes string from the end of other
03 function removeFromEnd($string, $stringToRemove)
04 {
05     // 獲得需要移除的字符串的長度
06     $stringToRemoveLen = strlen($stringToRemove);
07     // 獲得原始字符串的長度
08     $stringLen = strlen($string);
09     
10     // 計算出需要保留字符串的長度
11     $pos = $stringLen - $stringToRemoveLen;
12     
13     $out = substr($string, 0, $pos);
14     return $out;
15 }
16 $string = 'nowamagic.jpg.jpg';
17 $result = removeFromEnd($string, '.jpg');
18 echo $result;
19 ?>

程序運行結果:

1 nowamagic.jpg

Program List:太長的字符串只顯示首尾,中間用省略號代替

01 <?php
02 $file = "Hellothisfilehasmorethan30charactersandthisfayl.exe";
03 function funclongwords($file)
04 {
05     if (strlen($file) > 30)
06     {
07         $vartypesf = strrchr($file,".");
08         // 獲取字符創總長度
09         $vartypesf_len = strlen($vartypesf);
10         // 截取左邊15個字符
11         $word_l_w = substr($file,0,15);
12         // 截取右邊15個字符
13         $word_r_w = substr($file,-15);
14         $word_r_a = substr($word_r_w,0,-$vartypesf_len);
15         return $word_l_w."...".$word_r_a.$vartypesf;
16     }
17     else
18         return $file;
19 }
20 // RETURN: Hellothisfileha...andthisfayl.exe
21 $result = funclongwords($file);
22 echo $result;
23 ?>

程序運行結果:

1 Hellothisfileha...andthisfayl.exe

Program List:將多出的文字顯示為省略號

很多時候我們需要顯示固定的字數,多出的字數用省略號代替。

01 <?php
02 $text = 'welcome to nowamagic, I hope you can find something you wanted.';
03 $result = textLimit($text, 30);
04 echo $result;
05 function textLimit($string, $length, $replacer = '...')
06 {
07     if(strlen($string) > $length)
08     return (preg_match('/^(.*)/W.*$/', substr($string, 0, $length+1),$matches) ? $matches[1] : substr($string, 0, $length)) . $replacer;
09   
10     return $string;
11 }
12 ?>

程序運行結果:

1 welcome to nowamagic, I hope...

Program List:格式化字符串

有時候我們需要格式化字符串,比如電話號碼。

view source
print?
01 <?php
02 function str_format_number($String, $Format)
03 {
04     if ($Format == '') return $String;
05     if ($String == '') return $String;
06     $Result = '';
07     $FormatPos = 0;
08     $StringPos = 0;
09     while ((strlen($Format) - 1) >= $FormatPos)
10     {
11         //If its a number => stores it
12         if (is_numeric(substr($Format, $FormatPos, 1)))
13         {
14             $Result .= substr($String, $StringPos, 1);
15             $StringPos++;
16         //If it is not a number => stores the caracter
17         }
18         else
19         {
20             $Result .= substr($Format, $FormatPos, 1);
21         }
22         //Next caracter at the mask.
23         $FormatPos++;
24     }
25     return $Result;
26 }
27 // For phone numbers at Buenos Aires, Argentina
28 // Example 1:
29     $String = "8607562337788";
30     $Format = "+00 0000 0000000";
31     echo str_format_number($String, $Format);
32     echo '<br />';
33 // Example 2:
34     $String = "8607562337788";
35     $Format = "+00 0000 00.0000000";
36     echo str_format_number($String, $Format);
37     echo '<br />';
38 // Example 3:
39     $String = "8607562337788";
40     $Format = "+00 0000 00.000 a";
41     echo str_format_number($String, $Format);
42     echo '<br />';
43 ?>

程序運行結果:

1 +86 0756 2337788
2 +86 0756 23.37788
3 +86 0756 23.377 a

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大石桥市| 巴马| 五常市| 夏邑县| 波密县| 遂川县| 彭水| 双桥区| 合肥市| 新丰县| 岳普湖县| 句容市| 洛阳市| 武平县| 垦利县| 金华市| 多伦县| 紫云| 双牌县| 安泽县| 晋中市| 石门县| 白山市| 桂东县| 丽江市| 九江市| 太和县| 勐海县| 东乡族自治县| 正定县| 长泰县| 许昌市| 溆浦县| 纳雍县| 平陆县| 南郑县| 江西省| 朝阳市| 江华| 清丰县| 青州市|