簡體   English   中英

使用正則表達式從字符串中獲取所有單詞

[英]get all words from string with regular expression

我需要可以從字符串中找到所有單詞的preg_match 例如:

$str = "string: hi, it is string.";

我想得到這個:

[0] => string
[1] => hi
[2] => it
[3] => is
[4] => string

我使用'/[az]+/ui' ,但是得到了:

[0] => string:
[1] => hi,
[2] => it
[3] => is
[4] => string.

您說的是preg_match() ,而是應該使用preg_match_all() ,這里不需要在正則表達式中使用u修飾符。

$str = "string: hi, it is string.";

preg_match_all('/[a-z]+/i', $str, $matches);
print_r($matches[0]);

產量

Array
(
    [0] => string
    [1] => hi
    [2] => it
    [3] => is
    [4] => string
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM