I want to split the string around for comma( ,
) or &
. This is simple but I want to stop the match for any content between brackets. For example if I run on
sleeping , waking(sit,stop)
there need to be only one split and two elements
thanks in advance
This is a perfect example for the (*SKIP)(*FAIL)
mechanism PCRE
(and thus PHP
) offers.
You could come up with the following code:
<?php
$string = 'sleeping , waking(sit,stop)';
$regex = '~\([^)]*\)(*SKIP)(*FAIL)|[,&]~';
# match anything between ( and ) and discard it afterwards
# instead match any of the characters found on the right in square brackets
$parts = preg_split($regex, $string);
print_r($parts);
/*
Array
(
[0] => sleeping
[1] => waking(sit,stop)
)
*/
?>
This will split any ,
or &
which is not in parentheses.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.