


The article introduces a detailed explanation of the sub-patterns of regular expressions in PHP. Friends who need to know the sub-patterns of regular expressions in PHP can refer to it.
Function
mixed preg_replace (mixed pattern, mixed replacement, mixed subject [, int limit])
Function
Search subject for a match of pattern and replace with replacement. If limit is specified, only limit matches are replaced; if limit is omitted or has a value of -1, all matches are replaced.
Replacement can contain a reverse reference in the form of n or $n. n can be from 0 to 99. n represents the text matching the nth sub-pattern of pattern.
The regular expression enclosed in parentheses in the $pattern parameter, the number of sub-patterns is the number of parentheses from left to right. (pattern is pattern)
The code is as follows | Copy code |
代码如下 | 复制代码 |
$time = date ("Y-m-d H:i:s"); $pattern = "/d{4}-d{2}-d{2} d{2}:d{2}:d{2}/i"; if(preg_match($pattern,$time,$arr)){ echo " ";<br> print_r($arr); <br> echo ""; } ?> |
$pattern = "/d{4}-d{2}-d{2} d{2}:d{2}:d{2}/i";
If(preg_match($pattern,$time,$arr)){echo "
";<br> Print_r($arr); <br> echo "";
} ?>
Display results:
Array
(
[0] => 2012-06-23 03:08:45) Have you noticed that the displayed result only has one piece of data, which is the time format that matches the matching pattern? So if there is only one record, why should it be saved in an array? Wouldn't it be better to save it directly as a string?
代码如下 | 复制代码 |
$time = date ("Y-m-d H:i:s"); $pattern = "/(d{4})-(d{2})-(d{2}) (d{2}):(d{2}):(d{2})/i"; if(preg_match($pattern,$time,$arr)){ echo " ";<br> print_r($arr); <br> echo ""; } ?> |
The code is as follows | Copy code |
$time = date ("Y-m-d H:i:s");
$pattern = "/(d{4})-(d{2})-(d{2}) (d{2}):(d{2}):(d{2})/i";
If(preg_match($pattern,$time,$arr)){
echo ""; Print_r($arr); echo ""; } ?> |
Note: I only modified $pattern. In the matching pattern, I used brackets ()
Execution result:
Array
(
[0] => 2012-06-23 03:19:23
[1] => 2012
[2] => 06
[3] => 23
[4] => 03
[5] => 19
[6] => 23
)
Summary: We can use parentheses to group the entire matching pattern. By default, each group will automatically have a group number. The rule is, from left to right, with the left parenthesis of the group as the mark, the first group that appears The first one is group number 1, the second one is group number 2, and so on. Among them, group 0 corresponds to the entire regular expression. After the entire regular matching pattern is grouped, you can further use "backward reference" to repeatedly search for text matching a previous group. For example: 1 represents the text matched by group 1, 2 represents the text matched by group 2, etc. We can further modify the code as follows:
The code is as follows | Copy code |
代码如下 | 复制代码 |
$time = date ("Y-m-d H:i:s"); $pattern = "/(d{4})-(d{2})-(d{2}) (d{2}):(d{2}):(d{2})/i"; $replacement = "$time格式为: 替换后的格式为:1年2月3日 4时5分6秒"; print preg_replace($pattern, $replacement, $time); if(preg_match($pattern,$time,$arr)){ echo " ";<br> print_r($arr); <br> echo ""; } ?> |
The replaced format is: February 3, 1 year, 4 hours, 5 minutes and 6 seconds"; Print preg_replace($pattern, $replacement, $time); If(preg_match($pattern,$time,$arr)){ echo "
"; print_r($arr); echo ""; } ?>
Note:
Because it is in double quotes, you should use two backslashes when using grouping, such as: 1, and if it is in single quotes, use one backslash, such as: 1
1 is used to capture the content of group one: 2012, 6 is used to capture the content of group 6
Execution result:
The format of $time is: 2012-06-23 03:30:31
The replaced format is: June 23, 2012 03:30:31
Array
(
[0] => 2012-06-23 03:30:31
[1] => 2012
[2] => 06
[3] => 23
[4] => 03
[5] => 30
[6] => 31
)
Advanced Regular Expressions
In addition to POSIX BRE and ERE, libutilitis also supports advanced regular expression languages compatible with TCL 8.2
Law (ARE). ARE mode can be enabled by adding the prefix "***:" to the stRegEx parameter. This prefix replaces
Cover bExtended option. Basically, ARE is a superset of ERE. It performs the following steps based on ERE
Item extension:
1. Support "lazy matching" (also called "non-greedy matching" or "shortest matching"): in '?', '*', '+' or '{m,n}'
You can enable shortest matching by appending the '?' symbol after , so that the regular expression clause matches
if the conditions are met.
Match as few characters as possible (the default is to match as many characters as possible). For example: apply "a.*b" to "abab"
When "a.*?b" is used, it will match the entire string ("abab"). If "a.*?b" is used, it will only match the first two characters ("ab").
2. Supports forward reference matching of subexpressions: In stRegEx, you can use 'n' to forward reference a previously defined
Subexpression. For example: "(a.*)1" can match "abcabc" etc.
3. Unnamed subexpression: Use "(?:expression)" to create an unnamed expression. The unnamed expression does not return
to an 'n' match.
4. Forward prediction: To hit a match, the specified conditions must be met forward. Forward prediction is divided into positive prediction and negative prediction
There are two kinds. The syntax for positive prediction is: "(?=expression)", for example: "bai.*(?=yang)" matches "bai yang"
The first four characters ("bai ") in , but when matching, ensure that the string must contain "yang".
after "bai.*"
The syntax for negative judgment is: "(?!expression)", for example: "bai.*(?!yang)" matches the first
of "bai shan"
Four characters, but when matching, it is ensured that "yang" does not appear after "bai.*" in the string.
5. Support mode switching prefix, "***:" can be followed by a pattern string in the form of "(?pattern string)", pattern
The string affects the semantics and behavior of subsequent expressions. The pattern string can be a combination of the following characters:
b - Switch to POSIX BRE mode, overriding the bExtended option.
e - Switch to POSIX ERE mode, overriding the bExtended option.
q - Switch to text literal matching mode, the characters in the expression are searched as text, and all regular expressions are canceled
Semantics. This mode reduces regular matching to a simple string search. "***=" prefix is its shortcut representation
Method, meaning: "***=" is equivalent to "***:(?q)".
c - Perform case-sensitive matching, overrides the bNoCase option.
i - performs a case-ignoring match, overrides the bNoCase option.
n - Enable line-sensitive matching: '^' and '$' match the beginning and end of the line; '.' and negation sets ('[^...]') do not
Matches newline characters. This functionality is equivalent to the 'pw' pattern string. Override the bNewLine option.
m - Same as 'n'.
p - '^' and '$' only match the beginning and end of the entire string, not lines; '.' and the negative set do not match newlines.
Overrides the bNewLine option.
w - '^' and '$' match the beginning and end of the line; '.' and the negative set match newlines. Override the bNewLine option.
s - '^' and '$' only match the beginning and end of the entire string, not lines; '.' and the negative set match newlines. Reply
Cover bNewLine option. This mode is used by default in ARE state.
x - Turn on extended mode: In extended mode, whitespace characters and content after the comment character '#' in the expression will be ignored
For example:
@code@
(?x)
s+ ([[:graph:]]+) # first number
s+ ([[:graph:]]+) # second number
@code@
Equivalent to "s+([[:graph:]]+)s+([[:graph:]]+)".
T -Close the extension mode without ignoring the content of the blank and notes. This mode is used by default in ARE state.
6. Perl-style character class escape sequence different from BRE/ERE mode:
perl class Equivalent POSIX expression Description
-------------------------------------------------- --------------------------
a - Bell character
A - No matter what the current mode is, only the beginning of the entire string is matched
b - Backspace character ('x08')
B - The escape character itself ('')
cX - Control symbol-X (= X & 037)
D [[: Digit:]] 10 -in -made numbers ('0' -'9')
D [^[:digit:]]
e - Exit character ('x1B')
f - Form feed ('x0C')
m [[:<: word starting position>
M [[:>:]] Since
n - Newline character ('x0A')
r - Carriage return character ('x0D')
s [[:space:]]
S [^[:space:]]
t Tab character ('x09')
uX - 16-bit UNICODE characters (X∈[0000 .. FFFF])
UX - 32-bit UNICODE characters (X∈[00000000 .. FFFFFFFF])
v - Vertical tab character ('x0B')
w [[:alnum:]_] Characters that make up words
W [^[:alnum:]_] Non-word characters
xX - 8-bit characters (X∈[00 .. FF])
y - Word boundary (m or M)
Y - Non-word boundary
Z - No matter what the current mode is, only the last
of the entire string is matched.
- NULL, empty character
X - Subexpression forward reference (X∈[1 .. 9])
XX - Subexpression forward reference or 8-character octal representation
XXX - Subexpression forward reference or 8-character octal representation

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

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

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

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

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

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

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

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


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

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

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Linux new version
SublimeText3 Linux latest version

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