(PHP 4, PHP 5, PHP 7)
preg_match — 执锟斤拷匹锟斤拷锟斤拷锟斤拷锟斤拷式
$pattern
, string $subject
[, array &$matches
[, int $flags
= 0
[, int $offset
= 0
]]] ) : int
锟斤拷锟斤拷subject
锟斤拷pattern
锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷式锟斤拷一锟斤拷匹锟斤拷.
pattern
要锟斤拷锟斤拷锟斤拷模式锟斤拷锟街凤拷锟斤拷锟斤拷锟酵★拷
subject
锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷
matches
锟斤拷锟斤拷峁╋拷瞬锟斤拷锟�matches
锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟轿拷锟斤拷锟斤拷锟斤拷锟斤拷
$matches[0]锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷模式匹锟戒到锟斤拷锟侥憋拷锟斤拷 $matches[1]
锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷匹锟戒到锟斤拷锟侥憋拷锟斤拷锟皆达拷锟斤拷锟狡★拷
flags
flags
锟斤拷锟皆憋拷锟斤拷锟斤拷为锟斤拷锟铰憋拷锟街碉拷锟�
PREG_OFFSET_CAPTURE
锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷牵锟斤拷锟斤拷锟矫恳伙拷锟斤拷锟斤拷值锟狡ワ拷浞碉拷锟绞憋拷岣斤拷锟斤拷址锟斤拷锟狡拷锟斤拷锟�(锟斤拷锟斤拷锟侥匡拷锟斤拷址锟斤拷锟斤拷锟�)锟斤拷
注锟解:锟斤拷锟侥憋拷锟斤拷涞�matches
锟斤拷锟斤拷锟斤拷锟斤拷锟介,使锟斤拷每锟斤拷元锟截筹拷为一锟斤拷锟斤拷
锟斤拷0锟斤拷元锟斤拷锟斤拷匹锟戒到锟斤拷锟街凤拷锟斤拷锟斤拷锟斤拷1锟斤拷元锟斤拷锟角革拷匹锟斤拷锟街凤拷锟斤拷
锟斤拷目锟斤拷锟街凤拷锟斤拷subject
锟叫碉拷偏锟斤拷锟斤拷锟斤拷
<?php
preg_match('/(foo)(bar)(baz)/', 'foobarbaz', $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
?>
锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�
Array ( [0] => Array ( [0] => foobarbaz [1] => 0 ) [1] => Array ( [0] => foo [1] => 0 ) [2] => Array ( [0] => bar [1] => 3 ) [3] => Array ( [0] => baz [1] => 6 ) )
offset
通锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷目锟斤拷锟街凤拷锟斤拷锟侥匡拷始位锟矫匡拷始锟斤拷锟斤拷选锟斤拷锟斤拷 offset
锟斤拷锟斤拷
指锟斤拷锟斤拷目锟斤拷锟街凤拷锟斤拷锟斤拷某锟斤拷位锟矫匡拷始锟斤拷锟斤拷(锟斤拷位锟斤拷锟街斤拷)锟斤拷
Note:
使锟斤拷
offset
锟斤拷锟斤拷锟斤拷同锟斤拷锟斤拷preg_match() 锟斤拷锟捷帮拷锟斤拷位锟斤拷通锟斤拷substr($subject, $offset)锟斤拷取目锟斤拷锟街凤拷锟斤拷锟斤拷锟斤拷锟� 锟斤拷为pattern
锟斤拷锟皆帮拷锟斤拷锟斤拷锟皆憋拷锟斤拷^锟斤拷 $ 锟斤拷锟斤拷(?<=x)锟斤拷 锟饺较o拷<?php
$subject = "abcdef";
$pattern = '/^def/';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3);
print_r($matches);
?>锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�
Array ( )锟斤拷锟斤拷锟绞撅拷锟绞癸拷媒锟饺★拷蟠锟绞�
<?php
$subject = "abcdef";
$pattern = '/^def/';
preg_match($pattern, substr($subject,3), $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
?>锟斤拷锟斤拷锟斤拷锟狡ワ拷锟�
Array ( [0] => Array ( [0] => def [1] => 0 ) )
preg_match()锟斤拷锟斤拷 pattern
锟斤拷匹锟斤拷锟斤拷锟斤拷锟�
锟斤拷锟斤拷值锟斤拷锟斤拷0锟轿o拷锟斤拷匹锟戒)锟斤拷1锟轿o拷锟斤拷为preg_match()锟节碉拷一锟斤拷匹锟斤拷锟�
锟斤拷锟斤拷停止锟斤拷锟斤拷锟斤拷preg_match_all()锟斤拷同锟节此o拷锟斤拷锟斤拷一直锟斤拷锟斤拷subject
直锟斤拷锟斤拷锟斤拷锟轿诧拷锟�
锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟�preg_match()锟斤拷锟斤拷 FALSE
锟斤拷
锟芥本 | 说锟斤拷 |
---|---|
5.3.6 |
锟斤拷锟� offset 锟斤拷 subject 锟侥筹拷锟饺伙拷要锟斤拷锟津返伙拷 FALSE 锟斤拷
|
5.2.2 | 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷越锟斤拷锟�(?<name>)锟斤拷 (?'name') 锟皆硷拷(?P<name>)锟斤法锟斤拷之前锟芥本锟斤拷锟斤拷锟斤拷(?P<name>)锟斤法锟斤拷 |
Example #1 锟斤拷锟斤拷锟侥憋拷锟街凤拷锟斤拷"php"
<?php
//模式锟街革拷锟斤拷锟斤拷锟�"i"锟斤拷锟斤拷锟斤拷锟揭伙拷锟斤拷锟叫⌒达拷锟斤拷锟斤拷械锟斤拷锟斤拷锟�
if (preg_match("/php/i", "PHP is the web scripting language of choice.")) {
echo "A match was found.";
} else {
echo "A match was not found.";
}
?>
Example #2 锟斤拷锟揭碉拷锟斤拷"word"
<?php
/* 模式锟叫碉拷\b锟斤拷锟揭伙拷锟斤拷锟斤拷时呓纾拷锟斤拷锟街伙拷卸锟斤拷锟斤拷牡锟斤拷锟�"web"锟结被匹锟戒,锟斤拷锟斤拷锟斤拷匹锟斤拷
* 锟斤拷锟绞的诧拷锟斤拷锟斤拷锟捷憋拷锟斤拷"webbing" 锟斤拷 "cobweb" */
if (preg_match("/\bweb\b/i", "PHP is the web scripting language of choice.")) {
echo "A match was found.";
} else {
echo "A match was not found.";
}
if (preg_match("/\bweb\b/i", "PHP is the website scripting language of choice.")) {
echo "A match was found.";
} else {
echo "A match was not found.";
}
?>
Example #3 锟斤拷取URL锟叫碉拷锟斤拷锟斤拷
<?php
//锟斤拷URL锟叫伙拷取锟斤拷锟斤拷锟斤拷锟斤拷
preg_match('@^(?:http://)?([^/]+)@i',
"http://www.php.net/index.html", $matches);
$host = $matches[1];
//锟斤拷取锟斤拷锟斤拷锟斤拷锟狡的猴拷锟斤拷锟斤拷锟斤拷锟斤拷
preg_match('/[^.]+\.[^.]+$/', $host, $matches);
echo "domain name is: {$matches[0]}\n";
?>
锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�
domain name is: php.net
Example #4 使锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
<?php
$str = 'foobar: 2008';
preg_match('/(?P<name>\w+): (?P<digit>\d+)/', $str, $matches);
/* 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷php 5.2.2(pcre 7.0)锟斤拷锟斤拷掳姹撅拷鹿锟斤拷锟�, 然锟斤拷, 为锟剿猴拷锟斤拷锟斤拷锟�, 锟斤拷锟斤拷姆锟绞斤拷锟斤拷萍锟叫达拷锟�. */
// preg_match('/(?<name>\w+): (?<digit>\d+)/', $str, $matches);
print_r($matches);
?>
锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�
Array ( [0] => foobar: 2008 [name] => foobar [1] => foobar [digit] => 2008 [2] => 2008 )
锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷要锟斤拷锟侥筹拷锟斤拷址锟斤拷锟斤拷欠锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷锟街凤拷锟斤拷锟斤拷锟斤拷要使锟斤拷preg_match()锟斤拷 使锟斤拷 strpos() 锟斤拷锟斤拷臁�