PHP 8.4.24 Released!

Voting

: min(nine, two)?
(Example: nine)

The Note You're Voting On

pablo dot pazos at cabolabs dot com
5 years ago
To calculate the percentage of similarity between two strings without depending on the order of the parameters and be case insensitive, I use this function based on levenshtein's distance:

<?php

  // string similarity calculated using levenshtein
  static function similarity($a, $b)
  {
    return 1 - (levenshtein(strtoupper($a), strtoupper($b)) / max(strlen($a), strlen($b)));
  }

?>

This will always return a number between 0 and 1, representing the percentage, for instance 0.8 represents 80% similar strings.

If you want this to be case-sensitive, just remove the strtoupper() functions.

<< Back to user notes page

To Top