-1

I am doing some tests moving from the function split to preg_split in this line of code

list($tag) = preg_split('/[ >]/', substr($chunk, 2 - $open), 2);

What exactly do the [] do? Can they be omitted?

The Guy with The Hat
  • 10,836
  • 8
  • 57
  • 75

2 Answers2

3

It allows any character in the set. That is the use of [] in RegEx (Regular Expressions).

Eg: /[ABC]/ will match any capital letter of "A", "B" or "C".

Take a look at some information at http://www.regexr.com/. That should help a lot. It also allows you to see what will be picked up by testing your own search text with expressions.

Brandon White
  • 997
  • 9
  • 22
1

The [] chars define an char group. Any character in this group would be allowed.

So your code /[ >]/ allow a space or the > char. The / chars surround the regex expression.

=> Your regex split the input on every or >

Philipp
  • 15,377
  • 4
  • 35
  • 52