php - How to repeat a string while using a floating point number as multiplier -
i had repeat string n-times match length of string. problem multiplier floating point number , str_repeat accepts integer.
to more precisely had string of lowercase , uppercase letters of alphabet (strlen: 52) , had match length of string containing numbers 0-9 (strlen: 10) against it.
since did research before , not find answer looking going share function q&a-style.
to multiply string n-times floating point number multiplier need basic maths. here came with:
function str_repeat_float($str, $m){ $f = floor($m); $d = $m - $f; $l = strlen($str); $p = floor($d * $l); $r_str = str_repeat($str, $f); $s_str = substr($str, 0, $p); return $r_str.$s_str; } echo str_repeat_float('1234567890', 5.2); // 1234567890123456789012345678901234567890123456789012
Comments
Post a Comment