PHP Classes

PHP Similarity Between Two Strings: Calculate the similarity level of two text strings

Recommend this page to a friend!
  Info   View files Example   View files View files (12)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 55 This week: 1All time: 10,546 This week: 560Up
Version License PHP version Categories
find-diff 1.0.0GNU Lesser Genera...5Algorithms, PHP 5, Text processing
Description 

Author

This package can calculate the similarity level of two text strings.

It can take two text strings and apply algorithms to calculate a number that represents the similarity between those strings.

The class that implements the Hamming distance algorithm returns the number of text characters that are different in each string.

The class that implements the Levenshtein algorithm returns a number that represents the number of steps that are necessary to change the first string into the second strings.

Picture of mohammad anzawi
  Performance   Level  
Name: mohammad anzawi <contact>
Classes: 8 packages by
Country: Jordan Jordan
Age: 9
All time rank: 14322 in Jordan Jordan
Week rank: 106 Up1 in Jordan Jordan Up
Innovation award
Innovation award
Nominee: 2x

Example

<?php
require_once 'vendor/autoload.php';
$action = new \Anzawi\App\Action();

if (
$_GET && isset($_GET['first_string']) && isset($_GET['second_string'])) {
   
$data = $_GET;
   
$action->set($_GET['first_string'], $_GET['second_string']);

    if (
$action->validate()) {
       
$action->run();
    }
}
?>

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<?php if ($action->errors()): ?>
<p style="background-color: #ffcccc; color: red">
    all fields are required!
</p>
<?php endif; ?>
<form action="/" method="get">
    <label>
        First string
        <input type="text" maxlength="255" name="first_string" value="<?php echo $action->getFirstString() ?>">
    </label>
    <label>
        Second string
        <input type="text" maxlength="255" name="second_string" value="<?php echo $action->getSecondString()?>">
    </label>

    <button type="submit">Calculate</button>
</form>
<?php if($action->isSubmitted() === true): ?>
<div style="padding: 10px">
        <div>
            <table style="border: 2px solid;">
                <tr>
                    <?php foreach ($action->getDiff() as $item): ?>
<td><?php echo $item; ?></td>
                    <?php endforeach; ?>
</tr>
            </table>
        </div>

        <span>Levenshtein Distance: <?php echo $action->getLevenshtein() ?></span>
        <br>
        <span>Hamming Distance: <?php echo $action->getHamming() ?></span>
    </div>
<?php endif; ?>
</body>
</html>


Details

find-diff

calculate levenshtein distances &amp; hamming distances between 2 strings using PHP

please note : PHP provided build-in functions to do what this library do it's just for fun !

levenshtein function

gmp_hamdist function

main Classes located in (/libs/) directory

installation

  • open your `terminal`/`cmd`/`powershell`
  • navigate to your projects directory
  • run `git clone https://github.com/anzawi/find-diff.git`
  • navigate to project `cd find-diff`
  • run `composer install`

this project require find-diff as a library check composer.json file and take a look on repositories section.

run test in command-line

  • open your `terminal`/`cmd`/`powershell`
  • navigate to project directory
  • run `php runTest --s1="Mohammad" --s2="Mohammed"`

run test project

  • run any preferred php server
  • navigate to project
  • you will see a simple form
  • start testing

don't forget to run test using phpunit ./vendor/bin/phpunit


  Files folder image Files  
File Role Description
Files folder imageapp (1 file)
Files folder imagelibs (2 files, 1 directory)
Files folder imagetests (2 files)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file index.php Example Example script
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file readme.md Doc. Documentation
Accessible without login Plain text file runTest Data Auxiliary data

  Files folder image Files  /  app  
File Role Description
  Accessible without login Plain text file Action.php Class Class source

  Files folder image Files  /  libs  
File Role Description
Files folder imagesrc (2 files)
  Accessible without login Plain text file composer.json Data Auxiliary data
  Accessible without login Plain text file composer.lock Data Auxiliary data

  Files folder image Files  /  libs  /  src  
File Role Description
  Accessible without login Plain text file Hamming.php Class Class source
  Accessible without login Plain text file Levenshtein.php Class Class source

  Files folder image Files  /  tests  
File Role Description
  Accessible without login Plain text file HammingTest.php Class Class source
  Accessible without login Plain text file LevenshteinTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:55
This week:1
All time:10,546
This week:560Up