我构建了一个脚本,它应该为我的项目生成一个站点地图。
此脚本使用strtr()替换不需要的符号,并转换德语umlauts。
$ers = array( '<' => '', '>' => '', ' ' => '-', 'Ä' => 'Ae', 'Ö' => 'Oe', 'Ü' => 'Ue', 'ä' => 'ae', 'ö' => 'oe', 'ü' => 'ue', 'ß' => 'ss', '&' => 'und', '*' => '', ' - ' => '-', ',' => '', '.' => '', '!' => '', '?' => '' );
foreach ($rs_post as $row) {
$kategorie = $row['category'];
$kategorie = strtr($kategorie,$ers);
$kategorie = strtolower($kategorie);
$kategorie = trim($kategorie);
$org_file .= "<url><loc>https://domain.org/kategorie/" . $kategorie . "/</loc><lastmod>2016-08-18T19:02:42+00:00</lastmod><changefreq>monthly</changefreq><priority>0.2</priority></url>" . PHP_EOL;
}像"<“这样的不想要的标志将被正确地替换,但是德国货币不会被转换。我不知道为什么。
有人给我带了个tipp?
托尔斯顿
发布于 2016-08-19 18:57:51
正如其他人所指出的,最有可能的原因是字符编码不匹配。由于您要转换的标题显然是在UTF-8中,所以问题很可能不是PHP源代码。请尝试将文件重新保存为UTF-8文本,看看这是否解决了问题。
顺便说一下,调试它的一个简单方法是使用例如print_r()或var_dump()将数据行和音译数组输出到同一个输出文件中,然后查看输出,看看其中的非ASCII字符是否正确。如果字符在数据中看起来是对的,但在音译表中看起来却是错误的(反之亦然),那就是编码不匹配的迹象。
Ps。如果已经安装了PHP扩展(可能已经安装了艾特夫扩展),那么可以考虑使用艾特夫
发布于 2016-08-19 12:43:58
查一下字符集。如果发送表单页使用:
<meta charset="utf-8"> 都不管用。
尝试使用另一种编码方式,如
<meta charset="ISO-8859-1">下面是测试替换数组的小示例代码:
<!DOCTYPE html>
<html>
<?php
if(isset($_POST["txt"]))
{
echo '<head><meta charset="ISO-8859-1"></head><body>';
$posted = $_POST["txt"];
echo 'Received raw: ' . $posted .'<br/>';
echo 'Received: ' . htmlspecialchars($posted).'<br/>';;
$ers = array( '<' => '', '>' => '', ' ' => '-', 'Ä' => 'Ae', 'Ö' => 'Oe', 'Ü' => 'Ue', 'ä' => 'ae', 'ö' => 'oe', 'ü' => 'ue', 'ß' => 'ss', '&' => 'und', '*' => '', ' - ' => '-', ',' => '', '.' => '', '!' => '', '?' => '' );
$replaced = strtr($posted,$ers);
echo 'Replaced: ' . $replaced .'<br/>';
}
else {
?>
<head>
<!--<meta charset="utf-8">--> <!--THIS ENCODING WILL NOT WORK -->
<meta charset="ISO-8859-1"> <!--THIS WORKS FINE -->
</head>
<body>
<p>the text you want to replace here</p>
<form action="#" method="post">
Text: <input type="text" name="txt" value="">
<input type="submit" value="Submit">
</form>
<?php
}
?>
</body>
</html>https://stackoverflow.com/questions/39036429
复制相似问题