search
HomeBackend DevelopmentPHP ProblemDetailed introduction to the difference between preg_filter() and preg_replace() in PHP

In PHP, we know that there are two regular expression filter functions, namely preg_filter() and preg_replace(), but not everyone knows the difference between them. Today I will take you to find out more about it.

Detailed introduction to the difference between preg_filter() and preg_replace() in PHP

PHP regular replacement filter function

How to say? The regular replacement filter function is very similar to the replacement function we talked about before, with only a little difference. I don’t know if you have noticed it!!

==preg_filter()==Function: Perform a regular expression search and replacement

Usually the preg_filter() function is equivalent to the preg_replace() function

The code of case 1 is as follows:

$arr=array('1.jpg','2.txt','3.doc','4.exe','5.php');
$pattern='/\.jpg|\.txt/';$replacement='';
$result1=preg_replace($pattern, $replacement, $arr);
$result2=preg_filter($pattern, $replacement, $arr);
show($result2);

The actual difference between preg_filter() and preg_replace()

The code of case 2 is as follows:

$pattern=array(
    "/\d+/",
    "/ccc/");
$replacement=array(
    '1024',
    'PHP');
$string=array(
    '1234aaa',
    'abbbccc',
    'wampserver');
  $result1=preg_replace($pattern, $replacement, $string);
  show($result1);
  $result2=preg_filter($pattern, $replacement, $string);
  show($result2);

So the difference is as follows:

preg_filter() will only return the array elements that have been replaced and filtered, and the array elements that have not been replaced are not returned.

preg_replace() returns not only the array elements that have been replaced and filtered, but also Elements that have not been replaced will also be retained and returned!

In fact, as long as everyone tests and prints out each other's results, you can immediately know the difference between them!

Recommended learning:php video tutorial

The above is the detailed content of Detailed introduction to the difference between preg_filter() and preg_replace() in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:慕课网. If there is any infringement, please contact admin@php.cn delete

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 Article

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft