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
echo "<h2>0. 创建与修改字符串</h2>"; $baseString = "Hello, world!"; echo "原始字符串: $baseString<br>";
echo "长度: " . strlen($baseString) . "<br>";
$replaced = str_replace("world", "PHP", $baseString); echo "替换后: $replaced<br>";
$charArray = str_split($baseString); echo "字符数组: "; print_r($charArray);
$expression = "3 + 5 * 2 - 8 / 4"; $tokens = explode(" ", $expression); echo "<br>分词结果: "; print_r($tokens);
echo "<br>单词数: " . str_word_count($baseString) . "<br>";
echo "首次出现位置: " . strpos($baseString, "world") . "<br>";
echo "<h2>1. 字符串比较</h2>"; $str1 = "Apple"; $str2 = "apple"; $str3 = "Banana";
echo "区分大小写比较: " . strcmp($str1, $str2) . "<br>";
echo "不区分大小写比较: " . strcasecmp($str1, $str2) . "<br>";
echo "前3字符比较: " . strncmp("apple", "appetite", 3) . "<br>";
echo "前3字符不区分大小写: " . strncasecmp("APPLE", "appetite", 3) . "<br>";
echo "<h2>2. 搜索与替换</h2>"; $searchStr = "Hello, world! Welcome to the world of PHP.";
echo "首次出现位置: " . strpos($searchStr, "world") . "<br>";
echo "最后出现位置: " . strrpos($searchStr, "world") . "<br>";
echo "<h2>3. 字符串操作</h2>"; $manipulateStr = "hello, world!";
echo "大写: " . strtoupper($manipulateStr) . "<br>";
echo "小写: " . strtolower("HELLO") . "<br>";
echo "首字母大写: " . ucfirst($manipulateStr) . "<br>";
echo "单词首字母大写: " . ucwords($manipulateStr) . "<br>";
echo "重复字符串: " . str_repeat("PHP ", 3) . "<br>";
echo "随机打乱: " . str_shuffle("ABCD") . "<br>";
echo "替换操作: " . str_replace("world", "PHP", $manipulateStr) . "<br>";
echo "<h2>4. 字符串格式化</h2>";
$name = "Alice"; $age = 28; $formatted = sprintf("姓名: %s, 年龄: %d 岁", $name, $age); echo $formatted . "<br>";
echo "<h2>5. 编码与哈希</h2>"; $secret = "Sensitive Data";
$encoded = base64_encode($secret); echo "Base64 编码: $encoded<br>";
echo "Base64 解码: " . base64_decode($encoded) . "<br>";
echo "MD5 哈希: " . md5($secret) . "<br>";
echo "SHA1 哈希: " . sha1($secret) . "<br>";
echo "SHA256 哈希: " . hash('sha256', $secret) . "<br>";
$key = "secret_key"; echo "HMAC-SHA256: " . hash_hmac('sha256', $secret, $key) . "<br>";
echo "<h2>6. 加密与解密</h2>"; $plaintext = "Confidential Info"; $key = "my_secret_key_123"; $iv = random_bytes(16);
$ciphertext = openssl_encrypt( $plaintext, 'aes-256-cbc', $key, OPENSSL_RAW_DATA, $iv ); $encrypted = base64_encode($iv . $ciphertext); echo "加密结果: $encrypted<br>";
$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>";
echo "<h2>7. 字符串转换</h2>";
$binary = "PHP"; $hex = bin2hex($binary); echo "十六进制: $hex<br>";
echo "二进制还原: " . hex2bin($hex) . "<br>";
echo "<h2>8. 字符串验证</h2>";
$test1 = "Hello123"; echo "'$test1' 是否字母数字: " . (ctype_alnum($test1) ? "是" : "否") . "<br>";
$test2 = "Hello"; echo "'$test2' 是否纯字母: " . (ctype_alpha($test2) ? "是" : "否") . "<br>";
$test3 = "12345"; echo "'$test3' 是否纯数字: " . (ctype_digit($test3) ? "是" : "否") . "<br>";
$test4 = " \t\n"; echo "'$test4' 是否空白符: " . (ctype_space($test4) ? "是" : "否") . "<br>";
$test5 = "1aF3"; echo "'$test5' 是否十六进制字符: " . (ctype_xdigit($test5) ? "是" : "否") . "<br>"; ?>
|