Last active
November 1, 2021 08:17
-
-
Save gundamew/61a8f019987c30726612bab1ca4dcff1 to your computer and use it in GitHub Desktop.
Simple PHP helper functions to generate five-star rating stars.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Example: five_star_rating_stars(4.8); | |
* five_star_rating_stars( | |
* 4.8, | |
* '<i class="fas fa-star"></i>', | |
* '<i class="fas fa-star-half-alt"></i>', | |
* '<i class="far fa-star"></i>' | |
* ); | |
*/ | |
if (! function_exists('five_star_rating_stars')) { | |
function five_star_rating_stars(float $raw_rating, $filled_star = '★', $half_star = '⯪', $empty_star = '☆') | |
{ | |
$highest_rating = 5; | |
if ($raw_rating < 0 || $raw_rating > $highest_rating) { | |
return false; | |
} | |
// map rating scores to *.5 or *.0 | |
// see https://stackoverflow.com/a/19050089/10468523 | |
$rating = round($raw_rating * 2) / 2; | |
// https://stackoverflow.com/a/6619392/10468523 | |
$whole = (int) floor($rating); | |
// append filled star(s) | |
$all_five_stars = str_repeat($filled_star, $whole); | |
if ($whole === $highest_rating) { | |
return $all_five_stars; | |
} | |
$fraction = $rating - $whole; | |
$counter = $highest_rating - $whole; | |
// append half star | |
if ($fraction === 0.5) { | |
$all_five_stars .= $half_star; | |
--$counter; | |
} | |
// append empty star(s) | |
while ($counter > 0) { | |
$all_five_stars .= $empty_star; | |
--$counter; | |
} | |
return $all_five_stars; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Example: $rating = mround(4.8, 0.5); | |
* five_star_rating_stars($rating); | |
*/ | |
if (! function_exists('same_sign_float')) { | |
function same_sign_float(float $x, float $y) | |
{ | |
return ($x > 0 && $y > 0) || ($x < 0 && $y < 0); | |
} | |
} | |
// Try to create a function equivalent to Microsoft Excel MROUND function | |
// https://support.microsoft.com/en-us/office/mround-function-c299c3b0-15a5-426d-aa4b-d2d5b3baf427 | |
if (! function_exists('mround')) { | |
function mround(float $number, float $multiple) | |
{ | |
if (! same_sign_float($number, $multiple)) { | |
return false; | |
} | |
// https://stackoverflow.com/a/48643210/10468523 | |
return round($number / $multiple, 0, PHP_ROUND_HALF_UP) * $multiple; | |
} | |
} | |
if (! function_exists('five_star_rating_stars')) { | |
function five_star_rating_stars(float $rating, $full_star_sign = '★', $half_star_sign = '⯪', $empty_star_sign = '☆') { | |
$highest_rating = 5; | |
if ($rating < 0 || $rating > $highest_rating) { | |
return false; | |
} | |
// https://developer.wordpress.org/reference/functions/wp_star_rating/ | |
$full_star_number = floor($rating); | |
$half_star_number = ceil($rating - $full_star_number); | |
$empty_star_number = $highest_rating - $full_star_number - $half_star_number; | |
$stars = str_repeat($full_star_sign, $full_star_number); | |
$stars .= str_repeat($half_star_sign, $half_star_number); | |
$stars .= str_repeat($empty_star_sign, $empty_star_number); | |
return $stars; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment