vian
vian

Reputation: 801

regex - match empty space between any of characters

In PHP it is a common practice to treat strings as immutable. Sometimes there's a need to modify a string "in-place".

We go with the additional array creation approach.

This array should contain every single letter from the source string.

There's a function for that in PHP (str_split). One issue, it doesn't handle multibyte encodings well enough.

There's also a mb_split function which takes a regex as an input parameter for separator sequence. So

mb_split('.', '123') 

returns ['', '', '', ''].

BUT:

mb_split('', '123') 

returns ['123'].

So I believe there is a counterpart regex which matches empty space between any variation of multi-byte character sequence.

So for '123' it should match

'1~2', '2~3' 

where ~ is an actual match. That is just like \b but for anything.

Is there a regex hack to do so?

Upvotes: 1

Views: 141

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626896

Use

preg_match_all('~\X~u', $s, $arr)

The $arr[0] will contain all the characters. The \X pattern matches any Unicode grapheme. The /u modifier is necessary to make the regex engine treat the input string as a Unicode string and make the pattern Unicode aware.

See the PHP demo.

Upvotes: 1

Related Questions