Regular expressions in PHP replace, filter, grep
<code> - preg\_replace( \$pattern,\$replacement,\$subject),preg\_filter ( \$pattern,\$replacement,\$subject); preg\_replace 和preg\_filter 将匹配到的数值进行替换,可以使用数组替换; \ $replacement 将匹配到的值替换为这个变量中存放的值; \$pattern正则表达式; \$subject返回结果; </code>
区别:
<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">'
'
'; show($str2);
结果:
结论:
<code>preg\_replace在数组匹配中会输出未发生匹配的字段; preg\_filter 在数组匹配中不会输出未发生匹配的字段; </code>
- preg_grep($pattern,array $input)
功能与preg_filter 相似,只匹配不替换而且只返回发生匹配的字符串或数组,可以起一个过滤作用,过滤掉preg_filter不替换的符串或数组。
以上就介绍了Regular expressions in PHP replace, filter, grep,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics


![How to solve the '[Vue warn]: Failed to resolve filter' error](https://img.php.cn/upload/article/000/887/227/169243040583797.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
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

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

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;//

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

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

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

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 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
