Check if strings are x% equal

2024/2/27 8:45:49

I want to compare three strings:

"a text string" //target string
"a kind of similar text string"
"the cow jumps over the moon"

And set a percentage parameter that returns the results that are x% similar to the target.

$results = compare($array,$target,80)$results // Returns an array with str 1,2, because they are at least 80 similar to the target.

Also, is a similar function possible in JavaScript or jQuery?

Answer

In PHP, there is the function similar_text. As for JavaScript, there's the PHP.js project, which re-implements PHP functions in JavaScript. They have an implementation of similar_text you can use.

The JavaScript implementation doesn't support the percent parameter, it seems.

http://en.ppmy.cn/q/41953.html

Related Q&A

Get index of select option in optgroup

If I have an option list like the below:<select name="optionList" id="optionList"> <optgroup label="optgroup1"> <option value="1">1</option&…

Page jumps to the top onclick [duplicate]

This question already has answers here:Prevent href="#" link from changing the URL hash(11 answers)Closed 9 years ago.I have a click in the middle of a website with code like <a href=“#“…

Insert text before and after selection in a contenteditable

Is there a way to insert text similar to bbcode before and after the selected text in a contenteditable div? Ive seen many answers for textarea, but the code does not work with a contenteditable div. …

Countdown Timer that doesnt reset when you refresh the page?

I Need to have a code for the timer that doesnt reset the countdown timer even you refresh the page. I saw sample code at keith wood plug in, but dont know how to implement this in my html file. heres …

setTimeout function doesnt stop execution?

I have stumbled upon a problem with setTimeout.$current.removeClass(class2); setTimeout(function(){$current.css(display, none)},1000); if(!$current.is(# + id)){$(# + id).css(display,block);setTimeout(f…

How can I add a button over a video in HTML? [duplicate]

This question already has answers here:How can I position an icon over an image or video?(4 answers)Closed 7 months ago.This is my first time working with HTML other than basic websites.I have a video…

Whats the difference between window.x and x?

Lets say, "x" is a variable that never defined, so it should be undefined. In the following scenario: 1)if(x){//do something} //ReferenceError: x is not defined2)if(window.x){//do something} …

How can I use Multi Media Uploader in the WordPress Plugins?

I try to add the multi uploading options in the wordpress plugins I repeated this code in the plugin(two times) only changing the id name. <script language="JavaScript"> jQuery(document…

Object has no method apply

I am creating a few DOM elements dynamically like,var anchorElement = jQuery(<a />,{text:property.text}); var liElement = jQuery(<li />,{"class":"navlink_"+i,id:"na…

What are the risks of letting users upload and run Javascript

If you have say an HTML5 games arcade, that allows users to upload a script that runs a game with HTML5 and Javascript, assuming you have no filters on their input (apart from only allowing JS and HTML…