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
Unicode 文字プロパティ
PHP 5.1.0 以降、UTF-8 モード を設定した場合に、 一般的な文字タイプにマッチする新たなエスケープシーケンスが 3 つ追加されました。
- \p{xx}
- xx プロパティを持つ文字
- \P{xx}
- xx プロパティを持たない文字
- \X
- 拡張 Unicode シーケンス
ここで xx で表されているプロパティ名は、Unicode で 一般カテゴリプロパティ (general category properties) として規定されているものに なります。すべての文字は、いずれかひとつのプロパティを持ちます。 プロパティは、2 文字の略語で表されます。Perl と同じく、 開き波カッコとプロパティ名との間にハット文字を記述することで否定を指定できます。 たとえば、\p{^Lu} は \P{Lu} と同じです。
\p もしくは \P の後に、一文字だけを記述すると その文字で始まるすべてのプロパティが指定されたことになります。 この場合、否定の指定をしていない場合、波カッコを使用しなくても構いません。 以下の 2 つの例は等価になります。
\p{L}
\pL
| プロパティ | マッチ | 備考 |
|---|---|---|
| C | その他 (Other) | |
| Cc | コントロール文字 (Control) | |
| Cf | 非可視整形用文字 (Format) | |
| Cn | 未定義コードポイント (Unassigned) | |
| Co | 私的利用領域 (Private use) | |
| Cs | サロゲート (Surrogate) | |
| L | アルファベット (Letter) | Ll、 Lm、Lo、Lt および Lu を含む |
| Ll | 小文字アルファベット (Lower case letter) | |
| Lm | 擬似文字 (Modifier letter) | |
| Lo | その他の文字 (Other letter) | |
| Lt | タイトル文字 (Title case letter) | |
| Lu | 大文字アルファベット (Upper case letter) | |
| M | 記号 (Mark) | |
| Mc | 修飾文字 (Spacing mark) | |
| Me | 他の文字を囲むための文字 (Enclosing mark) | |
| Mn | 他の文字を修飾するための文字 (Non-spacing mark) | |
| N | 数字 (Number) | |
| Nd | 10 進数字 (Decimal number) | |
| Nl | 数値を表す文字 (Letter number) | |
| No | その他の数字 (Other number) | |
| P | 句読記号 (Punctuation) | |
| Pc | 連結用句読記号 (Connector punctuation) | |
| Pd | ダッシュ (Dash punctuation) | |
| Pe | 閉じ句読記号 (Close punctuation) | |
| Pf | 末尾句読記号 (Final punctuation) | |
| Pi | 先頭句読記号 (Initial punctuation) | |
| Po | その他の句読記号 (Other punctuation) | |
| Ps | 開き句読記号 (Open punctuation) | |
| S | 記号 (Symbol) | |
| Sc | 通貨記号 (Currency symbol) | |
| Sk | 合わせ文字 (Modifier symbol) | |
| Sm | 数学記号 (Mathematical symbol) | |
| So | その他の記号 (Other symbol) | |
| Z | 区切り文字 (Separator) | |
| Zl | 行区切り文字 (Line separator) | |
| Zp | 段落区切り文字 (Paragraph separator) | |
| Zs | 空白文字 (Space separator) |
"Greek" や "InMusicalSymbols" といった拡張プロパティ (extended properties) は PCRE によりサポートされていません。
大小文字を区別しないマッチングを設定していても、これらのエスケープ シーケンスには影響しません。たとえば、\p{Lu} は 常に大文字にのみマッチします。
\X は、拡張 Unicode シーケンスを構成する Unicode 文字群に マッチします。\X は、(?>\PM\pM*) と等価です。
つまり、記号 (mark) プロパティの付いていない文字と、その後に続く 0 以上の 記号プロパティ付きの文字にマッチし、その並びをアトミック (atomic) な まとまりとして取り扱います。記号プロパティ付きの文字とは、アクセント記号などの 直前の文字に対して影響するようなもののことです。
Unicode プロパティを使った文字列マッチングは速くありません。PCRE は 15,000 以上のデータからなるストラクチャを検索する必要が有るためです。 そのため、PCRE では、\d や \w といった 以前から有るエスケープシーケンスは Unicode プロパティを使用しないように なっています。
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
}
?>
