Question

I have this string:

Rosemary J. Harris $^{1}$, Vladislav Popkov $^{2}$ and Gunter M. Sch\"utz $^{3,}$*

And I would liek to split it with commas (,) or "and" string.

So, the result should be:

[0] -> Rosemary J. Harris $^{1}$
[1] -> Vladislav Popkov $^{2}$
[2] -> Gunter M. Sch\"utz $^{3,}$*

I tryied this:

$splitAuthors = preg_split('/[, ]+[ and ]/', $authors);

Which is returning:

[0] -> Rosemary J. Harris $^{1}$
[1] -> Vladislav Popkov $^{2}$
[2] -> nd Gunter M. Sch\"utz $^{3,}$*

There is the "nd" in the last array item.

Thank you for your help.

Was it helpful?

Solution

Dont use character classes for this use the or seperator |

This should give you the correct output:

preg_split("/, | and /",$data)

Gives the following output:

Array
(
    [0] => Rosemary J. Harris $^{1}$
    [1] => Vladislav Popkov $^{2}$
    [2] => Gunter M. Sch\"utz $^{3,}$*
)

OTHER TIPS

[] define a character class. These match a SINGLE character, where that character can be any of the characters in the class. So you're looking for a spot in your string where that spot contains either a space, an a, an n, or a d. It's NOT looking for the world "and" with spaces on either side. You probably want ( and ) instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top