<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>'<pre class="brush:php;toolbar:false">'; print_r($var); echo'
'; }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);
结果:
结论:
<code>preg\_replace在数组匹配中会输出未发生匹配的字段; preg\_filter 在数组匹配中不会输出未发生匹配的字段; </code>
- preg_grep($pattern,array $input)
功能与preg_filter 相似,只匹配不替换而且只返回发生匹配的字符串或数组,可以起一个过滤作用,过滤掉preg_filter不替换的符串或数组。
以上就介绍了Regular expressions in PHP replace, filter, grep,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl


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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version
Chinese version, very easy to use

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
