Answer the question
In order to leave comments, you need to log in
strtr conversion not working?
<?php
class UpdateForSearch
{
public function connect_db()
{
$params = parse_ini_file('../config.ini');
if (!is_array($params)) {
throw new Exception("Error #1");
} else {
$db = new PDO($params['db.conn'], $params['db.user'], $params['db.pass']);
$db->setAttribute (PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
return $db;
}
function encodestring($st)
{
// Сначала заменяем "односимвольные" фонемы.
$st=strtr($st,"абвгдеёзийклмнопрстуфхъыэ_",
"abvgdeeziyklmnoprstufh\'iei");
$st=strtr($st,"АБВГДЕЁЗИЙКЛМНОПРСТУФХЪЫЭ_",
"ABVGDEEZIYKLMNOPRSTUFH'IEI");
// Затем - "многосимвольные".
$st=strtr($st,
array(
"ж"=>"zh", "ц"=>"ts", "ч"=>"ch", "ш"=>"sh",
"щ"=>"shch","ь"=>"", "ю"=>"yu", "я"=>"ya",
"Ж"=>"ZH", "Ц"=>"TS", "Ч"=>"CH", "Ш"=>"SH",
"Щ"=>"SHCH","Ь"=>"", "Ю"=>"YU", "Я"=>"YA",
"ї"=>"i", "Ї"=>"Yi", "є"=>"ie", "Є"=>"Ye"
)
);
// Возвращаем результат.
return $st;
}
function str_to_bred($st)
{
// Сначала заменяем "односимвольные" фонемы.
$st=strtr($st,"абвгдеёзийклмнопрстуфхъыэ_",
"f,dult`pbqrkvyjghcnea[}s'");
$st=strtr($st,"АБВГДЕЁЗИЙКЛМНОПРСТУФХЪЫЭ_",
"F,DULT`PBQRKVYJGHCNEA[}S'");
return $st;
}
function getNameAndDesc($id_product){
$db = $this->connect_db();
$name = "select name,description from ps_product_lang where id_lang = 1 and id_product = ?";
$stmt = $db->prepare($name);
$stmt->execute(array((int)$id_product));
$result = $stmt->fetch(PDO::FETCH_NUM);
return $result;
}
function updateProduct($id_product){
$nd = $this->getNameAndDesc($id_product);
if(!empty($nd)){
$fragment = explode(" ",$nd[0]);
foreach ($fragment as $item) {
$first_rest[] = $this->encodestring($item);
$first_rest[] = $this->str_to_bred($item);
}
print_r($first_rest);
}
}
}
$cl = new UpdateForSearch();
$cl->updateProduct(68);
Answer the question
In order to leave comments, you need to log in
You most likely have UTF-8 encoding, so use mb-strstr instead of strtr
and if you need a normal SLUG, then everything is up to you - https://github.com/2amigos/transliteration-helper
Database encoding problem.
$link = mysql_connect('localhost', 'user', 'password');
mysql_set_charset('utf8',$link);
As it says in the documentation of the strtr function php.net/manual/en/function.strtr.php :
That is, for a multibyte encoding, instead of the construction:
..., you should use the construction:
$st=strtr($st,
array(
"а" => "a",
"б" => "b",
"в" => "v",
)
);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question