Раскладка клавиатуры
- 4 ответа
- 0 вопросов
0
Вклад в тег
<?php
$layouts['en'] = preg_split('!!u', 'qwertyuiop[]asdfghjkl;\'\zxcvbnm,./', null, PREG_SPLIT_NO_EMPTY);
$layouts['ru'] = preg_split('!!u', 'йцукенгшщзхъфывапролджэ\ячсмитьбю.', null, PREG_SPLIT_NO_EMPTY);
$phrase = "тестовая cnhjrf с неверной раскладкой\n";
echo $phrase;
$phrase_chunks = explode(' ', $phrase);
$ch = array();
foreach ($phrase_chunks as $phrase_chunk) $curl_handles[] = curl_init();
foreach ($curl_handles as $index => $curl_handle) {
curl_setopt($curl_handle, CURLOPT_URL, "http://ajax.googleapis.com/ajax/services/language/detect?v=1.0&q=".urlencode($phrase_chunks[$index]));
curl_setopt($curl_handle, CURLOPT_HEADER, false);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
}
$mh = curl_multi_init();
foreach ($curl_handles as $index => $curl_handle) {
curl_multi_add_handle($mh, $curl_handle);
}
$active = null;
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
while ($active && $mrc == CURLM_OK) {
if (curl_multi_select($mh) != -1) {
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
}
foreach ($curl_handles as $index => $curl_handle) {
$responses[] = json_decode(curl_multi_getcontent($curl_handle));
}
foreach ($curl_handles as $index => $curl_handle) {
curl_multi_remove_handle($mh, $curl_handle);
}
curl_multi_close($mh);
foreach ($responses as $index => $response) {
if ($response->responseData->confidence <= 0.005 && $response->responseData->language != 'ru') {
$phrase_chunks[$index] = str_replace($layouts['en'], $layouts['ru'], $phrase_chunks[$index]);
}
}
echo implode(' ', $phrase_chunks)."\n";
?>