


Introduction to delimiters and atoms in PHP regular expressions_PHP Tutorial
本节内容我们将介绍PHP中正则 表达式的基础语法:定界符和原子。内容包含了定界符的定义以及原子的定义和构成等等。其中原子的构成十分灵活,以便满足我们对处理字符串的需求。在这之 前,我们需要先了解一个正则表达式处理函数preg_match()来进行测试,以方便我们教程示例的进行。
先来看一下正则表达式的定界符、正则表达式的构成以及preg_match()函数:
1,正则表达式的定界符。
除了字母、数字和反斜线\以外的任何字符都可以为定界符号,比如 | |、//、{}、!!等等,但是需要注意,如果没有特殊需要,我们都使用正斜线//作为正则表达式的定界符号。
2,正则表达式的构成。
我们看一下这个公式:/原子和元字符/模式修正符
也就是说,正则表达式的原子和元字符都放在定界符之间,而模式修正符放在定界符之外。
3,preg_match()函数
我们会在后面进行详细解释,这里只是为了帮助测试,其返回一个布尔值,表示是否成功匹配。
了解完以上简单的内容,让我们进入正题。
正则表达式中的原子
什么是原子?原子是正则表达式的最基本组成单位,而且必须至少要包含一个原子。只要一个正则表达式可以单独使用的字符,就是原子。
这个概念可能看起来很模糊,没关系,下面我们来介绍一下正则表达式中原子的构成方式。
原子构成方式
1,所有打印(所有可以在屏幕上输出的字符串)和非打印字符(看不到的,比如空格,换行符等等)
2,如果所有有意义的字符,想做为原子使用,统统使用“\”转义字符进行转义即可。如:\. \* \+ \? \( \。
注意:" \ "转义字符可以将有意义的字符转成没意义的字符,还可以将没意义的字符转为有意义的字符。如:\d表示任意一个十进制的数字。
3,在正则表达式中可以直接使用一些系统提供的代表范围的原子,如下面的表格所示:
代表范围的原子 | 说明 | 自定义原子表示法 |
\d | 表示任意一个十进制的数字 | [0-9] |
\D | 表示任意一个除数字这外的字符 | [^0-9] |
\s | 表示任意一个空白字符,空格、\n\r\t\f | [\n\r\t\f ] |
\S | 表示任意一个非空白 | [^\n\r\t\f ] |
\w | 表示任意一个字 a-zA-Z0-9_ | [a-zA-Z0-9_] |
\W | 表示任意一个非字,除了a-zA-Z0-9_以外的任意一个字符 | [^a-zA-Z0-9_] |
4. Customize the atom table (using square brackets []), which can match any atom in the square brackets.
In the above table, we have equivalently converted the range atoms provided by the system in a custom way. Since the system cannot provide all the atoms I need, it is necessary to customize the atom table. For example, if we want to match letters or numbers, we need to write the atoms as [a-zA-Z0-9].
Note here:
A, the symbol "-" represents the range, such as [a-z] represents the lowercase letters a to z, but never write it as [a- 9] This form!
B, the symbol "^" means negation, and must be placed at the beginning of the square brackets. For example, if we want to match non-digits, the atom is [^0-9].
Let’s take a look at an example of using regular expression atoms. The code is as follows:
$pattern = '/d/';//Number atomic table, which is the pattern of regular expressions
$string = 'dsadsadsa';//Requires matching String
if(preg_match($pattern, $string)){
echo "Regular expression{$pattern} and string{$string} Match successful";
}else{
echo "Regular expression {$pattern} and string {$string} failed to match< ;/span>";
}
?>
Note: If one atom in the custom atom table is matched by a string, the match is successful. Removing the square brackets of the custom atom table means matching the entire string. For example, '/abc/' means that the substring abc must be included in the string to be matched, and '/[abc]/' means that the string will be matched as long as it contains any one of the characters a, b, and c.
You can modify the pattern in the above example (that is, the pattern variable $pattern of the regular expression), and then verify the atoms of the regular expression we talk about in this section.
This section has finished introducing the delimiters and atoms of regular expressions. I believe that based on practice, you will already be able to use atoms of regular expressions. In the next section we will introduce metacharacters in PHP regular expressions, don’t miss it.

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

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

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.

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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
