- Kód: Vybrat vše
function porovnat_str($string1,$string2){
$por1 = explode(",",$string1);
$por2 = explode(",",$string2);
$shody = 0;
for($x = 0; $x<count($por1); $x++){
for($y = 0; $y<count($por2); $y++){
if ($por1[$x]==$por2[$y]) $shody += 1;
}
}
return $shody;
}
Teď bych ale potřeboval porovnat ne jakékoliv hodnoty, ale pouze odzadu. Jako třeba když u Sportky sázíte Šanci, tak podle toho, kolik odzadu "uhodnete" čísel, tak je pak výhra.
Pokud budu mít string1 "5,6,7" a string2 "7,7,7", tak by mi měla funkce vrátit počet shodných čísel jedno. Původní funkce by mi vrátila 3.
Máte nějaký nápad, jak na to?
-- 16. 8. 2018 10:35 --
Tak jsem něco vymyslel, ale funguje to jen za předpokladu, že jsou oba řetězce stejně dlouhé. Co s tím? Díky
- Kód: Vybrat vše
function porovnat_str2($string1,$string2){
$por1 = explode(",",$string1);
$por2 = explode(",",$string2);
$shody = 0;
for($x = count($por2)-1; $x>0; $x--){
if ($por1[$x]==$por2[$x])
$shody += 1;
else
break;
}
return $shody;
}
-- 16. 8. 2018 10:52 --
Vyřešeno, nějak rychle dneska
- Kód: Vybrat vše
function porovnat_str2($string1,$string2){
$string1b = $string1; $string2b = $string2;
if (strlen($string1)>strlen($string2))
$string1b = substr($string1,strlen($string1)-strlen($string2),strlen($string2));
if (strlen($string2)>strlen($string1))
$string2b = substr($string2,strlen($string2)-strlen($string1),strlen($string1));
$por1 = explode(",",$string1b);
$por2 = explode(",",$string2b);
$shody = 0;
for($x = count($por2); $x>0; $x--){
if ($por1[$x-1]==$por2[$x-1])
$shody += 1;
else
break;
}
return $shody;