式は、 azと0-9を除く/[^a-z0-9-]+/i
すべての文字に一致します(その後、空のスペースに置き換えられます) 。in [は、そこに含まれる文字セットを無効にすることを意味します。^
^...]
[^a-z0-9]
英数字以外の文字と一致します
+
上記の1つ以上を意味します
/i
大文字と小文字を区別せずに一致させます
式/\&#?[a-z0-9]{2,4}\;/
は、&
その後にオプションで#
、2〜4個の文字と数字が続き、最後に次のようなHTMLエンティティ;
に一致します。
'
&#?
&
いずれかに一致するか&#
、?
前述#
のオプションを作成するため、&
実際にはエスケープする必要はありません。
[a-z0-9]{2,4}
2〜4文字の英数字に一致
;
文字通りのセミコロンです。実際にエスケープする必要はありません。
ご想像のとおり、最後のタグは、または<tagname>
のようなタグを空のスペースに置き換えます。の内部コンテンツだけでなく、タグ全体と一致することに注意してください。<tagname attr='value'>
</tagname>
<>
<
リテラル文字です
[^>]+
次までのすべての文字ですが、次の文字は含まれません>
>
リテラル文字です
preg_replace()
これをネストするのではなく、 への3つの別々の呼び出しとして書き直すことを強くお勧めします。
// Strips tags.
// Would be better done with strip_tags()!!
$texts = preg_replace('/<[^>]+>/', ' ', $texts);
// Removes HTML entities
$texts = preg_replace('/&#?[a-z0-9]{2,4};/', ' ', $texts);
// Removes remainin non-alphanumerics
$texts = preg_replace('/[^a-z0-9-]+/i', ' ', $texts);
$array = explode(' ', $texts);