There is a possibility to use \p{xx} and \P{xx} escape sequences with script names.
From http://www.pcre.org/pcre.txt
When PCRE is built with Unicode character property support, three addi-
tional escape sequences that match characters with specific properties
are available. When not in UTF-8 mode, these sequences are of course
limited to testing characters whose codepoints are less than 256, but
they do work in this mode. The extra escape sequences are:
\p{xx} a character with the xx property
\P{xx} a character without the xx property
\X an extended Unicode sequence
The property names represented by xx above are limited to the Unicode
script names, the general category properties, "Any", which matches any
character (including newline), and some special PCRE properties
(described in the next section). Other Perl properties such as "InMu-
sicalSymbols" are not currently supported by PCRE. Note that \P{Any}
does not match any characters, so always causes a match failure.
Sets of Unicode characters are defined as belonging to certain scripts.
A character from one of these sets can be matched using a script name.
For example:
\p{Greek}
\P{Han}
Those that are not part of an identified script are lumped together as
"Common". The current list of scripts is:
Arabic, Armenian, Avestan, Balinese, Bamum, Bengali, Bopomofo, Braille,
Buginese, Buhid, Canadian_Aboriginal, Carian, Cham, Cherokee, Common,
Coptic, Cuneiform, Cypriot, Cyrillic, Deseret, Devanagari, Egyp-
tian_Hieroglyphs, Ethiopic, Georgian, Glagolitic, Gothic, Greek,
Gujarati, Gurmukhi, Han, Hangul, Hanunoo, Hebrew, Hiragana, Impe-
rial_Aramaic, Inherited, Inscriptional_Pahlavi, Inscriptional_Parthian,
Javanese, Kaithi, Kannada, Katakana, Kayah_Li, Kharoshthi, Khmer, Lao,
Latin, Lepcha, Limbu, Linear_B, Lisu, Lycian, Lydian, Malayalam,
Meetei_Mayek, Mongolian, Myanmar, New_Tai_Lue, Nko, Ogham, Old_Italic,
Old_Persian, Old_South_Arabian, Old_Turkic, Ol_Chiki, Oriya, Osmanya,
Phags_Pa, Phoenician, Rejang, Runic, Samaritan, Saurashtra, Shavian,
Sinhala, Sundanese, Syloti_Nagri, Syriac, Tagalog, Tagbanwa, Tai_Le,
Tai_Tham, Tai_Viet, Tamil, Telugu, Thaana, Thai, Tibetan, Tifinagh,
Ugaritic, Vai, Yi.
Each character has exactly one Unicode general category property, spec-
ified by a two-letter abbreviation. For compatibility with Perl, nega-
tion can be specified by including a circumflex between the opening
brace and the property name. For example, \p{^Lu} is the same as
\P{Lu}.
If only one letter is specified with \p or \P, it includes all the gen-
eral category properties that start with that letter. In this case, in
the absence of negation, the curly brackets in the escape sequence are
optional; these two examples have the same effect:
\p{L}
\pL
Propiedades de los caracteres Unicode
A partir de 5.1.0, están disponibles tres secuencias de escape adicionales para comparar tipos de caracteres genéricos cuando el modo UTF-8 está seleccionado. Son:
- \p{xx}
- un carácter con la propiedad xx
- \P{xx}
- un carácter sin la propiedad xx
- \X
- una secuencia Unicode extendida
Los nombres de las propiedades representadas arriba por xx están limitadas a las propiedades de la categoría general de Unicode. Cada carácter tiene exactamente una propiedad, especificada por una abreviatura de dos letras. Por compatibilidad con Perl, la negación se puede especificar incluyendo un acento circunflejo entre la llave de apertura y el nombre de la propiedad. Por ejemplo, \p{^Lu} es lo mismo que \P{Lu}.
Si sólo se especifica una letra con \p o \P, se incluyen todas las propiedades que comienzan con esa letra. En este caso, en la ausencia de negación, las llaves en la secuencia de escape son opcionales; estos dos ejemplos tienen el mismo efecto:
\p{L}
\pL
| Propiedad | Coincidencias | Notas |
|---|---|---|
| C | Otro | |
| Cc | Control | |
| Cf | Formato | |
| Cn | Sin asignar | |
| Co | Uso privado | |
| Cs | Sustituto | |
| L | Letra | Incluye las siguientes propiedades: Ll, Lm, Lo, Lt y Lu. |
| Ll | Letra minúscula | |
| Lm | Letra modificadora | |
| Lo | Otra letra | |
| Lt | Letra de título | |
| Lu | Letra mayúscula | |
| M | Marca | |
| Mc | Marca de espacio | |
| Me | Marca de cierre | |
| Mn | Marca de no-espacio | |
| N | Número | |
| Nd | Número decimal | |
| Nl | Número letra | |
| No | Otro número | |
| P | Puntuación | |
| Pc | Puntuación de conexión | |
| Pd | Puntuación guión | |
| Pe | Puntuación de cierre | |
| Pf | Puntuación final | |
| Pi | Puntuación inicial | |
| Po | Otra puntuación | |
| Ps | Puntuación de apertura | |
| S | Símbolo | |
| Sc | Símbolo de moneda | |
| Sk | Símbolo modificador | |
| Sm | Símbolo matemático | |
| So | Otro símbolo | |
| Z | Separador | |
| Zl | Separador de línea | |
| Zp | Separador de párrafo | |
| Zs | Separador de espacio |
Las propiedades extendidas tales como "Greek" o "InMusicalSymbols" no están soportadas por PCRE.
El especificar coincidicencias insensibles a mayúsculas-minúsculas no afecta a estas secuencias de escape. Por ejemplo, \p{Lu} siempre coincide con letras mayúsculas.
El escape \X coincide con cualquier número de caracteres Unicode que forman un secuencia Unicode extendida. \X es equivalente a (?>\PM\pM*).
Es decir, coincide con un carácter sin la propiedad "marca", seguido por cero o más caracteres sin la propiedad "marca", y trata la secuencia como un grupo atómico (véase más adelante). Los caracteres con la propiedad "marca" son típicamente acentos que afectan al carácter precedente.
La comparación de caracteres por propiedades Unicode no es rápida, porque PCRE ha de buscar una estructura que contiene datos por más de quince mil caracteres. Es por esto por lo que las secuencias de escape tradicionales tales como \d y \w no usan propiedades Unicode en PCRE.
For those who wonder: 'letter_titlecase' applies to digraphs/trigraphs, where capitalization involves only the first letter.
For example, there are three codepoints for the "LJ" digraph in Unicode:
(*) uppercase "LJ": U+01C7
(*) titlecase "Lj": U+01C8
(*) lowercase "lj": U+01C9
For those who wonder: 'letter_titlecase' applies to digraphs/trigraphs, where capitalization involves only the first letter.
For example, there are three codepoints for the "LJ" digraph in Unicode:
(*) uppercase "LJ": U+01C7
(*) titlecase "Lj": U+01C8
(*) lowercase "lj": U+01C9
An excellent article explaining all these properties can be found here: http://www.regular-expressions.info/unicode.html
these properties are usualy only available if PCRE is compiled with "--enable-unicode-properties"
if you want to match any word but want to provide a fallback, you can do something like that:
<?php
if(@preg_match_all('/\p{L}+/u', $str, $arr) {
// fallback goes here
// for example just '/\w+/u' for a less acurate match
}
?>
