Home Backend Development PHP Tutorial Regular expressions in PHP replace, filter, grep

Regular expressions in PHP replace, filter, grep

Jul 29, 2016 am 09:00 AM
filter pattern

<code> - preg\_replace( \$pattern,\$replacement,\$subject),preg\_filter ( \$pattern,\$replacement,\$subject);
 preg\_replace 和preg\_filter 将匹配到的数值进行替换,可以使用数组替换;
\ $replacement 将匹配到的值替换为这个变量中存放的值;
 \$pattern正则表达式;
 \$subject返回结果;
</code>
Copy after login

区别:

<code><span><span>function</span><span>show</span><span>(<span>$var</span> = null)</span>{</span><span>if</span>(<span>empty</span>(<span>$var</span>)){
        <span>echo</span><span>'null'</span>;
    }<span>elseif</span>(is_array(<span>$var</span>)||is_object(<span>$var</span>)){
        <span>echo</span><span>'<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">'
Copy after login
Copy after login
; print_r($var); echo'
'
Copy after login
; }else { echo$var; } } //preg_replace,preg_filter//$pattern = '/[0-9]/';//字符串$pattern = array('/[0123]/','/[456]/','/[789]/');//数组//$subject = 'y1jp78yn16ww55j9';//是字符串,使用$pattern和$replacement作了一次匹配替换$subject = array('y','1jp78','yn','16ww','55j9');//把$subject字符串任意拆分成好几段,放在$subject数组中,对数组中的每个值都做一次匹配替换//$replacement = '慕女神';//当$subject匹配到结果后要替换的变量$replacement = array('慕','女','神'); $str1 = preg_replace($pattern,$replacement,$subject);//保留所有字符串$str2 = preg_filter($pattern,$replacement,$subject);//只会保留发生了替换的字符串 show($str1); echo'
'
; show($str2);

结果:
Regular expressions in PHP replace, filter, grep

结论:

<code>preg\_replace在数组匹配中会输出未发生匹配的字段;
preg\_filter 在数组匹配中不会输出未发生匹配的字段;
</code>
Copy after login
  • preg_grep($pattern,array $input)
    功能与preg_filter 相似,只匹配不替换而且只返回发生匹配的字符串或数组,可以起一个过滤作用,过滤掉preg_filter不替换的符串或数组。
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介绍了Regular expressions in PHP replace, filter, grep,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

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

Video Face Swap

Video Face Swap

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

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)

How to solve the '[Vue warn]: Failed to resolve filter' error How to solve the '[Vue warn]: Failed to resolve filter' error Aug 19, 2023 pm 03:33 PM

Methods to solve the "[Vuewarn]:Failedtoresolvefilter" error During the development process using Vue, we sometimes encounter an error message: "[Vuewarn]:Failedtoresolvefilter". This error message usually occurs when we use an undefined filter in the template. This article explains how to resolve this error and gives corresponding code examples. When we are in Vue

Using Pattern.compile method in Java Using Pattern.compile method in Java Feb 18, 2024 pm 09:04 PM

Usage of Pattern.compile function in Java The Pattern.compile function in Java is a method used to compile regular expressions. Regular expression is a powerful string matching and processing tool that can be used to find, replace, verify strings and other operations. The Pattern.compile function allows us to compile a string pattern into a Pattern object, which can then be used to perform a series of string operations. Pattern.compi

What is the principle and registration method of filter in Springboot What is the principle and registration method of filter in Springboot May 11, 2023 pm 08:28 PM

1. Filter First look at the location of the filter of the web server. Filter is a chain connected before and after. After the previous processing is completed, it is passed to the next filter for processing. 1.1Filter interface definition publicinterfaceFilter{//Initialization method, only executed once in the entire life cycle. //Filtering services cannot be provided until the init method is successfully executed (failure such as throwing an exception, etc.). //The parameter FilterConfig is used to obtain the initialization parameter publicvoidinit(FilterConfigfilterConfig)throwsServletException;//

How to integrate Filter in SpringBoot2 How to integrate Filter in SpringBoot2 May 16, 2023 pm 02:46 PM

First define a Filter for unified access URL interception. The code is as follows: publicclassUrlFilterimplementsFilter{privateLoggerlog=LoggerFactory.getLogger(UrlFilter.class);@OverridepublicvoiddoFilter(ServletRequestrequest,ServletResponseresponse,FilterChainchain)throwsIOException,ServletException{H

PatternSyntaxException class in Java regular expressions PatternSyntaxException class in Java regular expressions Sep 11, 2023 pm 07:37 PM

The PatternSyntaxException class represents an unchecked exception thrown when a syntax error occurs in a regular expression string. This class contains three main methods namely - getDescription() - returns the description of the error. getIndex() - Returns the error index. getPattern() - Returns the regular expression pattern in which the error occurred. getMessage() - Returns the complete message containing the error, the index, the regular expression pattern in which the error occurred, and the error in the indicated pattern. Example Real-time demonstration importjava.util.Scanner;importjava.util.regex.Matcher;i

How to filter in java How to filter in java Apr 18, 2023 pm 11:04 PM

Note 1. If the Lambda parameter generates a true value, the filter (Lambda that can generate a boolean result) will generate an element; 2. When false is generated, this element will no longer be used. Example to create a List collection: ListstringCollection=newArrayList();stringCollection.add("ddd2");stringCollection.add("aaa2");stringCollection.add("bbb1");stringC

CSS visual property analysis: box-shadow, text-shadow and filter CSS visual property analysis: box-shadow, text-shadow and filter Oct 20, 2023 pm 12:51 PM

Analysis of CSS visual properties: box-shadow, text-shadow and filter Introduction: In web design and development, CSS can be used to add various visual effects to elements. This article will focus on the three important properties of box-shadow, text-shadow and filter in CSS, including their usage and effect display. Below we analyze these three properties in detail. 1. box-shadow (box shadow) box-shado

Detailed explanation of CSS blur properties: filter and backdrop-filter Detailed explanation of CSS blur properties: filter and backdrop-filter Oct 20, 2023 pm 04:48 PM

Detailed explanation of CSS fuzzy attributes: filter and background-filter Introduction: When designing web pages, we often need some special effects to increase the visual appeal of the page. The blur effect is one of the common special effects. CSS provides two blur attributes: filter and background-filter, which are used to blur element content and background content respectively. This article explains these two properties in detail and provides some concrete code examples. 1. filter

See all articles