search
HomeBackend DevelopmentPHP ProblemHow to implement fuzzy query in php

How to implement fuzzy query in php

Aug 20, 2020 am 09:46 AM
phpfuzzy query

How to implement fuzzy query in php: 1. Use SQL matching mode, the operator must use LIKE or NOT LIKE, when matching, it is not case sensitive; 2. Use regular expression matching mode, its regular expression The formula appears anywhere in the matching field.

How to implement fuzzy query in php

[Related learning recommendations: php programming (video)]

php achieves blurring Query method:

Method 1: SQL matching pattern

1. Use sql matching pattern. You cannot use the operator = or ! =, but use the operator LIKE or NOT LIKE;

2. Using sql matching mode, MYSQL provides 2 kinds of wildcard characters.

  • % represents any number of any characters (including 0)

  • _ Represents any single character

3. Use sql matching mode. If the matching format does not contain any of the above two wildcard characters, the query effect is equivalent to = or! =

4. Use sql matching pattern. When matching, it is case-insensitive

#查询用户名以某个字符开头的用户
#查询用户名以字符'l'开头的用户: l%
SELECT * FROM user WHERE username LIKE 'l%';
#查询用户名以某个字符结尾的用户
#查询用户名以字符'e'结尾的用户:e%
SELECT * FROM user WHERE username LIKE 'e%';
#查询用户名包含某个字符的用户
#查询用户名包含字符'o'的用户:%o%
SELECT * FROM user WHERE username LIKE '%o%';
#查询包含三个字符的用户
SELECT * FROM user WHERE username LIKE '___';
#查询用户名第二个字符为o的用户:_o%
SELECT * FROM user WHERE username LIKE '_o%';

Method 2: Regular expression matching pattern

Wildcard ( Regular expression)

.matches any single character

*matches 0 or more characters before it

x* means matching any number of x characters

  • [..] Matches any character in square brackets

  • [abc] Matches the characters ab or c

  • [a-z] Matches any letter

  • [0-9] Matches any number

  • [0-9]*Match any number of any digits

  • [a-z]*Match any number of letters

^ means starting with a certain character or string

^a means starting with the letter a

$ means ending with a certain character or string

s$ means ending with the letter s

The operator used to match the pattern using regular expressions is:

REGEXP or NOT REGEXP(RLIKE or NOT RLIKE)

Note: Regular expression matching pattern, its regular expression appears anywhere in the matching field ,

Even if the pattern matches, there is no need to put a wildcard character on both sides to make it match;

If only the wildcard character . is used to match, assuming N, then the matching pattern means that it is greater than Equal to N;

How do you understand the above sentence?

That is to say

... Matches data that is greater than or equal to 3 characters

.... Matches data that is greater than or equal to 4 characters

# Query users whose username starts with the character l: ^l;

#Regular expression writing

SELECT * FROM user WHERE username REGEXP '^l';
#sql匹配模式写法:
SELECT * FROM user WHERE username LIKE 'l%';
#查询用户名正好是三个字符的用户:^...$;
#sql匹配模式写法:
SELECT * FROM user WHERE username LIKE '___';
#正则表达式写法
SELECT * FROM user WHERE username REGEXP '^...$';

Related recommendations:Programming video course

The above is the detailed content of How to implement fuzzy query in php. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
ACID vs BASE Database: Differences and when to use each.ACID vs BASE Database: Differences and when to use each.Mar 26, 2025 pm 04:19 PM

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

PHP Secure File Uploads: Preventing file-related vulnerabilities.PHP Secure File Uploads: Preventing file-related vulnerabilities.Mar 26, 2025 pm 04:18 PM

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

PHP Input Validation: Best practices.PHP Input Validation: Best practices.Mar 26, 2025 pm 04:17 PM

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

PHP API Rate Limiting: Implementation strategies.PHP API Rate Limiting: Implementation strategies.Mar 26, 2025 pm 04:16 PM

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

PHP Password Hashing: password_hash and password_verify.PHP Password Hashing: password_hash and password_verify.Mar 26, 2025 pm 04:15 PM

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.Mar 26, 2025 pm 04:13 PM

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

PHP XSS Prevention: How to protect against XSS.PHP XSS Prevention: How to protect against XSS.Mar 26, 2025 pm 04:12 PM

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

PHP Interface vs Abstract Class: When to use each.PHP Interface vs Abstract Class: When to use each.Mar 26, 2025 pm 04:11 PM

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct

See all articles

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 Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft