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
Propriétés des caractères Unicode
Depuis PHP 5.1.0, trois nouvelles séquences d'échappement pour trouver des types de caractères sont disponibles lorsque le mode UTF-8 est sélectionné. Elles sont :
- \p{xx}
- un caractère avec les propriétés xx
- \P{xx}
- un caractère sans les propriétés xx
- \X
- une séquence étendue Unicode
Les noms des propriétés représentés par xx ci-dessus sont limités aux catégories de propriétés générales Unicode. Chaque caractère a exactement une seule de ces propriétés, spécifié par une abréviation sur deux caractères. Pour des raisons de compatibilité avec Perl, la négation peut être spécifiée en incluant un accent circonflexe entre l'accolade ouvrante et le nom de la propriété. Par exemple, \p{^Lu} équivaut à la même chose que \P{Lu}.
Si une seule lettre est spécifiée avec \p ou \P, il inclut toutes les propriétés qui commencent par cette lettre. Dans ce cas, en l'absence de négation, les accolades dans la séquence d'échappement sont optionnelles ; ceci revient à la même chose :
\p{L}
\pL
| Propriétés | Correspondance | Notes |
|---|---|---|
| C | Autre | |
| Cc | Contrôle | |
| Cf | Format | |
| Cn | Non affecté | |
| Co | Utilisation privée | |
| Cs | Substitut | |
| L | Lettre | Inclut les propriétés suivantes : Ll, Lm, Lo, Lt et Lu. |
| Ll | Lettre en minuscule | |
| Lm | Lettre de modification | |
| Lo | Autres lettres | |
| Lt | Lettre titrée | |
| Lu | Lettre en majuscule | |
| M | Marque | |
| Mc | Marque d'espacement | |
| Me | Marque d'enfermement | |
| Mn | Marque non espacée | |
| N | Nombre | |
| Nd | Nombre décimal | |
| Nl | Nombre Lettre | |
| No | Autres nombres | |
| P | Ponctuation | |
| Pc | Ponctuation de connecteur | |
| Pd | Tiret de ponctuation | |
| Pe | Ponctuation de fermeture | |
| Pf | Ponctuation finale | |
| Pi | Ponctuation initiale | |
| Po | Autres ponctuations | |
| Ps | Ponctuation ouvrante | |
| S | Symbole | |
| Sc | Symbole monétaire | |
| Sk | Symbole de modification | |
| Sm | Symbole mathématique | |
| So | Autres symboles | |
| Z | Séparateur | |
| Zl | Séparateur de ligne | |
| Zp | Séparateur de paragraphe | |
| Zs | Séparateur d'espace |
Les propriétés étendues comme "Greek" ou "InMusicalSymbols" ne sont pas supportées par PCRE.
La sensibilité à la casse de la recherche n'affecte pas les séquences d'échappement. Par exemple, \p{Lu} cherche toujours et uniquement les lettres en majuscules
La séquence \X cherche n'importe quel numéro de caractères Unicode qui forme une séquence étendue Unicode. \X est l'équivalent de (?>\PM\pM*).
C'est-à-dire qu'il cherchera un caractère sans la propriété "Marque", suivi par zéro ou plus caractères avec la propriété "Marque", et traitera la séquence en tant que groupe atomique (voir ci-dessous). Les caractères avec la propriété "Marque" sont typiquement des accents qui affectent le caractère précédent.
La recherche de caractères par les propriétés Unicode n'est pas la méthode la plus rapide, car PCRE doit chercher une structure qui contient les données dans plus de quinze mille caractères. C'est pour cela que les séquences traditionnelles comme \d et \w n'utilisent pas les propriétés Unicode dans 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
}
?>
