Domanda

I need preg_match that can find me all words from string. for example:

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

I would like get this:

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

I use with '/[a-z]+/ui', but I get this:

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

Soluzione

You said preg_match(), instead you should be using preg_match_all() and there is no need to use the u modifier in your regular expression here.

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

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

Output

Array
(
    [0] => string
    [1] => hi
    [2] => it
    [3] => is
    [4] => string
)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top