1

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
  • 4,479
  • 21
  • 69
  • 111

3 Answers3

2

You need delimiters for PCRE regexes:

$rawtokens = preg_split("/[^-_A-Za-z0-9]+/", $string);
                         ^               ^
hakre
  • 184,866
  • 48
  • 414
  • 792
0

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

pattern('[^-_A-Za-z0-9]+')->split($string);
Danon
  • 2,190
  • 20
  • 34
0

Try escaping the - and adding delimiters:

$rawtokens = preg_split("/[^\-_A-Za-z0-9]+/", $string);
Ry-
  • 209,133
  • 54
  • 439
  • 449
  • 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