PHP's similar_text in TypeScript

✓ Verified: PHP 8.3
Examples tested against actual runtime. CI re-verifies continuously. Only documented examples are tested.

How to use

Install via yarn add locutus and import: import { similar_text } from 'locutus/php/strings/similar_text'.

Or with CommonJS: const { similar_text } = require('locutus/php/strings/similar_text')

Use a bundler that supports tree-shaking so you only ship the functions you actually use. Vite, webpack, Rollup, and Parcel all handle this. For server-side use this is less of a concern.

Examples

These examples are extracted from test cases that automatically verify our functions against their native counterparts.

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

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

export function similar_text(
first: string | number | boolean | null | undefined,
second: string | number | boolean | null | undefined,
percent?: boolean | number | null | undefined,
): number {
// discuss at: https://locutus.io/php/similar_text/
// parity verified: PHP 8.3
// original by: Rafał Kukawski (https://blog.kukawski.pl)
// bugfixed by: Chris McMacken
// bugfixed by: Jarkko Rantavuori original by findings in stackoverflow (https://stackoverflow.com/questions/14136349/how-does-similar-text-work)
// improved by: Markus Padourek (taken from https://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
}

const firstValue = String(first)
const secondValue = String(second)

let pos1 = 0
let pos2 = 0
let max = 0
const firstLength = firstValue.length
const secondLength = secondValue.length
let sum = 0

for (let p = 0; p < firstLength; p++) {
for (let q = 0; q < secondLength; q++) {
let l = 0
for (
l = 0;
p + l < firstLength && q + l < secondLength && firstValue.charAt(p + l) === secondValue.charAt(q + l);
l++
) {
// @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(firstValue.substr(0, pos1), secondValue.substr(0, pos2))
}

if (pos1 + max < firstLength && pos2 + max < secondLength) {
sum += similar_text(
firstValue.substr(pos1 + max, firstLength - pos1 - max),
secondValue.substr(pos2 + max, secondLength - pos2 - max),
)
}
}

if (!percent) {
return sum
}

return (sum * 200) / (firstLength + secondLength)
}

Improve this function

Locutus is a community effort following The McDonald's Theory: we ship first iterations, hoping others will improve them. If you see something that could be better, we'd love your contribution.

View on GitHub · Edit on GitHub · View Raw


« More PHP strings functions


Star