2

I'm trying to write a regex with which I can split a string into tokens. This used to work:

$rawtokens = split("[^-_A-Za-z0-9]+", $string);

But now split() has been deprecated (and using preg_split is recommended), this doesn't work:

$rawtokens = preg_split("[^-_A-Za-z0-9]+", $string);

The error I get is that +\ is an unknown modifier. What's has changed with the migration from split to preg_split?

Pr0no
  • 3,910
  • 21
  • 74
  • 121

3 Answers3

2

You need delimiters for PCRE regexes:

$rawtokens = preg_split("/[^-_A-Za-z0-9]+/", $string);
                         ^               ^
hakre
  • 193,403
  • 52
  • 435
  • 836
0

You can use T-Regx tool and you won't need delimiters :)

pattern('[^-_A-Za-z0-9]+')->split($string);
Danon
  • 2,771
  • 27
  • 37
0

Try escaping the - and adding delimiters:

$rawtokens = preg_split("/[^\-_A-Za-z0-9]+/", $string);
Ry-
  • 218,210
  • 55
  • 464
  • 476
  • 2
    There is no need to escape the `-` character at the beginning or end of a character class because in those positions its meaning is unambiguous – meouw Oct 31 '11 at 23:40
  • @meouw: It's good to develop good regular expression habits. – Ry- Oct 31 '11 at 23:52
  • you suggested escaping as part of your answer - the escaping is irrelevant here – meouw Nov 01 '11 at 07:31