Split string if the next chracter is a specific character, e.g. 'abc'

I currently have the line shown below

$string = "4WL533cba00780352524MT_FAILED(SCRNotInAllowList)                  STATUS_NOT_AVAILABLE";

I read the online tutorial:

(gif|jpg)   Matches either "gif" or "jpeg".

In my case, I only need to split when the text contains DN_ or MT _

$data = preg_split("/[\s,(DN_|MT_),]+/", trim($string));

conclusion

[0] => Array
    (
        [0] => 4WL533cba00780352524
        [1] => FAILE
        [2] => SCR
        [3] => otInAllowList
        [4] => S
        [5] => A
        [6] => US
        [7] => O
        [8] => AVAILABLE
    )

however, I expect the output to be:

[0] => Array
    (
        [0] => 4WL533cba00780352524
        [1] => MT_FAILED(SCRNotInAllowList)
        [2] => STATUS_NOT_AVAILABLE
    )

Hope you guys can help me.

+4
source share
2 answers

In your regular expression pattern, you transfer the intended pattern to a character class and cannot use the rotation operator — you can see this regular expression site for a detailed explanation of the basic regular expression syntax, background theory, and extension functions in common regular expression machines.

$data = preg_split("/\s+|(?=DN_|MT_)/", trim($string));

:

, "DN_", "MT_". , . lookahead, . , "DN_" "MT_".

+4
$string = "4WL533cba00780352524MT_FAILED(SCRNotInAllowList)                  STATUS_NOT_AVAILABLE";

$data = preg_split("/\s+|(?=DN_|MT_)/", trim($string));
print_r($data);

Array
(
[0] => 4WL533cba00780352524
[1] => MT_FAILED(SCRNotInAllowList)
[2] => STATUS_NOT_AVAILABLE
)
+4

Source: https://habr.com/ru/post/1539871/


All Articles