php preg match array
preg_match
(PHP 4, PHP 5, PHP 7, PHP 8)
preg_match — Выполняет проверку на соответствие регулярному выражению
Описание
Список параметров
Искомый шаблон в виде строки.
flags может быть комбинацией следующих флагов: PREG_OFFSET_CAPTURE
Результат выполнения данного примера:
PREG_UNMATCHED_AS_NULL
Если этот флаг передан, несовпадающие подмаски будут представлены значениями null ; в противном случае они отображаются в виде пустых строк ( string ).
Результат выполнения данного примера:
Обычно поиск осуществляется слева направо, с начала строки. Можно использовать дополнительный параметр offset для указания альтернативной начальной позиции для поиска (в байтах).
Результат выполнения данного примера:
В то время как этот пример
Возвращаемые значения
Список изменений
Примеры
Пример #1 Поиск подстроки «php» в тексте
Пример #2 Поиск слова «web» в тексте
Пример #3 Извлечение доменного имени из URL
Результат выполнения данного примера:
Пример #4 Использование именованных подмасок
Результат выполнения данного примера:
Примечания
Смотрите также
User Contributed Notes 51 notes
Regex quick reference
[abc] A single character: a, b or c
[^abc] Any single character but a, b, or c
[a-z] Any single character in the range a-z
[a-zA-Z] Any single character in the range a-z or A-Z
^ Start of line
$ End of line
\A Start of string
\z End of string
. Any single character
\s Any whitespace character
\S Any non-whitespace character
\d Any digit
\D Any non-digit
\w Any word character (letter, number, underscore)
\W Any non-word character
\b Any word boundary character
(. ) Capture everything enclosed
(a|b) a or b
a? Zero or one of a
a* Zero or more of a
a+ One or more of a
a <3>Exactly 3 of a
a <3,>3 or more of a
a <3,6>Between 3 and 6 of a
options: i case insensitive m make dot match newlines x ignore whitespace in regex o perform # <. >substitutions only once
I noticed that in order to deal with UTF-8 texts, without having to recompile php with the PCRE UTF-8 flag enabled, you can just add the following sequence at the start of your pattern: (*UTF8)
for instance : ‘#(*UTF8)[[:alnum:]]#’ will return TRUE for ‘é’ where ‘#[[:alnum:]]#’ will return FALSE
found this very very useful tip after hours of research over the web directly in pcre website right here : http://www.pcre.org/pcre.txt
there are many further informations about UTF-8 support in the lib
hop that will help!
Sometimes its useful to negate a string. The first method which comes to mind to do this is: [^(string)] but this of course won’t work. There is a solution, but it is not very well known. This is the simple piece of code on how a negation of a string is done:
Hope this helps some ppl.
Was working on a site that needed japanese and alphabetic letters and needed to
validate input using preg_match, I tried using \p