最近在学习正则,一些比较有用的东西怕忘记,记下来,比较乱,想一条记录一条:
1.
2.
$str
=
'[a-z]'
;
3.
$str
= preg_replace(
'/\G[a-z]\E/'
,
''
,
$str
);
4.
echo
$str
;
1.
2.
$str
=
'abc123abc'
;
3.
preg_match(
'/(?P<num>\d+)/'
,
$str
,
$arr
);
4.
echo
$arr
[
'num'
];
1.
2.
$str
=
'abc123abc'
;
3.
preg_match(
'/abc(?:\d+)/'
,
$str
,
$arr
);
4.
echo
$arr
[1];
1.
2.
$str
=
'fdfad123456789fdfd'
;
3.
$str
= preg_replace(
'/(?<=\d)(?=(\d{3})+(?!\d))/'
,
','
,
$str
);
4.
echo
$str
;
1.
2.
$str
= 123456;
3.
preg_match(
'/\d+/'
,
$str
,
$arr
);
4.
echo
$arr
[0];
5.
preg_match(
'/\d+?/'
,
$str
,
$arr
);
6.
echo
$arr
[0];
01.
02.
$str
= <<<HTML
03.
<font size=12></font>
04.
<font size=
'13'
></font>
05.
<font size=
"14"
></font>
06.
<font size="15></font>
07.
HTML;
08.
preg_match_all(
'/<font\s+size=([\'"]?)(\d+)\1[^>]*>/'
,
$str
,
$arr
);
09.
print_r(
$arr
);
10.
11.
12.
13.
14.
15.
16.
17.
01.
02.
03.
$str
=
'<b style="COLOR:red"></b><b STYLE="color:blue"></b><b style="color:green"></b>'
;
04.
preg_match_all(
'/style=([\'"]?)(?i)color:(\w+)\1(?-i)/'
,
$str
,
$arr
);
05.
print_r(
$arr
[2])
06.
07.
08.
09.
$str
=
'<B>Style</B>'
;
10.
preg_match(
'/<B>(?i:style)<\/B>/'
,
$str
,
$arr
);
11.
print_r(
$arr
);
12.
$str
=
'<B>Style</b>'
;
13.
preg_match(
'/<B>(?i:style)<\/B>/'
,
$str
,
$arr
);
14.
print_r(
$arr
);
1.
2.
$str
=
'I\'m a teacher'
;
3.
preg_match_all(
'/\b[a-z]+\b/i'
,
$str
,
$arr
);
4.
print_r(
$arr
);
01.
02.
$str
=
'你您'
;
03.
$str
= preg_replace(
'/[你您]/'
,
'you'
,
$str
);
04.
echo
$str
;
05.
06.
07.
$str
= iconv(
'gb2312'
,
'utf-8'
,
'你您'
);
08.
$regex
= iconv(
'gb2312'
,
'utf-8'
,
'/[你您]/u'
);
09.
$str
= preg_replace(
$regex
,
'you'
,
$str
);
10.
echo
$str
;
1.
2.
$str
=
'test Test'
;
3.
preg_match(
'/test #只匹配小写的test/x'
,
$str
,
$arr
);
4.
print_r(
$arr
);
01.
02.
$str
=
'test <B>test1<B> test2</B>'
;
03.
preg_match(
'/<B>(?:.(?<!<B>))*<\/B>/i'
,
$str
,
$arr
);
04.
05.
print_r(
$arr
)
06.
07.
08.
09.
$str
=
'test [b]test1[b] test2[/b] test3[/b]test'
;
10.
$str
= preg_replace(
'/\[B\]((?:(?!\[\/?B\]).)*)\[\/B\]/i'
,
'<b>\1</b>'
,
$str
);
11.
$str
= preg_replace(
'/\[B\]((?:(?!\[\/?B\]).)*)\[\/B\]/i'
,
'<b>\1</b>'
,
$str
);
12.
print_r(
$str
);
1.
2.
$str
=
'Subject'
;
3.
preg_match(
'/(\w+):/'
,
$str
,
$arr
);
4.
5.
6.
preg_match(
'/(?>\w+):/'
,
$str
,
$arr
);