M
M
msimrial2015-11-10 11:49:41
PHP
msimrial, 2015-11-10 11:49:41

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);

Outputs:
Array ( [0] => i�ilihm�ibm� [1] => '�'k'[v�',v� [2] => Sand [3] => Sand [4] => iem �i'm�m�isieism�ibieisi�i�m�iu [5] => '�v�'sv�v�'c't'cv�','�'c'�'�v�' e [6] => 1 [7] => 1 [8] => i' [9] => 's
) is it possible otherwise?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Andrew, 2015-11-10
@msimrial

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

I
inDeepCode, 2015-11-10
@inDeepCode

Database encoding problem.

$link = mysql_connect('localhost', 'user', 'password');
mysql_set_charset('utf8',$link);

W
WhiteSEOMagic, 2017-11-10
@WhiteSEOMagic

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",
  )
);

... despite the fact that we are replacing only one character at a time - after all, these characters are multibyte.
mb_strtr
Скорее всего именно поэтому функция mb_strtr отсутствует, так как в ней нет необходимости, достаточно использовать strtr. Хотя с точки зрения юзабилити удобнее было бы писать mb_ для многобайтовых кодировок, так привычнее.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question