PHP's similar_text in JavaScript

Here’s what our current JavaScript equivalent to PHP's similar_text looks like.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
module.exports = function similar_text (first, second, percent) { // eslint-disable-line camelcase
// discuss at: http://locutus.io/php/similar_text/
// original by: Rafał Kukawski (http://blog.kukawski.pl)
// bugfixed by: Chris McMacken
// bugfixed by: Jarkko Rantavuori original by findings in stackoverflow (http://stackoverflow.com/questions/14136349/how-does-similar-text-work)
// improved by: Markus Padourek (taken from http://www.kevinhq.com/2012/06/php-similartext-function-in-javascript_16.html)
// example 1: similar_text('Hello World!', 'Hello locutus!')
// returns 1: 8
// example 2: similar_text('Hello World!', null)
// returns 2: 0
if (first === null ||
second === null ||
typeof first === 'undefined' ||
typeof second === 'undefined') {
return 0
}
first += ''
second += ''
var pos1 = 0
var pos2 = 0
var max = 0
var firstLength = first.length
var secondLength = second.length
var p
var q
var l
var sum
for (p = 0; p < firstLength; p++) {
for (q = 0; q < secondLength; q++) {
for (l = 0; (p + l < firstLength) && (q + l < secondLength) && (first.charAt(p + l) === second.charAt(q + l)); l++) { // eslint-disable-line max-len
// @todo: ^-- break up this crazy for loop and put the logic in its body
}
if (l > max) {
max = l
pos1 = p
pos2 = q
}
}
}
sum = max
if (sum) {
if (pos1 && pos2) {
sum += similar_text(first.substr(0, pos1), second.substr(0, pos2))
}
if ((pos1 + max < firstLength) && (pos2 + max < secondLength)) {
sum += similar_text(
first.substr(pos1 + max, firstLength - pos1 - max),
second.substr(pos2 + max,
secondLength - pos2 - max))
}
}
if (!percent) {
return sum
}
return (sum * 200) / (firstLength + secondLength)
}
[ View on GitHub | Edit on GitHub | Source on GitHub ]

How to use

You you can install via npm install locutus and require it via require('locutus/php/strings/similar_text'). You could also require the strings module in full so that you could access strings.similar_text instead.

If you intend to target the browser, you can then use a module bundler such as Browserify, webpack or rollup.js.

ES5/ES6

This function targets ES5, but as of Locutus 2.0.2 we also support ES6 functions. Locutus transpiles to ES5 before publishing to npm.

A community effort

Not unlike Wikipedia, Locutus is an ongoing community effort. Our philosophy follows The McDonald’s Theory. This means that we don't consider it to be a bad thing that many of our functions are first iterations, which may still have their fair share of issues. We hope that these flaws will inspire others to come up with better ideas.

This way of working also means that we don't offer any production guarantees, and recommend to use Locutus inspiration and learning purposes only.

Examples

Please note that these examples are distilled from test cases that automatically verify our functions still work correctly. This could explain some quirky ones.

#codeexpected result
1similar_text('Hello World!', 'Hello locutus!')8
2similar_text('Hello World!', null)0

« More PHP strings functions