Dim13i
Dim13i

Reputation: 2015

Obtain specific data with preg_match_all

I have different texts which aren't well formatted, therefore I need a pattern which works with all of them and return some specific elements (text) from it. Let's say I have this text:

"AL TEST232    KW     12*/13*/17 TEST kw16TEST123 kw 15*"

and I want my preg_match_all() to return something like this:

Array
(
    [0] => Array
        (
            [0] => AL TEST232
            [1] => 12/13/17
        )
    [1] => Array
        (
            [0] => TEST
            [1] => 16
        )
    [2] => Array
        (
            [0] => TEST123
            [1] => 15
        )
)

Is this possible with a single pattern?

Upvotes: 1

Views: 28

Answers (1)

anubhava
anubhava

Reputation: 785256

You can use:

preg_match_all('~(\w[\s\w]*?\w)\s*kw\s*([\d/*]+)~', $input, $matches);

RegEx Demo

Upvotes: 1

Related Questions