我知道这个问题已经有答案了,但我遇到了同样的问题,并通过修复表中的字符集来解决它,以便未来输入数据。
我正在使用 SQL Server 2017,排序规则设置为SQL_Latin1_General_CP1_CI_AS
对于现有字符,我编写了一个脚本从数据库中提取数据并搜索匹配的每个字符,并修复这些字符。
我创建了两个 CSV 文件,一个包含带有杂乱字符的数据(email_templates.csv),另一个包含干净的 HTML 模板(clean_templates.csv)。
要查找文本/HTML 中的杂乱字符,您也可以使用此在线工具,这非常有帮助。
https://freetools.textmagic.com/unicode-detector
UTF-8编码调试表
https://www.i18nqa.com/debug/utf8-debug.html
HTML代码和HTML特殊字符
https://psdtowp.net/html-codes-special-characters.html
为了在VS Code中验证相同的内容,我使用了扩展程序
Render Special Characters
,以下是链接。
https://marketplace.visualstudio.com/items?itemName=miku3920.vscode-render-special-chars
PHP脚本以验证和修复问题
$fileName = "email_templates.csv";
$ofileName = "clean_templates.csv";
try {
$stray_chars = array(
'—' => '-',
'–' => '-',
'‘'=> '\'',
'’' => '\'',
'“' => '"' ,
'â€' => '"',
'Â'=>'',
'ó'=> "ó",
"ñ" => "ñ",
"Ã" => "í",
"á"=> "á",
"" => '',
);
$contents = getEmailTemplateContent();
$handle = fopen($fileName, "w") or die('Unable to open file');
$ohandle = fopen($ofileName, "w") or die('Unable to open file');
$data = [];
$cleaned = [];
$i = 0;
$html = '';
foreach($contents as $content) {
$html = $content['html'];
$clean = str_replace(array_keys($stray_chars), array_values($stray_chars), $html);
$cleaned[$i] = $content['id']."-xxxx-".$clean;
$data[$i] = $content['id']."-xxxx-".$html;
updateEmailTemplateContent($clean, $content['id']);
$i++;
}
fputcsv($handle, $data);
fputcsv($ohandle, $cleaned);
fclose($handle);
fclose($ohandle);
}
catch(\PDOException $e) {
$jobStatus = 'E';
$jobError = $e->getMessage();
}