Преобразование слова, взависимости от числа
из базы данных бертся число статей
она выводит:
56 статей
52 статьи
51 статья
то есть меняет слово взависимости от числа, только чем больше статей, тем больше приходится функцию делать:
Код:
<?php
function preobrazovatel($num, $one, $ed, $mn)
{
# $one="статья";
# $ed="статьи";
# $mn="статей";
if($num===""){
print"";
}
if(($num=="0") or
($num >= "5") and ($num <= "20")or
($num >= "25") and ($num <= "30")or
($num >= "35") and ($num <= "40")or
($num >= "45") and ($num <= "50")or
($num >= "55") and ($num <= "60")or
($num >= "65") and ($num <= "70")or
($num >= "75") and ($num <= "80")or
($num >= "85") and ($num <= "90")or
($num >= "95") and ($num <= "100")or
($num >= "105") and ($num <= "120")or
($num >= "125") and ($num <= "130")or
($num >= "135") and ($num <= "140")or
($num >= "145") and ($num <= "150")or
($num >= "155") and ($num <= "160")or
($num >= "165") and ($num <= "170")or
($num >= "175") and ($num <= "180")or
($num >= "185") and ($num <= "190")or
($num >= "195") and ($num <= "200")or
($num >= "205") and ($num <= "220")or
($num >= "225") and ($num <= "230")or
($num >= "235") and ($num <= "240")or
($num >= "245") and ($num <= "250")or
($num >= "255") and ($num <= "260")or
($num >= "265") and ($num <= "270")or
($num >= "275") and ($num <= "280")or
($num >= "285") and ($num <= "290")or
($num >= "295") and ($num <= "300")
)
{
print"$num $mn";
}
if(($num=="1")
or ($num == "21")
or ($num == "31")
or ($num == "41")
or ($num == "51")
or ($num == "61")
or ($num == "71")
or ($num == "81")
or ($num == "91")
or ($num == "101")
or ($num == "121")
or ($num == "131")
or ($num == "141")
or ($num == "151")
or ($num == "161")
or ($num == "171")
or ($num == "181")
or ($num == "191")
or ($num == "201")
or ($num == "221")
or ($num == "231")
or ($num == "241")
or ($num == "251")
or ($num == "261")
or ($num == "271")
or ($num == "281")
or ($num == "291")
)
{
print"$num $one";
}
if(($num >= "2") and ($num <= "4")
or ($num >= "22") and ($num <= "24")
or ($num >= "32") and ($num <= "34")
or ($num >= "42") and ($num <= "44")
or ($num >= "52") and ($num <= "54")
or ($num >= "62") and ($num <= "64")
or ($num >= "72") and ($num <= "74")
or ($num >= "82") and ($num <= "84")
or ($num >= "92") and ($num <= "94")
or ($num >= "102") and ($num <= "104")
or ($num >= "122") and ($num <= "124")
or ($num >= "132") and ($num <= "134")
or ($num >= "142") and ($num <= "144")
or ($num >= "152") and ($num <= "154")
or ($num >= "162") and ($num <= "164")
or ($num >= "172") and ($num <= "174")
or ($num >= "182") and ($num <= "184")
or ($num >= "192") and ($num <= "194")
or ($num >= "202") and ($num <= "204")
or ($num >= "222") and ($num <= "224")
or ($num >= "232") and ($num <= "234")
or ($num >= "242") and ($num <= "244")
or ($num >= "252") and ($num <= "254")
or ($num >= "262") and ($num <= "264")
or ($num >= "272") and ($num <= "274")
or ($num >= "282") and ($num <= "284")
or ($num >= "292") and ($num <= "294")
)
{
print"$num $ed";
}
}
?>
function preobrazovatel($num, $one, $ed, $mn)
{
# $one="статья";
# $ed="статьи";
# $mn="статей";
if($num===""){
print"";
}
if(($num=="0") or
($num >= "5") and ($num <= "20")or
($num >= "25") and ($num <= "30")or
($num >= "35") and ($num <= "40")or
($num >= "45") and ($num <= "50")or
($num >= "55") and ($num <= "60")or
($num >= "65") and ($num <= "70")or
($num >= "75") and ($num <= "80")or
($num >= "85") and ($num <= "90")or
($num >= "95") and ($num <= "100")or
($num >= "105") and ($num <= "120")or
($num >= "125") and ($num <= "130")or
($num >= "135") and ($num <= "140")or
($num >= "145") and ($num <= "150")or
($num >= "155") and ($num <= "160")or
($num >= "165") and ($num <= "170")or
($num >= "175") and ($num <= "180")or
($num >= "185") and ($num <= "190")or
($num >= "195") and ($num <= "200")or
($num >= "205") and ($num <= "220")or
($num >= "225") and ($num <= "230")or
($num >= "235") and ($num <= "240")or
($num >= "245") and ($num <= "250")or
($num >= "255") and ($num <= "260")or
($num >= "265") and ($num <= "270")or
($num >= "275") and ($num <= "280")or
($num >= "285") and ($num <= "290")or
($num >= "295") and ($num <= "300")
)
{
print"$num $mn";
}
if(($num=="1")
or ($num == "21")
or ($num == "31")
or ($num == "41")
or ($num == "51")
or ($num == "61")
or ($num == "71")
or ($num == "81")
or ($num == "91")
or ($num == "101")
or ($num == "121")
or ($num == "131")
or ($num == "141")
or ($num == "151")
or ($num == "161")
or ($num == "171")
or ($num == "181")
or ($num == "191")
or ($num == "201")
or ($num == "221")
or ($num == "231")
or ($num == "241")
or ($num == "251")
or ($num == "261")
or ($num == "271")
or ($num == "281")
or ($num == "291")
)
{
print"$num $one";
}
if(($num >= "2") and ($num <= "4")
or ($num >= "22") and ($num <= "24")
or ($num >= "32") and ($num <= "34")
or ($num >= "42") and ($num <= "44")
or ($num >= "52") and ($num <= "54")
or ($num >= "62") and ($num <= "64")
or ($num >= "72") and ($num <= "74")
or ($num >= "82") and ($num <= "84")
or ($num >= "92") and ($num <= "94")
or ($num >= "102") and ($num <= "104")
or ($num >= "122") and ($num <= "124")
or ($num >= "132") and ($num <= "134")
or ($num >= "142") and ($num <= "144")
or ($num >= "152") and ($num <= "154")
or ($num >= "162") and ($num <= "164")
or ($num >= "172") and ($num <= "174")
or ($num >= "182") and ($num <= "184")
or ($num >= "192") and ($num <= "194")
or ($num >= "202") and ($num <= "204")
or ($num >= "222") and ($num <= "224")
or ($num >= "232") and ($num <= "234")
or ($num >= "242") and ($num <= "244")
or ($num >= "252") and ($num <= "254")
or ($num >= "262") and ($num <= "264")
or ($num >= "272") and ($num <= "274")
or ($num >= "282") and ($num <= "284")
or ($num >= "292") and ($num <= "294")
)
{
print"$num $ed";
}
}
?>
Как уменьшить код функции, и сделать, чтоб она моглать роботать хоть с числом 1565
Код:
$word="раз";
if ($count>1 && $count<5) $word="раза";
if ($count>20) {
$sn=substr($count,-1);
if ($sn>1 && $sn<5) $word="раза";
}
if ($count>1 && $count<5) $word="раза";
if ($count>20) {
$sn=substr($count,-1);
if ($sn>1 && $sn<5) $word="раза";
}
Код:
if(($num%10>="5")and($num/10%10!="1"))print"$num $mn";
else if($num%10=="1")print"$num $one";
else if(($num%10>="2")and($num/10%10!="4"))print"$num $ed";
else if($num%10=="1")print"$num $one";
else if(($num%10>="2")and($num/10%10!="4"))print"$num $ed";
И два (на счет синтаксиси switch - не уверен, писал по аналогии с си++)
Код:
if($num/10%10!="1")
{
switch($num%10)
{
case "1" : print"$num $one"; break;
case "2" :
case "3" :
case "4" :
case "5" : print"$num $ed"; break;
case "6" :
case "7" :
case "8" :
case "9" :
case "0" : print"$num $mn"; break;
}
}
{
switch($num%10)
{
case "1" : print"$num $one"; break;
case "2" :
case "3" :
case "4" :
case "5" : print"$num $ed"; break;
case "6" :
case "7" :
case "8" :
case "9" :
case "0" : print"$num $mn"; break;
}
}
Код:
<?php
function preobrazovatel($num, $one, $ed, $mn)
{
$last = substr($num, strlen($num)-1);
if(($num>="10") and ($num<="20") )
{
print"$num $mn";
}
else if(
($last=="0") or
($last == "5") or
($last == "6") or
($last == "7") or
($last == "8") or
($last == "9"))
{
print"$num $mn";
}
else if($last=="1")
{
print"$num $one";
}
else if(
($last == "2") or
($last == "3") or
($last == "4"))
{
print"$num $ed";
}
}
?>
function preobrazovatel($num, $one, $ed, $mn)
{
$last = substr($num, strlen($num)-1);
if(($num>="10") and ($num<="20") )
{
print"$num $mn";
}
else if(
($last=="0") or
($last == "5") or
($last == "6") or
($last == "7") or
($last == "8") or
($last == "9"))
{
print"$num $mn";
}
else if($last=="1")
{
print"$num $one";
}
else if(
($last == "2") or
($last == "3") or
($last == "4"))
{
print"$num $ed";
}
}
?>
Проверить её можно так:
Код:
<?php
while($t < "10000")
{
print preobrazovatel($t, Статья, Статьи, Статей);
print"
";
$t++;
}
?>
while($t < "10000")
{
print preobrazovatel($t, Статья, Статьи, Статей);
print"
";
$t++;
}
?>