Skip to content

Instantly share code, notes, and snippets.

@alextraza
Created September 28, 2017 17:28
Show Gist options
  • Select an option

  • Save alextraza/2b57af6043cf3d06c11ec2a777d810eb to your computer and use it in GitHub Desktop.

Select an option

Save alextraza/2b57af6043cf3d06c11ec2a777d810eb to your computer and use it in GitHub Desktop.
Создает slug для opencart
<?php
include 'config.php';
$mysqli = new mysqli('DB_HOSTNAME', 'DB_USERNAME', 'DB_PASSWORD', 'DB_DATABASE');
$sqlChar = 'utf8';
if ($mysqli->connect_errno) {
echo "Извините, возникла проблема на сайте";
exit;
}
mysqli_set_charset($mysqli, $sqlChar);
$sql = "SELECT * FROM " . DB_PREFIX . "product_description where language_id = 3";
if (!$result = $mysqli->query($sql)) {
echo "Извините, возникла проблема в работе сайта.";
exit;
}
while($products = $result->fetch_assoc()) {
$product_id = 'product_id='.$products['product_id'];
$sql = "SELECT * FROM ". DB_PREFIX ."cp_url_alias WHERE query = '{$product_id}'";
$alias = $mysqli->query($sql);
if ($alias->num_rows === 0) {
$name = slugify($products['name']);
$sql = "INSERT INTO " . DB_PREFIX . "url_alias (query, keyword) VALUES ('{$product_id}', '{$name}')";
$mysqli->query($sql);
}
echo "Теперь необходимо почистить кеш";
}
function slugify($text)
{
// replace non letter or digits by -
$text = preg_replace('~[^\pL\d]+~u', '-', $text);
// transliterate
$text = translit($text);
// remove unwanted characters
$text = preg_replace('~[^-\w]+~', '', $text);
// trim
$text = trim($text, '-');
// remove duplicate -
$text = preg_replace('~-+~', '-', $text);
// lowercase
$text = strtolower($text);
if (empty($text)) {
return 'n-a';
}
return $text;
}
function translit($str){
$translit=array(
"А"=>"a","Б"=>"b","В"=>"v","Г"=>"g","Д"=>"d","Е"=>"e","Ё"=>"e","Ж"=>"zh","З"=>"z","И"=>"y", "І"=>'i', "Ї"=>'ї', "Й"=>"j","К"=>"k","Л"=>"l","М"=>"m","Н"=>"n","О"=>"o","П"=>"p","Р"=>"r","С"=>"s","Т"=>"t","У"=>"u","Ф"=>"f","Х"=>"h","Ц"=>"ts","Ч"=>"ch","Ш"=>"sh","Щ"=>"shch","Ъ"=>"","Ы"=>"y","Ь"=>"","Э"=>"e","Ю"=>"yu","Я"=>"ya",
"а"=>"a","б"=>"b","в"=>"v","г"=>"g","д"=>"d","е"=>"e","ё"=>"e","ж"=>"zh","з"=>"z","и"=>"y", "і"=>'i', "ї"=>'i', "й"=>"j","к"=>"k","л"=>"l","м"=>"m","н"=>"n","о"=>"o","п"=>"p","р"=>"r","с"=>"s","т"=>"t","у"=>"u","ф"=>"f","х"=>"h","ц"=>"ts","ч"=>"ch","ш"=>"sh","щ"=>"shch","ъ"=>"","ы"=>"y","ь"=>"","э"=>"e","ю"=>"yu","я"=>"ya",
"A"=>"a","B"=>"b","C"=>"c","D"=>"d","E"=>"e","F"=>"f","G"=>"g","H"=>"h","I"=>"i","J"=>"j","K"=>"k","L"=>"l","M"=>"m","N"=>"n","O"=>"o","P"=>"p","Q"=>"q","R"=>"r","S"=>"s","T"=>"t","U"=>"u","V"=>"v","W"=>"w","X"=>"x","Y"=>"y","Z"=>"z"
);
$result=strtr($str,$translit);
$result=preg_replace("/[^a-zA-Z0-9_]/i","-",$result);
$result=preg_replace("/\-+/i","-",$result);
$result=preg_replace("/(^\-)|(\-$)/i","",$result);
return $result;
}
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment