


本节内容我们将介绍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.

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version
SublimeText3 Linux latest version

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

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

Dreamweaver CS6
Visual web development tools
