PCRE 函数 业 ,精于勤 荒于嬉.
- PCRE 函数 preg_filter 执行一个正则表达式搜索和替换
-
发表日期:2021-07-01 08:56:55 | 来源: | 分类:PCRE 函数
-
示例1
1
<?php
2
$subject
=
array
(
'1'
,
'a'
,
'2'
,
'b'
,
'3'
,
'A'
,
'B'
,
'4'
);
3
$pattern
=
array
(
'/\d/'
,
'/[a-z]/'
,
'/[1a]/'
);
4
$replace
=
array
(
'A:$0'
,
'B:$0'
,
'C:$0'
);
5
echo
"preg_filter returns\n"
;
6
print_r(preg_filter(
$pattern
,
$replace
,
$subject
));
7
echo
"preg_replace returns\n"
;
8
print_r(preg_replace(
$pattern
,
$replace
,
$subject
));
9
?>
- PCRE 函数 preg_grep 返回匹配模式的数组条目
-
发表日期:2021-07-01 08:56:56 | 来源: | 分类:PCRE 函数
-
示例1
1
<?php
2
// 返回所有包含浮点数的元素$fl_array = preg_grep("/^(\d+)?\.\d+$/", $array);
3
?>
- PCRE 函数 preg_last_error_msg Returns the error message of the last PCRE regex execution
-
发表日期:2021-07-01 08:56:56 | 来源: | 分类:PCRE 函数
-
示例1
1
<?php
2
preg_match(
'/(?:\D+|<\d+>)*[!?]/'
,
'foobar foobar foobar'
);
3
if
(preg_last_error() !== PREG_NO_ERROR) {
4
echo
preg_last_error_msg();
5
}
6
?>
- PCRE 函数 preg_last_error 返回最后一个PCRE正则执行产生的错误代码
-
发表日期:2021-07-01 08:56:55 | 来源: | 分类:PCRE 函数
-
示例1
1
<?php
2
preg_match(
'/(?:\D+|<\d+>)*[!?]/'
,
'foobar foobar foobar'
);
3
if
(preg_last_error() == PREG_BACKTRACK_LIMIT_ERROR) {
4
print
'Backtrack limit was exhausted!'
;
5
}
6
?>
- PCRE 函数 preg_match_all 执行一个全局正则表达式匹配
-
发表日期:2021-07-01 08:56:56 | 来源: | 分类:PCRE 函数
-
示例1
1
<?php
2
preg_match_all(
"|<[^>]+>(.*)</[^>]+>|U"
,
"<b>example: </b><div align=left>this is a test</div>"
,
$out
, PREG_PATTERN_ORDER);
3
echo
$out
[0][0] .
", "
.
$out
[0][1] .
"\n"
;
4
echo
$out
[1][0] .
", "
.
$out
[1][1] .
"\n"
;
5
?>
示例2
1
<?php
2
preg_match_all(
'/(?J)(?<match>foo)|(?<match>bar)/'
,
'foo bar'
,
$matches
, PREG_PATTERN_ORDER);
3
print_r(
$matches
[
'match'
]);
4
?>
示例3
1
<?php
2
preg_match_all(
"|<[^>]+>(.*)</[^>]+>|U"
,
"<b>example: </b><div align=\"left\">this is a test</div>"
,
$out
, PREG_SET_ORDER);
3
echo
$out
[0][0] .
", "
.
$out
[0][1] .
"\n"
;
4
echo
$out
[1][0] .
", "
.
$out
[1][1] .
"\n"
;
5
?>
示例4
1
<?php
2
preg_match_all(
'/(foo)(bar)(baz)/'
,
'foobarbaz'
,
$matches
, PREG_OFFSET_CAPTURE);
3
print_r(
$matches
);
4
?>
示例5
1
<?php
2
preg_match_all("/\(? (\d{
3
3}
4
)? \)? (?(1) [\-\s] ) \d{
5
3}
6
-\d{
7
4}
8
/x
", "
Call 555-1212
or
1-800-555-1212",
$phones
);
9
?>
示例6
01
<?php
02
//\\2是一个后向引用的示例. 这会告诉pcre它必须匹配正则表达式中第二个圆括号(这里是([\w]+))//匹配到的结果. 这里使用两个反斜线是因为这里使用了双引号.$html = "<b>bold text</b><a href=howdy.html>click me</a>";
03
preg_match_all(
"/(<([\w]+)[^>]*>)(.*?)(<\/\\2>)/"
,
$html
,
$matches
, PREG_SET_ORDER);
04
foreach
(
$matches
as
$val
) {
05
echo
"matched: "
.
$val
[0] .
"\n"
;
06
echo
"part 1: "
.
$val
[1] .
"\n"
;
07
echo
"part 2: "
.
$val
[2] .
"\n"
;
08
echo
"part 3: "
.
$val
[3] .
"\n"
;
09
echo
"part 4: "
.
$val
[4] .
"\n\n"
;
10
}
11
?>
示例7
1
<?php
2
$str
= <<<FOOa: 1b: 2c: 3FOO;
3
preg_match_all(
'/(?P<name>\w+): (?P<digit>\d+)/'
,
$str
,
$matches
);
4
/* 选择方式 */
5
// preg_match_all('/(?<name>\w+): (?<digit>\d+)/', $str, $matches);
6
print_r(
$matches
);
7
?>
- PCRE 函数 preg_match 执行匹配正则表达式
-
发表日期:2021-07-01 08:56:55 | 来源: | 分类:PCRE 函数
-
示例1
1
<?php
2
preg_match(
'/(foo)(bar)(baz)/'
,
'foobarbaz'
,
$matches
, PREG_OFFSET_CAPTURE);
3
print_r(
$matches
);
4
?>
示例2
1
<?php
2
preg_match(
'/(a)(b)*(c)/'
,
'ac'
,
$matches
);
3
var_dump(
$matches
);
4
preg_match(
'/(a)(b)*(c)/'
,
'ac'
,
$matches
, PREG_UNMATCHED_AS_NULL);
5
var_dump(
$matches
);
6
?>
示例3
1
<?php
2
$subject
=
"abcdef"
;
3
$pattern
=
'/^def/'
;
4
preg_match(
$pattern
,
$subject
,
$matches
, PREG_OFFSET_CAPTURE, 3);
5
print_r(
$matches
);
6
?>
示例4
1
<?php
2
$subject
=
"abcdef"
;
3
$pattern
=
'/^def/'
;
4
preg_match(
$pattern
,
substr
(
$subject
,3),
$matches
, PREG_OFFSET_CAPTURE);
5
print_r(
$matches
);
6
?>
示例5
1
<?php
2
//模式分隔符后的"i"标记这是一个大小写不敏感的搜索if (preg_match("/php/i", "PHP is the web scripting language of choice.")) {
3
echo
"A match was found."
;
4
}
5
else
{
6
echo
"A match was not found."
;
7
}
8
?>
示例6
01
<?php
02
/* 模式中的\b标记一个单词边界,所以只有独立的单词"web"会被匹配,而不会匹配 * 单词的部分内容比如"webbing" 或 "cobweb" */
03
if
(preg_match(
"/\bweb\b/i"
,
"PHP is the web scripting language of choice."
)) {
04
echo
"A match was found."
;
05
}
06
else
{
07
echo
"A match was not found."
;
08
}
09
if
(preg_match(
"/\bweb\b/i"
,
"PHP is the website scripting language of choice."
)) {
10
echo
"A match was found."
;
11
}
12
else
{
13
echo
"A match was not found."
;
14
}
15
?>
示例7
1
<?php
2
//从URL中获取主机名称preg_match('@^(?:http://)?([^/]+)@i', "http://www.php.net/index.html", $matches);
3
$host
=
$matches
[1];
4
//获取主机名称的后面两部分preg_match('/[^.]+\.[^.]+$/', $host, $matches);
5
echo
"domain name is: {
6
$matches
[0]}
7
\n";
8
?>
示例8
1
<?php
2
$str
=
'foobar: 2008'
;
3
preg_match(
'/(?P<name>\w+): (?P<digit>\d+)/'
,
$str
,
$matches
);
4
/* 可选的方式 */
5
// preg_match('/(?<name>\w+): (?<digit>\d+)/', $str, $matches);
6
print_r(
$matches
);
7
?>
- PCRE 函数 preg_quote 转义正则表达式字符
-
发表日期:2021-07-01 08:56:55 | 来源: | 分类:PCRE 函数
-
示例1
1
<?php
2
$keywords
=
'$40 for a g3/400'
;
3
$keywords
= preg_quote(
$keywords
,
'/'
);
4
echo
$keywords
;
5
// 返回 \$40 for a g3\/400?>
示例2
1
<?php
2
//在这个例子中,preg_quote($word) 用于保持星号原文涵义,使其不使用正则表达式中的特殊语义。$textbody = "This book is *very* difficult to find.";
3
$word
=
"*very*"
;
4
$textbody
= preg_replace (
"/"
. preg_quote(
$word
,
'/'
) .
"/"
,
"<i>"
.
$word
.
"</i>"
,
$textbody
);
5
?>
- PCRE 函数 preg_replace_callback_array Perform a regular expression search and replace using callbacks
-
发表日期:2021-07-01 08:56:56 | 来源: | 分类:PCRE 函数
-
示例1
01
<?php
02
$subject
=
'Aaaaaa Bbb'
;
03
preg_replace_callback_array( [
'~[a]+~i'
=>
function
(
$match
) {
04
echo
strlen
(
$match
[0]),
' matches for "a" found'
, PHP_EOL;
05
}
06
,
'~[b]+~i'
=>
function
(
$match
) {
07
echo
strlen
(
$match
[0]),
' matches for "b" found'
, PHP_EOL;
08
}
09
],
$subject
);
10
?>
- PCRE 函数 preg_replace_callback 执行一个正则表达式搜索并且使用一个回调进行替换
-
发表日期:2021-07-01 08:56:56 | 来源: | 分类:PCRE 函数
-
示例1
01
<?php
02
/* 一个unix样式的命令行过滤器,用于将段落开始部分的大写字母转换为小写。 */
03
$fp
=
fopen
(
"php://stdin"
,
"r"
)
or
die
(
"can't read stdin"
);
04
while
(!
feof
(
$fp
)) {
05
$line
=
fgets
(
$fp
);
06
$line
= preg_replace_callback(
'|<p>\s*\w|'
,
function
(
$matches
) {
07
return
strtolower
(
$matches
[0]);
08
}
09
,
$line
);
10
echo
$line
;
11
}
12
fclose(
$fp
);
13
?>
示例2
01
<?php
02
// 将文本中的年份增加一年.$text = "April fools day is 04/01/2002\n";
03
$text
.=
"Last christmas was 12/24/2001\n"
;
04
// 回调函数function next_year($matches){
05
// 通常: $matches[0]是完成的匹配 // $matches[1]是第一个捕获子组的匹配 // 以此类推 return $matches[1].($matches[2]+1);
06
}
07
echo
preg_replace_callback( "|(\d{
08
2}
09
/\d{
10
2}
11
/)(\d{
12
4}
13
)|
", "
next_year",
$text
);
14
?>
示例3
01
<?php
02
$input
=
"plain [indent] deep [indent] deeper [/indent] deep [/indent] plain"
;
03
function
parseTagsRecursive(
$input
){
04
/* 译注: 对此正则表达式分段分析 * 首尾两个#是正则分隔符 * \[indent] 匹配一个原文的[indent] * ((?:[^[]|\[(?!/?indent])|(?R))+)分析: * (?:[^[]|\[(?!/?indent])分析: * 首先它是一个非捕获子组 * 两个可选路径, 一个是非[字符, 另一个是[字符但后面紧跟着不是/indent或indent. * (?R) 正则表达式递归 * \[/indent] 匹配结束的[/indent] * /
$regex
=
'#\[indent]((?:[^[]|\[(?!/?indent])|(?R))+)\[/indent]#'
;
05
if
(
is_array
(
$input
)) {
06
$input
=
'<div style="margin-left: 10px">'
.
$input
[1].
'</div>'
;
07
}
08
return
preg_replace_callback(
$regex
,
'parseTagsRecursive'
,
$input
);
09
}
10
$output
= parseTagsRecursive(
$input
);
11
echo
$output
;
12
?>
- PCRE 函数 preg_replace 执行一个正则表达式的搜索和替换
-
发表日期:2021-07-01 08:56:56 | 来源: | 分类:PCRE 函数
-
示例1
1
<?php
2
$string
=
'April 15, 2003'
;
3
$pattern
=
'/(\w+) (\d+), (\d+)/i'
;
4
$replacement
= '${
5
1}
6
1,
$3
';
7
echo
preg_replace(
$pattern
,
$replacement
,
$string
);
8
?>
示例2
01
<?php
02
$string
=
'The quick brown fox jumps over the lazy dog.'
;
03
$patterns
=
array
();
04
$patterns
[0] =
'/quick/'
;
05
$patterns
[1] =
'/brown/'
;
06
$patterns
[2] =
'/fox/'
;
07
$replacements
=
array
();
08
$replacements
[2] =
'bear'
;
09
$replacements
[1] =
'black'
;
10
$replacements
[0] =
'slow'
;
11
echo
preg_replace(
$patterns
,
$replacements
,
$string
);
12
?>
示例3
1
<?php
2
ksort(
$patterns
);
3
ksort(
$replacements
);
4
echo
preg_replace(
$patterns
,
$replacements
,
$string
);
5
?>
示例4
01
<?php
02
$patterns
=
array
('/(19|20)(\d{
03
2}
04
)-(\d{
05
1,2}
06
)-(\d{
07
1,2}
08
)/
', '
/^\s*{
09
(\w+)}
10
\s*=/');
11
$replace
=
array
(
'\3/\4/\1\2'
,
'$\1 ='
);
12
echo
preg_replace(
$patterns
,
$replace
, '{
13
startDate}
14
= 1999-5-27');
15
?>
示例5
1
<?php
2
$str
=
'foo o'
;
3
$str
= preg_replace(
'/\s\s+/'
,
' '
,
$str
);
4
// 将会改变为'foo o'echo $str;
5
?>
示例6
1
<?php
2
$count
= 0;
3
echo
preg_replace(
array
(
'/\d/'
,
'/\s/'
),
'*'
,
'xp 4 to'
, -1 ,
$count
);
4
echo
$count
;
5
//3?>
- PCRE 函数 preg_split 通过一个正则表达式分隔字符串
-
发表日期:2021-07-01 08:56:56 | 来源: | 分类:PCRE 函数
-
示例1
1
<?php
2
//使用逗号或空格(包含" ", \r, \t, \n, \f)分隔短语$keywords = preg_split("/[\s,]+/", "hypertext language, programming");
3
print_r(
$keywords
);
4
?>
示例2
1
<?php
2
$str
=
'string'
;
3
$chars
= preg_split(
'//'
,
$str
, -1, PREG_SPLIT_NO_EMPTY);
4
print_r(
$chars
);
5
?>
示例3
1
<?php
2
$str
=
'hypertext language programming'
;
3
$chars
= preg_split(
'/ /'
,
$str
, -1, PREG_SPLIT_OFFSET_CAPTURE);
4
print_r(
$chars
);
5
?>
- PHP杂项(34)
- PHP基础-李炎恢系列课程(20)
- 中文函数手册(0)
- 错误处理 函数(13)
- OPcache 函数(6)
- PHP 选项/信息 函数(54)
- Zip 函数(10)
- Hash 函数(15)
- OpenSSL 函数(63)
- Date/Time 函数(51)
- 目录函数(9)
- Fileinfo 函数(6)
- iconv 函数(11)
- 文件系统函数(81)
- 多字节字符串 函数(57)
- GD 和图像处理 函数(114)
- 可交换图像信息(5)
- Math 函数(50)
- 程序执行函数(11)
- PCNTL 函数(23)
- JSON 函数(4)
- SPL 函数(15)
- URL 函数(10)
- cURL 函数(32)
- 网络 函数(33)
- FTP 函数(36)
- Session 函数(23)
- PCRE 函数(11)
- PCRE 正则语法(19)
- 数组 函数(81)
- 类/对象 函数(18)
- 函数处理 函数(13)
- 变量处理 函数(37)
- SimpleXML 函数(3)
- 杂项 函数(31)
- 字符串 函数(101)