PHP Classes

Simple PHP String Comparison: Compare strings and return a similarity score

Recommend this page to a friend!
  Info   View files Example   View files View files (4)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2023-11-01 (2 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 46 All time: 10,660 This week: 175Up
Version License PHP version Categories
simple-string-compar 1.0GNU General Publi...5PHP 5, Text processing
Description 

Author

This package can compare strings and return a similarity score.

It can take two strings as parameters and create arrays with the codes of each character of the strings.

The class can compare the two arrays of string character codes and return a number that is the hammering distance and represents a value that determines how similar the two strings are.

Picture of Ákos Nikházy
Name: Ákos Nikházy <contact>
Classes: 9 packages by
Country: Hungary Hungary
Innovation award
Innovation award
Nominee: 1x

Example

<?php
spl_autoload_register
(function ($class) {
    include
$class . '.class.php';
});

/* examples */
    /* string mode off for letter by letter comparison */
// int(0) the same text
var_dump(stringDifference::hammeringDistance('The quick brown fox jumps over the lazy dog','The quick brown fox jumps over the lazy dog'));

// int(1) one letter difference
var_dump(stringDifference::hammeringDistance('The quick brown fex jumps over the lazy dog','The quick brown fox jumps over the lazy dog'));

// int(1) there is a space at the end of the first string
var_dump(stringDifference::hammeringDistance('The quick brown fox jumps over the lazy dog ','The quick brown fox jumps over the lazy dog'));


   
/* bin mode on for bit by bit comparison */
// int(0) the same text
var_dump(stringDifference::hammeringDistance('The quick brown fox jumps over the lazy dog','The quick brown fox jumps over the lazy dog',true));

// int(2) one letter difference
var_dump(stringDifference::hammeringDistance('The quick brown fex jumps over the lazy dog','The quick brown fox jumps over the lazy dog',true));

// int(8) there is a space at the end of the first string
var_dump(stringDifference::hammeringDistance('The quick brown fox jumps over the lazy dog ','The quick brown fox jumps over the lazy dog',true));


Details

simple-string-comparison

Less simple than strcmp. Compare strings letter by letter or bit by bit

It returns an int value representing the difference between two strings. For example, it gives a more precise difference between uppercase and lowercase letters, so you can decide if there is a typo between two strings. There are also letter-by-letter and binary comparisons.

Use it like this:

Letter by letter mode: This will return 1 as one letter is different (in the word fox there is a typo).

echo stringDifference::hammeringDistance('The quick brown fex jumps over the lazy dog','The quick brown fox jumps over the lazy dog')

Binary mode: This will return 2 because in binary mode the distance between the e and o in the two sentences is 2.

echo stringDifference::hammeringDistance('The quick brown fex jumps over the lazy dog','The quick brown fox jumps over the lazy dog',true));

  Files folder image Files  
File Role Description
Accessible without login Plain text file examples.php Example Example script
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation
Plain text file stringDifference.class.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:46
This week:0
All time:10,660
This week:175Up