1

Is there a built in function that can calculate the distance between 2 chars? for example:

if i want to calculate the distance between a and c the result should be 1 because there is one letter in between a and c.

I did a function to achieve that, but i need to know if there is a built in solution or a simpler one

function str_distance($a, $b){
    $cant = 0;
    while(true){
        if (++$a == $b)
            return $cant;
        $cant++;
    }
}

But this wont work when calculating z to b or any calculations that gets to the end of the alphabet.

Any suggestions?

  • I don't think there is a buildin function to do this, I would probably go and use `ord` to get he ascii value (potentially lowercase a and b before if that doesn't matter) then substract the two ord values but that depends on what exactly you need especially for z and b... – ChristianM Mar 13 '18 at 18:56

6 Answers6

3
$firstChar = 'a';
$secondChar = 'z';

$distance = abs( ord($firstChar) - ord($secondChar) );

are you looking for something like this? ord returns ascii code for character, we use abs to compensate for negative distance, or you can keep sign of this operation to know which way they differ (if first is bigger or smaller than second), depends what you want to achieve

EDIT

function letterDistance($a, $b){
    $alphLetterCount = ord('z') - ord('a');

    $distance = ord(strtolower($b)) - ord(strtolower($a));

    return ($distance < 0 ? $alphLetterCount + $distance : $distance-1);
}

this function works exactly as OP wanted to - distance from 'z' to 'b' is 1 and from 'b' to 'z' is 23, it's also case insensitive

bestestefan
  • 852
  • 6
  • 20
1

Simply use ord() function in php and get the int value of char. use it like this `

function str_distance($a, $b){
    return ord($b) - ord($a);
}

echo str_distance('a', 'c') - 1;

`

Achintha
  • 176
  • 1
  • 1
  • 8
1

OK, this might seem a little bit stupid, but I'm bored:

return count(array_diff(range($a, $b), [$a, $b]));
  • Create a range from $a to $b
  • Get the difference between the range and the 2 characters $a and $b
  • Count the difference
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
1
function Letter_Distance($x,$y){
    $L1 = strtolower($x);
    $L2 = strtolower($y);
    $alphabet = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
    $pos1 = array_search($L1, $alphabet);
    $pos2 = array_search($L2, $alphabet);
    return abs(($pos1 - $pos2)) - 1;
}

echo Letter_Distance("a","c");

1

Hope this helps, feel free to tweak it.

Richard
  • 325
  • 7
  • 23
1

I'd like the function prefer to return with - sign when going from Z to A and also takes care of lowercasing

private function str_distance($start,$finish)
{
   return ord(lcfirst($finish)) - ord(lcfirst($start));
}
   //str_distance('a','z') => 25
   //str_distance('Z','A') => -25
Mahdi Younesi
  • 6,889
  • 2
  • 20
  • 51
1

Recursive version:

function str_distance($a, $b, $c = 0) {
    if ($a > $b) return str_distance($b, $a, $c);
    if (++$a >= $b) return $c;
    return str_distance($a, $b, ++$c);
}
Don't Panic
  • 41,125
  • 10
  • 61
  • 80