I wanted this functionality without having to install the extension.
So here's a script I wrote to find out the greatest common denominator:
<?php
// Our fraction, 3/12, could be written better
$numerator = 3;
$denominator = 12;
/**
* @param int $num
* @return array The common factors of $num
*/
function getFactors($num)
{
$factors = [];
// get factors of the numerator
for ($x = 1; $x <= $num; $x ++) {
if ($num % $x == 0) {
$factors[] = $x;
}
}
return $factors;
}
/**
* @param int $x
* @param int $y
*/
function getGreatestCommonDenominator($x, $y)
{
// first get the common denominators of both numerator and denominator
$factorsX = getFactors($x);
$factorsY = getFactors($y);
// common denominators will be in both arrays, so get the intersect
$commonDenominators = array_intersect($factorsX, $factorsY);
// greatest common denominator is the highest number (last in the array)
$gcd = array_pop($commonDenominators);
return $gcd;
}
// divide the numerator and denomiator by the gcd to get our refactored fraction
$gcd = getGreatestCommonDenominator($numerator, $denominator);
echo ($numerator / $gcd) .'/'. ($denominator / $gcd); // we can use divide (/) because we know result is an int :-)
Which you can see running here https://3v4l.org/uTucY