ホームページ  >  記事  >  バックエンド開発  >  PHP substr() 関数に関するいくつかのプログラム

PHP substr() 関数に関するいくつかのプログラム

小云云
小云云オリジナル
2017-11-11 11:02:331959ブラウズ

substr()関数とは何ですか? substr() 関数は文字列の一部を返します。 PHPのsubstr()関数を使って書かれたプログラムは数多くありますが、この記事では主にPHPのsubstr()関数を使って書かれたプログラムをいくつか紹介します。

構文: substr(文字列,開始,長さ)。

文字列: 必須。返される文字列の一部を指定します。

開始: 必須。文字列内のどこから開始するかを指定します。正の数値 - 文字列内の指定された位置から始まり、負の数値 - 文字列の末尾からの指定された位置から始まります。0 - 文字列内の最初の文字から始まります。

文字リスト: オプション。返される文字列の長さを指定します。デフォルトは文字列の終わりまでです。正の数値 - 開始パラメータの位置から返されます。負の数値 - 文字列の末尾から返されます。

注: start が負の数で、length が start 以下の場合、length は 0 になります。 MPROgram list: 負の値の開始パラメータ

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

プログラムリスト: 負の長さのパラメータ

は、長さが長さの場合、開始位置から開始されます長さは長さです。値が負の場合は、文字列の末尾から数えます。 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

プログラムリスト: 基本的なsubstr()関数の使用法

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 b cdef

2 bcd

3 abcd

4 abcdef

5 f

6 a

7 d

8 f

プログラムリスト: サフィックスを削除

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 nowamagic.jpg

プログラムリスト: 長すぎる文字 先頭と終わりの文字列が表示され、代わりに省略記号が中央で使用されます。

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

プログラム リスト: 余分なテキストを省略記号として表示します

何度も表示する必要があります。一定数の余分な文字は省略記号に置き換えられます。

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 nowamagic へようこそ、願っています...

プログラムリスト: 文字列の書式設定

電話番号などの文字列を書式設定する必要がある場合があります。

01    <?php    
02    function str_format_number($String, $Format)    
03    {    
04        if ($Format == &#39;&#39;) return $String;    
05        if ($String == &#39;&#39;) return $String;    
06        $Result = &#39;&#39;;    
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 &#39;<br />&#39;;    
33    // Example 2:    
34        $String = "8607562337788";    
35        $Format = "+00 0000 00.0000000";    
36        echo str_format_number($String, $Format);    
37        echo &#39;<br />&#39;;    
38    // Example 3:    
39        $String = "8607562337788";    
40        $Format = "+00 0000 00.000 a";    
41        echo str_format_number($String, $Format);    
42        echo &#39;<br />&#39;;    
43    ?>

プログラムの実行結果:

1 +86 0756 2337788

2 +86 0756 23.37788

いくつかの単純な PHP substr() 関数アプレットですが、それらを理解する必要があります、のみアイデアを追い続けることで、ヒントをたどり、より良いプログラムを書くことができるでしょうか。

関連おすすめ:

php substr()関数の処理詳細中国語説明

php substr()関数の文字列インターセプトの使用例説明

php substr()関数の使い方

php substr()についてFunction 10のおすすめ記事

php substr中国語文字化け解決策

以上がPHP substr() 関数に関するいくつかのプログラムの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。