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


Fatal error: Uncaught JSMin_UnterminatedStringException: JSMin: Unterminated String at byte 20: 't work:</p> in /var/www/www-ars1963/data/www/farminfoservice.ru/wp-content/plugins/autoptimize/classes/external/php/jsmin.php:214 Stack trace: #0 /var/www/www-ars1963/data/www/farminfoservice.ru/wp-content/plugins/autoptimize/classes/external/php/jsmin.php(152): JSMin->action(1) #1 /var/www/www-ars1963/data/www/farminfoservice.ru/wp-content/plugins/autoptimize/classes/external/php/jsmin.php(86): JSMin->min() #2 /var/www/www-ars1963/data/www/farminfoservice.ru/wp-content/plugins/autoptimize/classes/autoptimizeSpeedupper.php(38): JSMin::minify('but didn't work...') #3 /var/www/www-ars1963/data/www/farminfoservice.ru/wp-includes/class-wp-hook.php(324): autoptimizeSpeedupper->js_snippetcacher('but didn't work...', '') #4 /var/www/www-ars1963/data/www/farminfoservice.ru/wp-includes/plugin.php(205): WP_Hook->apply_filters('but didn't work...', Array) #5 /var/www/www-ars1963/data/www/farminfoservice.ru/wp-content/plugins/autoptimize/ in /var/www/www-ars1963/data/www/farminfoservice.ru/wp-content/plugins/autoptimize/classes/external/php/jsmin.php on line 214