Skip to content
Advertisement

PHP preg_split split by group 1

I have these inputs:

JavaScript

I want to split them by the first parentheses, the output i want:

JavaScript

I tried these but not working correctly :

JavaScript

But it splits every space with parentheses and returns five items rather two.

Advertisement

Answer

You can use

JavaScript

See the PHP demo. See the regex demo.

The preg_split third $limit argument set to 2 makes it split the string with the first occurrence of the pattern that matches:

  • s* – 0+ whitespaces
  • (?=([^()]*)) – that are followed with (, 0 or more chars other than ( and ) and then a ).
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement