How do I get the parts of string like: abc_abc\_cde_d ?
The result should be: abc, abc\_cde, d
How do I get the parts of string like: abc_abc\_cde_d ?
Moderator: General Moderators
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Code: Select all
[feyd@home]>php -r "$t = 'abc_abc\_cde_d'; $n = preg_split('#(?<!\\\\)_#', $t); var_dump($n);"
array(3) {
[0]=>
string(3) "abc"
[1]=>
string(8) "abc\_cde"
[2]=>
string(1) "d"
}feyd wrote:Code: Select all
$n = preg_split('#(?<!\\\\)_#', $t);
Can you explain how it's work?
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
#(?<!\\\\)_#
# - opening delimiter
( - begin a group
?< - special command that for zero-width look behind
! - negate the zero-width; without this, it would require the enclosed pattern (of the group) before the pattern that follows the group.
\\\\ - A single backslash to PREG; PHP wants \\ to signal one backslash, so does PREG. To not escape the following closing parenthesis, we need four.
) - end the group
_ - underscore that's being looked for
# - ending delimiter
# - opening delimiter
( - begin a group
?< - special command that for zero-width look behind
! - negate the zero-width; without this, it would require the enclosed pattern (of the group) before the pattern that follows the group.
\\\\ - A single backslash to PREG; PHP wants \\ to signal one backslash, so does PREG. To not escape the following closing parenthesis, we need four.
) - end the group
_ - underscore that's being looked for
# - ending delimiter