search
HomeBackend DevelopmentPHP ProblemA brief discussion on the difference between preg_match and preg_match_all functions in PHP

This article analyzes and introduces the preg_match function and preg_match_all function in PHP regular expressions. Friends in need can refer to it.

A brief discussion on the difference between preg_match and preg_match_all functions in PHP

preg_match_all() function

Search in the given string according to the specified regular expression and match Take out the parts that fit the characteristics.

$pattern='/t(.*?)st/';
$str='tgvregbvst      test    tdst';
var_dump(preg_match_all($pattern,$str,$arr));
var_dump($arr);

We can see that the output result is:

int(3) array(2) { [0]=> array(3) { [0]=> string(10) "tgvregbvst" [1]=> string(4) "test" [2]=> string(4) "tdst" } [1]=> array(3) { [0]=> string(7) "gvregbv" [1]=> string(1) "e" [2]=> string(1) "d" } }

He lists all those that meet the conditions, but the preg_match() function is different.

preg_match() function

Performs a regular expression match and returns the number of pattern matches. Its value will be 0 (no match) or 1 because preg_match() will stop the search after the first match.

$pattern='/t(.*?)st/';
$str='tgvregbvst      test    tdst';
var_dump(preg_match($pattern,$str,$arr));
var_dump($arr);

We can see that the output result is:

int(1) array(2) { [0]=> string(10) "tgvregbvst" [1]=> string(7) "gvregbv" }

Compared with the previous function, the output result of this function is much less. This is their difference.

preg_match_all()Unlike the preg_match() function, it will search subject until it reaches the end. If an error occurs preg_match() returns false.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of A brief discussion on the difference between preg_match and preg_match_all functions 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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment