PHP基础入门(六)

[!NOTE]

本篇文章仅用于个人交流与学习,如涉及侵权请联系站长删除!

​ 在前面我们已经简单了解了有关于PHP数组的一些基础概念,这篇文章将展示关于PHP数组字符串的一些方法,和一些加密算法。

1. 创建与修改字符串

  1. strlen()

    • 返回字符串长度。
    • strlen("Hello") → 5
    1
    2
    3
    4
    $baseString = "Hello, world!";
    echo "原始字符串: $baseString<br>";

    echo "长度: " . strlen($baseString) . "<br>"; // 13
  2. str_replace()

    • 替换字符串中的子串。
    • str_replace("world", "PHP", "Hello, world!") → "Hello, PHP!"
    1
    2
    3
    4
    5
    $baseString = "Hello, world!";
    echo "原始字符串: $baseString<br>";

    $replaced = str_replace("world", "PHP", $baseString);
    echo "替换后: $replaced<br>"; // Hello, PHP!
    • 代码分析:str_replace()函数的第一个参数"world"是原字符串中需要替换的部分,第二个参数"PHP"是用来替换的部分,第三个参数$baseString表示了使用什么字符串。
  3. str_split()

    • 将字符串拆分为数组
    • str_split("Hi") → ["H", "i"]
    1
    2
    3
    4
    5
    6
    $baseString = "Hello, world!";
    echo "原始字符串: $baseString<br>";

    $charArray = str_split($baseString);
    echo "字符数组: ";
    print_r($charArray); // ['H', 'e', 'l', 'l', 'o', ','...]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
// ===== 0. 创建与修改字符串 =====
echo "<h2>0. 创建与修改字符串</h2>";
$baseString = "Hello, world!";
echo "原始字符串: $baseString<br>";

// strlen()
echo "长度: " . strlen($baseString) . "<br>"; // 13

// str_replace()
$replaced = str_replace("world", "PHP", $baseString);
echo "替换后: $replaced<br>"; // Hello, PHP!

// str_split()
$charArray = str_split($baseString);
echo "字符数组: ";
print_r($charArray); // ['H','e','l','l','o',...]

// explode()
$expression = "3 + 5 * 2 - 8 / 4";
$tokens = explode(" ", $expression);
echo "<br>分词结果: ";
print_r($tokens); // ['3','+','5','*','2','-','8','/','4']

// str_word_count()
echo "<br>单词数: " . str_word_count($baseString) . "<br>"; // 2

// strpos()
echo "首次出现位置: " . strpos($baseString, "world") . "<br>"; // 7

// ===== 1. 字符串比较 =====
echo "<h2>1. 字符串比较</h2>";
$str1 = "Apple";
$str2 = "apple";
$str3 = "Banana";

// strcmp()
echo "区分大小写比较: " . strcmp($str1, $str2) . "<br>"; // -32

// strcasecmp()
echo "不区分大小写比较: " . strcasecmp($str1, $str2) . "<br>"; // 0

// strncmp()
echo "前3字符比较: " . strncmp("apple", "appetite", 3) . "<br>"; // 0

// strncasecmp()
echo "前3字符不区分大小写: " . strncasecmp("APPLE", "appetite", 3) . "<br>"; // 0

// ===== 2. 搜索与替换 =====
echo "<h2>2. 搜索与替换</h2>";
$searchStr = "Hello, world! Welcome to the world of PHP.";

// strpos()
echo "首次出现位置: " . strpos($searchStr, "world") . "<br>"; // 7

// strrpos()
echo "最后出现位置: " . strrpos($searchStr, "world") . "<br>"; // 25

// ===== 3. 字符串操作 =====
echo "<h2>3. 字符串操作</h2>";
$manipulateStr = "hello, world!";

// strtoupper()
echo "大写: " . strtoupper($manipulateStr) . "<br>"; // HELLO, WORLD!

// strtolower()
echo "小写: " . strtolower("HELLO") . "<br>"; // hello

// ucfirst()
echo "首字母大写: " . ucfirst($manipulateStr) . "<br>"; // Hello, world!

// ucwords()
echo "单词首字母大写: " . ucwords($manipulateStr) . "<br>"; // Hello, World!

// str_repeat()
echo "重复字符串: " . str_repeat("PHP ", 3) . "<br>"; // PHP PHP PHP

// str_shuffle()
echo "随机打乱: " . str_shuffle("ABCD") . "<br>"; // 随机结果如 "BCDA"

// str_replace()
echo "替换操作: " . str_replace("world", "PHP", $manipulateStr) . "<br>"; // hello, PHP!

// ===== 4. 字符串格式化 =====
echo "<h2>4. 字符串格式化</h2>";

// sprintf()
$name = "Alice";
$age = 28;
$formatted = sprintf("姓名: %s, 年龄: %d 岁", $name, $age);
echo $formatted . "<br>"; // 姓名: Alice, 年龄: 28 岁

// ===== 5. 编码与哈希 =====
echo "<h2>5. 编码与哈希</h2>";
$secret = "Sensitive Data";

// base64_encode()
$encoded = base64_encode($secret);
echo "Base64 编码: $encoded<br>"; // U2Vuc2l0aXZlIERhdGE=

// base64_decode()
echo "Base64 解码: " . base64_decode($encoded) . "<br>"; // Sensitive Data

// md5()
echo "MD5 哈希: " . md5($secret) . "<br>"; // 32位哈希值

// sha1()
echo "SHA1 哈希: " . sha1($secret) . "<br>"; // 40位哈希值

// hash()
echo "SHA256 哈希: " . hash('sha256', $secret) . "<br>"; // 64位哈希值

// hash_hmac()
$key = "secret_key";
echo "HMAC-SHA256: " . hash_hmac('sha256', $secret, $key) . "<br>"; // 带密钥的哈希

// ===== 6. 加密与解密 =====
echo "<h2>6. 加密与解密</h2>";
$plaintext = "Confidential Info";
$key = "my_secret_key_123";
$iv = random_bytes(16); // 初始化向量

// openssl_encrypt()
$ciphertext = openssl_encrypt(
$plaintext,
'aes-256-cbc',
$key,
OPENSSL_RAW_DATA,
$iv
);
$encrypted = base64_encode($iv . $ciphertext);
echo "加密结果: $encrypted<br>";

// openssl_decrypt()
$decoded = base64_decode($encrypted);
$iv = substr($decoded, 0, 16);
$ciphertext = substr($decoded, 16);
$decrypted = openssl_decrypt(
$ciphertext,
'aes-256-cbc',
$key,
OPENSSL_RAW_DATA,
$iv
);
echo "解密结果: $decrypted<br>"; // Confidential Info

// ===== 7. 字符串转换 =====
echo "<h2>7. 字符串转换</h2>";

// bin2hex()
$binary = "PHP";
$hex = bin2hex($binary);
echo "十六进制: $hex<br>"; // 504850

// hex2bin()
echo "二进制还原: " . hex2bin($hex) . "<br>"; // PHP

// ===== 8. 字符串验证 =====
echo "<h2>8. 字符串验证</h2>";

// ctype_alnum()
$test1 = "Hello123";
echo "'$test1' 是否字母数字: " . (ctype_alnum($test1) ? "是" : "否") . "<br>"; // 是

// ctype_alpha()
$test2 = "Hello";
echo "'$test2' 是否纯字母: " . (ctype_alpha($test2) ? "是" : "否") . "<br>"; // 是

// ctype_digit()
$test3 = "12345";
echo "'$test3' 是否纯数字: " . (ctype_digit($test3) ? "是" : "否") . "<br>"; // 是

// ctype_space()
$test4 = " \t\n";
echo "'$test4' 是否空白符: " . (ctype_space($test4) ? "是" : "否") . "<br>"; // 是

// ctype_xdigit()
$test5 = "1aF3";
echo "'$test5' 是否十六进制字符: " . (ctype_xdigit($test5) ? "是" : "否") . "<br>"; // 是
?>