PHP历理 检测并突出显示不符合 MySQL utf8 标准的字符
<?php
/**
* 检测并突出显示不符合 MySQL utf8 标准的字符
*
* @param string $str 待检测的字符串
* @return string 处理后的字符串,不符合的字符会被标红
*/
function highlightNonMysqlUtf8Chars($str) {
$result = '';
$len = mb_strlen($str, 'UTF-8');
for ($i = 0; $i < $len; $i++) {
$char = mb_substr($str, $i, 1, 'UTF-8');
$unicodeCodePoint = mb_ord($char, 'UTF-8');
// MySQL utf8 只支持 U+0000 至 U+FFFF 范围内的字符
if ($unicodeCodePoint > 0xFFFF) {
$result .= '<span style="background-color:#ffebee; color:#b71c1c; padding:0 2px; font-weight:bold;">'
. htmlspecialchars($char)
. '</span>';
} else {
$result .= htmlspecialchars($char);
}
}
return $result;
}
// 处理表单提交
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$inputText = $_POST['input_text'] ?? '';
$processedText = highlightNonMysqlUtf8Chars($inputText);
// 检查是否存在不符合的字符
$hasNonCompatibleChars = strpos($processedText, '<span') !== false;
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>MySQL UTF8 兼容性检测</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
line-height: 1.6;
color: #333;
}
h1 {
color: #1a365d;
text-align: center;
}
.container {
background-color: #f8fafc;
border-radius: 8px;
padding: 20px;
margin-top: 20px;
box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);
}
textarea {
width: 100%;
padding: 10px;
margin: 10px 0;
border: 1px solid #cbd5e0;
border-radius: 4px;
box-sizing: border-box;
font-family: monospace;
min-height: 150px;
}
button {
background-color: #4a5568;
color: white;
padding: 10px 15px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}
button:hover {
background-color: #2d3748;
}
.result {
margin-top: 20px;
padding: 15px;
background-color: white;
border: 1px solid #e2e8f0;
border-radius: 4px;
font-family: monospace;
white-space: pre-wrap;
min-height: 100px;
}
.success {
color: #38a169;
font-weight: bold;
}
.warning {
color: #e53e3e;
font-weight: bold;
}
.info {
margin-top: 15px;
padding: 10px;
background-color: #edf2f7;
border-radius: 4px;
font-size: 14px;
}
</style>
</head>
<body>
<h1>MySQL UTF8 兼容性检测</h1>
<div class="container">
<form method="post" action="">
<label for="input_text">输入文本:</label>
<textarea
id="input_text"
name="input_text"
placeholder="在此输入需要检测的文本..."
><?= htmlspecialchars($inputText ?? '') ?></textarea>
<button type="submit">检测兼容性</button>
</form>
<?php if (isset($processedText)): ?>
<div class="result">
<?php if ($hasNonCompatibleChars): ?>
<div class="warning">⚠️ 发现无法在 MySQL utf8 中存储的字符(标红部分):</div>
<?php else: ?>
<div class="success">✓ 所有字符均可在 MySQL utf8 中正常存储</div>
<?php endif; ?>
<?= $processedText ?>
</div>
<?php endif; ?>
<div class="info">
<p><strong>说明:</strong>MySQL 的 utf8 编码最多支持 3 字节的 UTF-8 字符(U+0000 至 U+FFFF)。</p>
<p>标红的字符需要 4 字节编码(如 Emoji、某些生僻字),需使用 utf8mb4 编码才能正确存储。</p>
</div>
</div>
</body>
</html>
效果图:


浙公网安备 33010602011771号