Table of Contents
Use PHP similar text to calculate the similarity of two strings, similartext
Home Backend Development PHP Tutorial Use PHP similar text to calculate the similarity of two strings, similartext_PHP tutorial

Use PHP similar text to calculate the similarity of two strings, similartext_PHP tutorial

Jul 12, 2016 am 09:05 AM
php text string

Use PHP similar text to calculate the similarity of two strings, similartext

In website development, we often use php similar text to calculate the similarity of two strings;

1, usage of similar_text

If I want to calculate the similarity between "ly89cn" and "ly89", there are two ways to express it

Copy code The code is as follows:
echo similar_text('ly89cn', 'ly89');

This outputs 4 because they have 4 equal characters

Copy code The code is as follows:
similar_text('ly89cn', 'ly89', $percent);
echo $percent;

This outputs 80, $percent represents the percentage, indicating that they are 80% similar

Of course, you can also compare two Chinese characters, such as "Wang Yelou's personal blog" and "Wang Yelou"

Copy code The code is as follows:
echo similar_text('Wang Yelou's personal blog', 'Wang Yelou');

Output 9 like this, indicating that their 9 bytes are equal

Copy code The code is as follows:
similar_text('Wang Yelou's personal blog', 'Wang Yelou', $percent);
echo $percent;

Output 54.545454545455, please note that Chinese characters may not be accurate!

PHP similar_text() function

Example

Calculate the similarity between two strings and return the number of matching characters:

Copy code The code is as follows:
echo similar_text("Hello World","Hello Shanghai");
?>

Running instance

Definition and usage

The similar_text() function calculates the similarity of two strings.

This function can also calculate the percentage similarity of two strings.

Note: levenshtein() function is faster than similar_text() function. However, the similar_text() function provides more accurate results with fewer modifications required.

Grammar

Copy code The code is as follows:
similar_text(string1,string2,percent)

Parameters Description
string1 Required. Specifies the first string to compare.
string2 Required. Specifies the second string to be compared.
percent Optional.规定供存储百分比相似度的变量名。

技术细节

 

返回值:
返回值: 返回两个字符串的匹配字符的数目。
PHP 版本: 4
返回两个字符串的匹配字符的数目。

PHP 版本: 4

更多实例 例子 1
计算两个字符串之间的百分比相似度:



复制代码

代码如下:

similar_text("Hello World","Hello Shanghai",$percent); ?> http://www.bkjia.com/PHPjc/1068822.htmlwww.bkjia.comtrue

http://www.bkjia.com/PHPjc/1068822.html
TechArticle
使用PHP similar text计算两个字符串相似度,similartext 在网站开发中,我们经常使用php similar text 计算两个字符串相似度; 1,similar_text的用法 如...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles