I need to split string with multiple possible delimiters
$output = preg_split( "/(\^|+)/", "54654561^uo_sgzg@zrgher.com" );
-> (“54654561”, “uo_sgzg@zrgher.com”)
$output = preg_split( "/(\^|+)/", "54654561+ghkkgzg@zrgher.com" );
-> (“54654561”, “ghkkgzg@zrgher.com”)
But this regex "/(\^|+)/" fails: PHP Warning: preg_split(): Compilation failed: nothing to repeat at offset 3 for some reason, however it’s based on this answer Php multiple delimiters in explode
this one is working $output = preg_split("/[\^|+]/", "54654561^MYMED_sgzg@zrgher.com" ); is it the right way to split with multiple delimiters?
edit : sorry I just realized it’s working like this $output = preg_split("/(\^|\+)/", "54654561^MYMED_sgzg@zrgher.com" );
If it’s single characters you’re trying to match, I would actually opt for the
[\^\+]option.Demo: http://ideone.com/ftXSS
The
[]syntax is called a character class or character set, and it’s designed to do specifically what you need.http://www.regular-expressions.info/quickstart.html