關於PHP substr()函數的幾個程序

小云云
發布: 2023-03-17 10:58:02
原創
1955 人瀏覽過

substr()函數是什麼? substr() 函數是傳回字串的一部分。關於PHP substr()函數寫的程式很多,這篇主要就是介紹幾個用PHP 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.&#39;<br />&#39;;    
4        $rest = substr("abcdef", -2);    // returns "ef"    
5        echo $rest.&#39;<br />&#39;;    
6        $rest = substr("abcdef", -3, 1); // returns "d"    
7        echo $rest.&#39;<br />&#39;;    
8    ?>
登入後複製

程式運作結果:

1    f    

2    ef    

#3    d    ef    

#3  一樣  d    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.&#39;<br />&#39;;    
04        $rest = substr("abcdef", 2, -1);  // returns "cde"    
05        echo $rest.&#39;<br />&#39;;    
06        $rest = substr("abcdef", 4, -4);  // returns ""    
07        echo $rest.&#39;<br />&#39;;    
08        $rest = substr("abcdef", -3, -1); // returns "de"    
09        echo $rest.&#39;<br />&#39;;    
10    ?>
登入後複製

#程式運作結果:

##1    abcde    

2    cde    

3    de    

#Program用法

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

程式運作結果:

##1    bcdef    

2    bcd    

3    abcd 

ab##n##boo 5。

##1    nowamagic.jpg    

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 = &#39;nowamagic.jpg.jpg&#39;;    
17    $result = removeFromEnd($string, &#39;.jpg&#39;);    
18    echo $result;    
19    ?>
登入後複製

程式執行結果:

1    Hellothisfileha ...andthisfayl.exe    

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    welcome to nowamagic, I hope...    

Program List:格式化字串

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

01    <?php    
02    $text = &#39;welcome to nowamagic, I hope you can find something you wanted.&#39;;    
03    $result = textLimit($text, 30);    
04    echo $result;    
05    function textLimit($string, $length, $replacer = &#39;...&#39;)    
06    {    
07        if(strlen($string) > $length)    
08        return (preg_match(&#39;/^(.*)\W.*$/&#39;, substr($string, 0, $length+1), $matches) ? $matches[1] : substr($string, 0, $length)) . $replacer;    
09    
10        return $string;    
11    }    
12    ?>
登入後複製
#程式運作結果:

1    +86 0756 2337788    

##2    +86 0756 23.37788      +86 0756 23.37788 #     +86 0756 23.37788 #     +86 0756 23.37788 #    112

幾個簡單的PHP substr()函數小程序,但還是要得我們去理解,自己跟上思路才能順藤摸瓜寫出更好的程序來。

相關推薦:

php substr()函數處理中文詳解

php substr()函數字串截取用法實例解說

php substr()函數的用法

#關於php substr()函數的文章推薦10篇

php substr中文亂碼解決方法

以上是關於PHP substr()函數的幾個程序的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!