php+date+timezoe,PHP 字符串
# PHP 字符串
> 原文: [https://zetcode.com/lang/php/strings/](https://zetcode.com/lang/php/strings/)
在 PHP 編程教程的這一部分中,我們將更詳細地處理字符串數(shù)據(jù)。
字符串是計算機語言中非常重要的數(shù)據(jù)類型。 這就是為什么我們將一整章專門討論在 PHP 中使用字符串的原因。
## PHP 字符串字面值
字符串字面值是表示計算機程序文本內(nèi)的字符串值的符號。 在 PHP 中,可以使用單引號,雙引號或使用 heredoc 或 nowdoc 語法創(chuàng)建字符串。
`literals.php`
```php
$a = "PHP";
$b = 'PERL';
echo $a, $b;
```
在此代碼示例中,我們創(chuàng)建兩個字符串并將它們分配給`$a`和`$b`變量。 我們用`echo`關鍵字打印它們。 第一個字符串使用雙引號定界符創(chuàng)建,第二個字符串使用單引號定界符。
下一個示例將使用 heredoc 語法創(chuàng)建一個字符串。 Heredoc 保留文本中的換行符和其他空格(包括縮進)。 heredoc 使用`<<
`heredoc.php`
```php
$str = <<
"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 內(nèi)部未進行任何解析。 nowdoc 的標識與 Heredocs 所使用的序列相同`<<
`nowdoc.php`
```php
$str = <<
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
$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
$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 轉(zhuǎn)義字符
轉(zhuǎn)義字符是指定用于對字符序列中緊隨其后的字符調(diào)用替代解釋的單個字符。
```php
php> echo " bbb\raaa";
aaabbb
```
回車`\r`是控制字符,用于將行尾返回到行首。
`strophe.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'
```
單引號和雙引號可以嵌套。 或者,如果僅使用單引號,則可以使用反斜杠來轉(zhuǎn)義單引號的默認含義。
`backslash.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;
```
在此示例中,我們有一個多行文本,其中包括直接語音。 雙引號用反斜杠字符轉(zhuǎn)義。
```php
php> $var = 233;
php> echo "$var";
233
php> echo "\$var is $var";
$var is 233
```
美元符號`$`在 PHP 中也有特殊含義; 它表示一個變量。 如果在字符串中使用了變量,則會對其進行插值,即使用變量的值。 為了回顯變量名,我們轉(zhuǎn)義了`$`字符`\$`。
## PHP 字符串操作
PHP 具有大量有用的有用內(nèi)置函數(shù),可用于處理字符串。
```php
echo strlen("Eagle"); # prints 5
echo strtoupper("Eagle"); # prints EAGLE
echo strtolower("Eagle"); # prints eagle
```
在這里,我們使用三個函數(shù)。 `strlen()`函數(shù)返回字符串中的許多字符。 `strtoupper()`將字符轉(zhuǎn)換為大寫字母,`strtolower()`將字符轉(zhuǎn)換為小寫字母。
`letters.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";
```
在我們的示例中,我們有一個字符串句子。 我們計算句子中的絕對字符數(shù),字母字符數(shù),數(shù)字和空格。 為此,我們使用以下函數(shù):`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()`函數(shù)。
```php
echo substr("PHP language", 0, 3); # prints PHP
echo substr("PHP language", -8); # prints language
```
該函數(shù)返回字符串的一部分。 第一個參數(shù)是指定的字符串。 第二個參數(shù)是子字符串的開頭。 第三個參數(shù)是可選的。 它是返回的子字符串的長度。 默認值為返回到字符串末尾。
`str_repeat()`函數(shù)將字符串重復指定的次數(shù)。
`repeat.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()`函數(shù)創(chuàng)建兩行`#`字符。
```php
$ php repeat.php
##################
Project Neurea
Priority high
Security maximum
##################
```
這是`repeat.php`腳本的輸出。
在下一個示例中,我們將隨機修改一個字符串。
`shuffling.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()`函數(shù)用于將字符串分成多個部分。 它返回一個分割字符串部分的數(shù)組。
`exploding.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";
```
字符串中包含整數(shù),并用逗號分隔。 我們計算整數(shù)的數(shù)量。
```php
$vals = explode(",", $nums);
```
在這里,我們使用`explode()`函數(shù)分割文本。 每當找到點`,`字符時,該函數(shù)就會將其切成小段。
```php
$ php exploding.php
There are 11 numbers in the string
```
示例的輸出。
`teams1.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
$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()`函數(shù)改善了輸出格式。 它將在字符串的左側,右側或兩側添加指定的字符串(在我們的示例中為空格)。
```php
$ php teams2.php
Ajax Amsterdam - Inter Milano 2:3
Real Madrid - AC Milano 3:3
Dortmund - Sparta Praha 2:1
```
我們設法提供了更好的格式化輸出。
## 字符數(shù)組
PHP 中的字符串是一個字符數(shù)組。
`array_of_chars.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`循環(huán)遍歷字符串。 字符串的大小由`strlen()`函數(shù)確定。 `ord()`函數(shù)返回字符的 ASCII 值。 我們使用數(shù)組索引符號來獲取字符。
```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 字符串格式化
字符串格式化或字符串插值是將各種值動態(tài)地放入字符串中。
`fruits.php`
```php
printf("There are %d oranges and %d apples in the basket.\n", 12, 32);
```
我們使用`%d`格式說明符。 指定者希望傳遞一個整數(shù)值。
```php
$ php fruits.php
There are 12 oranges and 32 apples in the basket.
```
在下一個示例中,我們傳遞一個`float`和一個字符串值。
`height.php`
```php
printf("Height: %f %s\n", 172.3, "cm");
```
浮點值的格式說明符為`%f`,字符串為`%s`。
```php
$ php height.php
Height: 172.300000 cm
```
我們可能不喜歡上面示例中的數(shù)字默認情況下具有 6 個小數(shù)位的事實。 我們可以控制格式說明符中的小數(shù)位數(shù)。
`height2.php`
```php
printf("Height: %.1f %s\n", 172.3, 'cm');
```
小數(shù)點后跟一個整數(shù)控制小數(shù)位數(shù)。 在我們的例子中,數(shù)字恰好有一位小數(shù)。
```php
$ php height2.php
Height: 172.3 cm
```
下面的示例顯示其他格式設置選項。
`formatting.php`
```php
# hexadecimal
printf("%x\n", 300);
# octal
printf("%o\n", 300);
# scientific
printf("%e\n", 300000);
```
第一種格式適用于十六進制數(shù)字。 `x`字符以十六進制格式格式化數(shù)字。 `o`字符以八進制格式顯示數(shù)字。 `e`字符以科學格式顯示數(shù)字。
```php
$ php formatting.php
12c
454
3.000000e+5
```
下一個示例顯示三列數(shù)字。
`columns.php`
```php
foreach (range(1,11) as $num) {
echo $num , " ", $num*$num, " ",
$num*$num*$num, "\n";
}
```
數(shù)字左對齊,輸出不整齊。
```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
foreach (range(1,11) as $num) {
printf("%2d %3d %4d\n", $num, $num*$num, $num*$num*$num);
}
```
現(xiàn)在輸出看起來正常。 數(shù)字 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 教程的這一部分介紹了字符串。
《新程序員》:云原生和全面數(shù)字化實踐50位技術專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的php+date+timezoe,PHP 字符串的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 放授权代码的php文件夹,自己的项目如果
- 下一篇: 适合做公安网的php,gonganbei