(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 ����
ָ����Ŀ���ַ�����ij��λ�ÿ�ʼ����(��λ���ֽ�)��
Note:
ʹ��
offset������ͬ����preg_match() ���ݰ���λ��ͨ��substr($subject, $offset)��ȡĿ���ַ�������� ��Ϊpattern���������Ա���^�� $ ����(?<=x)�� �Ƚϣ�<?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�Σ���ƥ�䣩��1�Σ���Ϊpreg_match()�ڵ�һ��ƥ���
����ֹͣ������preg_match_all()��ͬ�ڴˣ�����һֱ����subject
ֱ�������β��
�����������preg_match()���� FALSE��
| �汾 | ˵�� |
|---|---|
| 5.3.6 |
��� offset �� subject �ij��Ȼ�Ҫ���� 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"�ᱻƥ�䣬������ƥ��
* ���ʵIJ������ݱ���"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
)
����������Ҫ���ij���ַ����Ƿ��������һ���ַ�������Ҫʹ��preg_match()�� ʹ�� strpos() ����졣