Split string by delimiter, but not if it is escaped
Use dark magic: $array = preg_split(‘~\\\\.(*SKIP)(*FAIL)|\|~s’, $string); \\\\. matches a backslash followed by a character, (*SKIP)(*FAIL) skips it and \| matches your delimiter.
Use dark magic: $array = preg_split(‘~\\\\.(*SKIP)(*FAIL)|\|~s’, $string); \\\\. matches a backslash followed by a character, (*SKIP)(*FAIL) skips it and \| matches your delimiter.
Here you go: preg_split(‘/([^.:!?]+[.:!?]+)/’, ‘good:news.everyone!’, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); How it works: The pattern actually turns everything into a delimiter. Then, to include these delimiters in the array, you can use the PREG_SPLIT_DELIM_CAPTURE constant. This will return an array like: array ( 0 => ”, 1 => ‘good:’, 2 => ”, 3 => ‘news.’, 4 … Read more
You can do the following using array_map: $new_arr = array_map(‘trim’, explode(‘,’, $str));