提问人:Hejya 提问时间:9/16/2023 最后编辑:DharmanHejya 更新时间:9/17/2023 访问量:18
预输入无法识别重音
Typeahead not recognizing accents
问:
当人们在我的网站上使用表单时,我正在使用预输入。
人们输入城市名称的前几个字母,然后我返回可能的匹配结果。我在数据库中有城市名称。
问题是,例如,当城市以重音“é”开头时,我没有得到任何结果。我应该得到结果,因为我的数据库中有以“é”开头的城市名称。 下面是一个示例:
不工作“élan”应返回“élancourt”。如果我写“elan”,它也不起作用,因为在我的数据库中,它可能是“élancourt”。
如果单词中有重音,则它起作用,只有当单词以重音开头时,它才会失败。提前打字工作
所以这是提前输入的代码:
<script>
$(document).ready(function () {
$('#code_postal_ville').typeahead({
minLength: 3,
highlight: true,
},
{
name: 'villes',
source: function (query, syncResults, asyncResults) {s
var tableName = 'villes_france_free';
console.log('Table Name:', tableName);
$.ajax({
url: '<?php echo get_template_directory_uri(); ?>/get_cities.php',
method: 'GET',
data: { query: query, table: tableName },
dataType: 'json',
success: function (data) {
var uniqueCities = new Set();
data.suggestions.forEach(function (item) {
if (item.toLowerCase().indexOf(query.toLowerCase()) === 0) {
uniqueCities.add(item);
}
});
var filteredData = Array.from(uniqueCities);
asyncResults(filteredData);
}
});
}
});
});
</script>
这是“get_cities.php”文件
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test_leo";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (isset($_GET['query']) && isset($_GET['table'])) {
$query = $_GET['query'];
$table = $_GET['table'];
$sql = "SELECT ville_nom_reel FROM $table WHERE ville_nom_reel LIKE ?";
$stmt = $conn->prepare($sql);
$param = "%" . $query . "%";
$stmt->bind_param("s", $param);
$stmt->execute();
$result = $stmt->get_result();
$cities = array();
while ($row = $result->fetch_assoc()) {
$cities[] = $row['ville_nom_reel'];
}
$uniqueCities = array_unique($cities);
$filteredCities = array_filter($uniqueCities, function($item) use ($query) {
return stripos($item, $query) === 0;
});
$response = new stdClass();
$response->suggestions = array_values($filteredCities);
echo json_encode($response);
}
$conn->close();
?>
我尝试了像 unidecode 或变音符号这样的库。但我可能用错了它们,因为它不起作用。
答: 暂无答案
评论