<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # PHP 字符串 > 原文: [https://zetcode.com/lang/php/strings/](https://zetcode.com/lang/php/strings/) 在 PHP 編程教程的這一部分中,我們將更詳細地處理字符串數據。 字符串是計算機語言中非常重要的數據類型。 這就是為什么我們將一整章專門討論在 PHP 中使用字符串的原因。 ## PHP 字符串字面值 字符串字面值是表示計算機程序文本內的字符串值的符號。 在 PHP 中,可以使用單引號,雙引號或使用 heredoc 或 nowdoc 語法創建字符串。 `literals.php` ```php <?php $a = "PHP"; $b = 'PERL'; echo $a, $b; ``` 在此代碼示例中,我們創建兩個字符串并將它們分配給`$a`和`$b`變量。 我們用`echo`關鍵字打印它們。 第一個字符串使用雙引號定界符創建,第二個字符串使用單引號定界符。 下一個示例將使用 heredoc 語法創建一個字符串。 Heredoc 保留文本中的換行符和其他空格(包括縮進)。 heredoc 使用`<<<`來創建,后跟定界標識符,然后從下一行開始,在要引用的文本之后,再在同一行中以相同的標識符關閉。 結束標識符不能縮進。 它只能包含字母數字字符和下劃線,并且必須以非數字字符或下劃線開頭。 `heredoc.php` ```php <?php $str = <<<TEXT "That is just as I intended." Vautrin said. "You know quite well what you are about. Good, my little eaglet! You are born to command, you are strong, you stand firm on your feet, you are game! I respect you." TEXT; echo $str, "\n"; ``` 該示例打印直接語音的示例。 ```php $ php heredoc.php "That is just as I intended." Vautrin said. "You know quite well what you are about. Good, my little eaglet! You are born to command, you are strong, you stand firm on your feet, you are game! I respect you." ``` 這是示例的輸出。 nowdoc 的指定方式與 Heredoc 相似,但是在 nowdoc 內部未進行任何解析。 nowdoc 的標識與 Heredocs 所使用的序列相同`<<<`,但是后面的標識符用單引號引起來,例如`<<<'TEXT'`。 `nowdoc.php` ```php <?php $str = <<<'TEXT' Fear is among the greatest obstacles which prevent us from enjoying life to its fullest extent. Since of the most commonly held fears among people are the fear of heights and the fear of falling from heights. Rock climbing is a fantastic way to conquer these fears. TEXT; echo $str, "\n"; ``` 該示例使用 nowdoc 語法打印三個句子。 ```php $ php nowdoc.php Fear is among the greatest obstacles which prevent us from enjoying life to its fullest extent. Since of the most commonly held fears among people are the fear of heights and the fear of falling from heights. Rock climbing is a fantastic way to conquer these fears. ``` 這是`nowdoc.php`腳本的輸出。 ## PHP 插值 變量被插入雙引號中的字符串中。 `interpolation.php` ```php <?php $quantity = 5; echo "There are $quantity roses in the vase\n"; ``` `$quantity`變量將在字符串輸出中替換為其值。 ```php $ php interpolation.php There are 5 roses in the vase ``` 這是`interpolation.php`腳本的輸出。 當變量名稱位于另一個字符旁邊時,可以使用大括號。 `curlybraces.php` ```php <?php $quantity = 5; $item_name = "rose"; echo "There are $quantity {$item_name}s in the vase\n"; ``` 沒有花括號,PHP 解釋器將查找`$item_names`變量,該變量不存在。 ```php $ php curlybraces.php There are 5 roses in the vase ``` 這是`curlybraces.php`腳本的輸出。 ## PHP 字符串連接 PHP 使用點`.`運算符來連接字符串。 ```php php > echo "PHP " . "language\n"; PHP language ``` 該示例連接了兩個字符串。 ```php php > $a = "Java "; php > $a .= "language\n"; php > echo $a; Java language ``` PHP 還支持`.=`復合運算符。 ## PHP 轉義字符 轉義字符是指定用于對字符序列中緊隨其后的字符調用替代解釋的單個字符。 ```php php> echo " bbb\raaa"; aaabbb ``` 回車`\r`是控制字符,用于將行尾返回到行首。 `strophe.php` ```php <?php echo "Incompatible, it don't matter though\n'cos someone's bound to hear my cry\n"; echo "Speak out if you do\nYou're not easy to find\n"; ``` 新行是一個控制字符,它開始一行新文本。 ```php $ php strophe.php Incompatible, it don't matter though 'cos someone's bound to hear my cry Speak out if you do You're not easy to find ``` 這是`strophe.php`腳本的輸出。 ```php php> echo "Towering\tinferno\n"; Towering inferno ``` 水平選項卡在文本之間放置一個空格。 ```php "Johnie's dog" 'Johnie\'s dog' ``` 單引號和雙引號可以嵌套。 或者,如果僅使用單引號,則可以使用反斜杠來轉義單引號的默認含義。 `backslash.php` ```php <?php $text = " \"That is just as I intended.\" Vautrin said. \"You know quite well what you are about. Good, my little eaglet! You are born to command, you are strong, you stand firm on your feet, you are game! I respect you.\" "; echo $text; ``` 在此示例中,我們有一個多行文本,其中包括直接語音。 雙引號用反斜杠字符轉義。 ```php php> $var = 233; php> echo "$var"; 233 php> echo "\$var is $var"; $var is 233 ``` 美元符號`$`在 PHP 中也有特殊含義; 它表示一個變量。 如果在字符串中使用了變量,則會對其進行插值,即使用變量的值。 為了回顯變量名,我們轉義了`$`字符`\$`。 ## PHP 字符串操作 PHP 具有大量有用的有用內置函數,可用于處理字符串。 ```php echo strlen("Eagle"); # prints 5 echo strtoupper("Eagle"); # prints EAGLE echo strtolower("Eagle"); # prints eagle ``` 在這里,我們使用三個函數。 `strlen()`函數返回字符串中的許多字符。 `strtoupper()`將字符轉換為大寫字母,`strtolower()`將字符轉換為小寫字母。 `letters.php` ```php <?php $sentence = "There are 22 apples"; $alphas = 0; $digits = 0; $spaces = 0; $length = strlen($sentence); for ($i = 0; $i < $length; $i++) { $c = $sentence[$i]; if (ctype_alpha($c)) $alphas++; if (ctype_digit($c)) $digits++; if (ctype_space($c)) $spaces++; } echo "There are $length characters.\n"; echo "There are $alphas alphabetic characters.\n"; echo "There are $digits digits.\n"; echo "There are $spaces spaces.\n"; ``` 在我們的示例中,我們有一個字符串句子。 我們計算句子中的絕對字符數,字母字符數,數字和空格。 為此,我們使用以下函數:`strlen()`,`ctype_alpha()`,`ctype_digit()`和`ctype_space()`。 ```php $ php letters.php There are 19 characters. There are 14 alphabetic characters. There are 2 digits. There are 3 spaces. ``` 這是`letters.php`腳本的輸出。 接下來,我們介紹`substr()`函數。 ```php echo substr("PHP language", 0, 3); # prints PHP echo substr("PHP language", -8); # prints language ``` 該函數返回字符串的一部分。 第一個參數是指定的字符串。 第二個參數是子字符串的開頭。 第三個參數是可選的。 它是返回的子字符串的長度。 默認值為返回到字符串末尾。 `str_repeat()`函數將字符串重復指定的次數。 `repeat.php` ```php <?php echo str_repeat("#", 18); echo "\nProject Neurea\n"; echo "Priority high\n"; echo "Security maximum\n"; echo str_repeat("#", 18); echo "\n"; ``` 我們使用`str_repeat()`函數創建兩行`#`字符。 ```php $ php repeat.php ################## Project Neurea Priority high Security maximum ################## ``` 這是`repeat.php`腳本的輸出。 在下一個示例中,我們將隨機修改一個字符串。 `shuffling.php` ```php <?php $string = "ZetCode"; echo str_shuffle($string), "\n"; echo str_shuffle($string), "\n"; echo str_shuffle($string), "\n"; echo str_shuffle($string), "\n"; echo str_shuffle($string), "\n"; echo str_shuffle($string), "\n"; echo str_shuffle($string), "\n"; ``` `str_shuffle()`隨機隨機播放一個字符串。 ```php $ php shuffling.php ZtCeoed eodtCZe toZeeCd oCdeteZ edtCZoe tdeCeoZ oeZdteC ``` 這是`shuffling.php`腳本的示例輸出。 `explode()`函數用于將字符串分成多個部分。 它返回一個分割字符串部分的數組。 `exploding.php` ```php <?php $nums = "1,2,3,4,5,6,7,8,9,10,11"; $vals = explode(",", $nums); $len = count($vals); echo "There are $len numbers in the string\n"; ``` 字符串中包含整數,并用逗號分隔。 我們計算整數的數量。 ```php $vals = explode(",", $nums); ``` 在這里,我們使用`explode()`函數分割文本。 每當找到點`,`字符時,該函數就會將其切成小段。 ```php $ php exploding.php There are 11 numbers in the string ``` 示例的輸出。 `teams1.php` ```php <?php echo "Ajax Amsterdam" . " - " . "Inter Milano " . "2:3\n"; echo "Real Madridi" . " - " . "AC Milano " . "3:3\n"; echo "Dortmund" . " - " . "Sparta Praha ". "2:1\n"; ``` 我們用點運算符連接字符串。 ```php $ php teams1.php Ajax Amsterdam - Inter Milano 2:3 Real Madridi - AC Milano 3:3 Dortmund - Sparta Praha 2:1 ``` 輸出不是最佳的。 我們將對其進行更改,以使其看起來更整潔。 `teams2.php` ```php <?php $teams = array( array("Ajax Amsterdam", "Inter Milano"), array("Real Madrid", "AC Milano"), array("Dortmund", "Sparta Praha") ); $results = array("2:3", "3:3", "2:1"); $i = 0; foreach ($teams as $team) { echo str_pad($team[0], 14); echo str_pad("-", 3, " ", STR_PAD_BOTH); echo str_pad($team[1], 14); echo str_pad($results[$i], 3, " ", STR_PAD_LEFT); echo "\n"; $i++; } ``` 我們使用`str_pad()`函數改善了輸出格式。 它將在字符串的左側,右側或兩側添加指定的字符串(在我們的示例中為空格)。 ```php $ php teams2.php Ajax Amsterdam - Inter Milano 2:3 Real Madrid - AC Milano 3:3 Dortmund - Sparta Praha 2:1 ``` 我們設法提供了更好的格式化輸出。 ## 字符數組 PHP 中的字符串是一個字符數組。 `array_of_chars.php` ```php <?php $site = "zetcode.com"; for ($i=0; $i < strlen($site); $i++) { $o = ord($site[$i]); echo "$site[$i] has ASCII code $o\n"; } ``` 在示例中,我們遍歷字符串并打印每個字符的 ASCII 碼。 ```php $site = "zetcode.com"; ``` 定義了一個字符串。 它包含十一個字符。 ```php for ($i=0; $i < strlen($site); $i++) { $o = ord($site[$i]); echo "$site[$i] has ASCII code $o\n"; } ``` 我們使用`for`循環遍歷字符串。 字符串的大小由`strlen()`函數確定。 `ord()`函數返回字符的 ASCII 值。 我們使用數組索引符號來獲取字符。 ```php $ php array_of_chars.php z has ASCII code 122 e has ASCII code 101 t has ASCII code 116 c has ASCII code 99 o has ASCII code 111 d has ASCII code 100 e has ASCII code 101 . has ASCII code 46 c has ASCII code 99 o has ASCII code 111 m has ASCII code 109 ``` 這是`array_of_chars.php`示例的輸出。 ## PHP 字符串格式化 字符串格式化或字符串插值是將各種值動態地放入字符串中。 `fruits.php` ```php <?php printf("There are %d oranges and %d apples in the basket.\n", 12, 32); ``` 我們使用`%d`格式說明符。 指定者希望傳遞一個整數值。 ```php $ php fruits.php There are 12 oranges and 32 apples in the basket. ``` 在下一個示例中,我們傳遞一個`float`和一個字符串值。 `height.php` ```php <?php printf("Height: %f %s\n", 172.3, "cm"); ``` 浮點值的格式說明符為`%f`,字符串為`%s`。 ```php $ php height.php Height: 172.300000 cm ``` 我們可能不喜歡上面示例中的數字默認情況下具有 6 個小數位的事實。 我們可以控制格式說明符中的小數位數。 `height2.php` ```php <?php printf("Height: %.1f %s\n", 172.3, 'cm'); ``` 小數點后跟一個整數控制小數位數。 在我們的例子中,數字恰好有一位小數。 ```php $ php height2.php Height: 172.3 cm ``` 下面的示例顯示其他格式設置選項。 `formatting.php` ```php <?php # hexadecimal printf("%x\n", 300); # octal printf("%o\n", 300); # scientific printf("%e\n", 300000); ``` 第一種格式適用于十六進制數字。 `x`字符以十六進制格式格式化數字。 `o`字符以八進制格式顯示數字。 `e`字符以科學格式顯示數字。 ```php $ php formatting.php 12c 454 3.000000e+5 ``` 下一個示例顯示三列數字。 `columns.php` ```php <?php foreach (range(1,11) as $num) { echo $num , " ", $num*$num, " ", $num*$num*$num, "\n"; } ``` 數字左對齊,輸出不整齊。 ```php $ php columns.php 1 1 1 2 4 8 3 9 27 4 16 64 5 25 125 6 36 216 7 49 343 8 64 512 9 81 729 10 100 1000 11 121 1331 ``` 為了解決這個問題,我們使用寬度說明符。 寬度說明符定義對象的最小寬度。 如果對象小于寬度,則將填充空格。 `columns2.php` ```php <?php foreach (range(1,11) as $num) { printf("%2d %3d %4d\n", $num, $num*$num, $num*$num*$num); } ``` 現在輸出看起來正常。 數字 2 表示第一列的寬度為 2 個字符。 ```php $ php columns2.php 1 1 1 2 4 8 3 9 27 4 16 64 5 25 125 6 36 216 7 49 343 8 64 512 9 81 729 10 100 1000 11 121 1331 ``` PHP 教程的這一部分介紹了字符串。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看