4

So i have this problem, i want to split a string by a pattern that contains white space( ) and comma (,). I managed to split my string by that pattern but the problem is that i want to keep that comma in array. Here is my string:

$string = "It's just me, myself, i and an empty glass of wine. Age = 21";

Here is how i split it:

$split = preg_split('/[\s,]+/', $string, -1, PREG_SPLIT_DELIM_CAPTURE);

Here is the array that i'm getting in this example(as you can see that comma disappears)

This is the one that i have now:

Array
(
    [0] => It's
    [1] => just
    [2] => me
    [3] => myself
    [4] => i
    [5] => and
    [6] => an
    [7] => empty
    [8] => glass
    [9] => of
    [10] => wine.
    [11] => Age
    [12] => =
    [13] => 21
)

This is the one that i want:

Array
(
    [0] => It's
    [1] => just
    [2] => me
    [3] => ,
    [4] => myself
    [5] => ,
    [6] => i
    [7] => and
    [8] => an
    [9] => empty
    [10] => glass
    [11] => of
    [12] => wine.
    [13] => Age
    [14] => =
    [15] => 21
)

If there is a space before that comma and that comma is not in that pattern i get it into that array that's generated by preg_split but the problem is that i want it to get that comma with or without space before it.

Is there a way to achieve this?

Thank you! :D

emma
  • 761
  • 5
  • 20
  • use the flag `PREG_SPLIT_DELIM_CAPTURE` http://php.net/manual/en/function.preg-split.php – ArtisticPhoenix Apr 16 '18 at 18:35
  • Should the `space-comma` be at the end or beginning of the splits? – MonkeyZeus Apr 16 '18 at 18:36
  • Your split is for one or more commas, or spaces. Is the comma required? Maybe you just want to ignore the character before the space(s)? https://3v4l.org/H8pTd – chris85 Apr 16 '18 at 18:38
  • That comma is required and that comma should be where it is :D(if the key in array before comma is [3] comma needs to be at [4] - i hope it makes sense :D) – emma Apr 16 '18 at 18:42
  • What should `$string` become, 3 parts? – chris85 Apr 16 '18 at 18:43
  • String should become an array, like this: `Array ( [0] => It's [1] => just [2] => me [3] => myself [4] => i [5] => and [6] => an [7] => empty [8] => glass [9] => of [10] => wine. [11] => Age [12] => = [13] => 21 )` but as you can see, from here those commas are missing :( – emma Apr 16 '18 at 18:45
  • @emma Please add an array that shows how the correct array should look. Should index 2 be `me,`? – chris85 Apr 16 '18 at 18:47
  • Hei @chris85, I've edited, added an example - thank you for your time, i've got an answer below! :D – emma Apr 16 '18 at 18:51

2 Answers2

3

the problem is that i want to keep that comma in array

Then just use the flag PREG_SPLIT_DELIM_CAPTURE

PREG_SPLIT_DELIM_CAPTURE

If this flag is set, parenthesized expression in the delimiter pattern will be captured and returned as well.

http://php.net/manual/en/function.preg-split.php

So you will split it like this

$split = preg_split('/(,)\s|\s/', $string, null, PREG_SPLIT_DELIM_CAPTURE);

You can test it here

https://3v4l.org/Eq8uS

For the Limit argument null is more appropriate then -1 because we just want to skip to the flag argument. It's more clean when you read it because null means nothing where -1 may have some important value (in this case it doesn't) but it just makes it clearer for someone that doesn't know preg_split as well that we are just ignoring that argument.

Community
  • 1
  • 1
ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38
  • Have you tried your suggestion? It doesn't work for me – Daniel Apr 16 '18 at 18:39
  • well considering I wasn't done, but anyway, you can test the sandbox link and it works fine. – ArtisticPhoenix Apr 16 '18 at 18:42
  • 1
    It doesn't split the white space, it splits only the comma X_X – emma Apr 16 '18 at 18:42
  • I just checked the sandbox link - it doesn't work either – Daniel Apr 16 '18 at 18:42
  • 1
    Now it does... simple just use `|` or, you can't capture inside of `[...]` for example. And if you use this `'/(,)\s*|\s/'` is `,` and none or more spaces and if you use this `'/\s*(,)\s*|\s/'` it can do ", " or "," or " , " or ", " , what else you need? – ArtisticPhoenix Apr 16 '18 at 18:45
  • This is the answer! Thank you so muchhh! O my god <3 – emma Apr 16 '18 at 18:47
  • Sure, it's not a big deal. – ArtisticPhoenix Apr 16 '18 at 18:50
  • 2
    You could also reduce `(,)\s|\s` to `(,)?\s+` – revo Apr 16 '18 at 19:09
  • 1
    I will use `preg_split('/\s+|(,)/', $string, null, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY)` to prevent empty results when a comma isn't surrounded by whitespaces. – Casimir et Hippolyte Apr 16 '18 at 19:42
  • Hey guys how do i add the (=) there too? So it can split where it encouters that = just like it does with the comma?And also how do i add all operators there, to split where they are encountered with or without space around(=, <, >, <=, >=, !=, <>) :D – emma Apr 16 '18 at 19:48
  • @emma: change `(,)` to `(,|[<>!]?=|<>?|>)` – Casimir et Hippolyte Apr 16 '18 at 20:03
  • @CasimiretHippolyte Thank youuuu! :D Can i please ask offtopic where can i learn how to ... write or compose a pattern? :-s – emma Apr 16 '18 at 20:06
  • 1
    @emma: read "mastering regular expressions" - J Friedl, O'reilly (you can find the previous editions for free), look at rexegg.com, pcre.org, and the most important is to practise with regex101 or with short php cli scripts. – Casimir et Hippolyte Apr 16 '18 at 20:09
  • @CasimiretHippolyte <3 thank you! I will check all of this:D it's sooo funny how this patterns behave :D – emma Apr 16 '18 at 20:22
1

You intend to split your string on each space OR on the zero-width position before a comma.

The following pattern will explode on (and absorb/destroy) spaces as well as lookahead for commas -- in which case, there is no loss of a character for that qualifying delimiter.

In writing this pattern, you will not need to include the 3rd or 4th parameters available to preg_split().

For tightest practices when your input might not be well formed, you may want to include the NO_EMPTY flag (implemented as preg_split('/ |(?=,)/', $string, 0, PREG_SPLIT_NO_EMPTY).

Code: (Demo)

$string = "It's just me, myself, i and an empty glass of wine. Age = 21";

$array = preg_split('/ |(?=,)/', $string);

print_r($array);

Output:

Array
(
    [0] => It's
    [1] => just
    [2] => me
    [3] => ,
    [4] => myself
    [5] => ,
    [6] => i
    [7] => and
    [8] => an
    [9] => empty
    [10] => glass
    [11] => of
    [12] => wine.
    [13] => Age
    [14] => =
    [15] => 21
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136