Page 1 of 1

How do I get the parts of string like: abc_abc\_cde_d ?

Posted: Tue Jan 30, 2007 2:08 am
by nadavvin
How do I get the parts of string like: abc_abc\_cde_d ?

The result should be: abc, abc\_cde, d

Posted: Tue Jan 30, 2007 2:13 am
by feyd

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"
}

Posted: Tue Jan 30, 2007 2:35 am
by nadavvin
Thanks

Posted: Sun Feb 04, 2007 3:21 am
by nadavvin
feyd wrote:

Code: Select all

$n = preg_split('#(?<!\\\\)_#', $t);

Can you explain how it's work?

Posted: Sun Feb 04, 2007 12:13 pm
by feyd
It looks for an underscore without a backslash before it. ... or are you asking me to break the entire pattern down?

Posted: Sun Feb 04, 2007 2:12 pm
by nadavvin
to break the entire pattern down

Although, I know regular expression syntax, I don't know How this work.

Posted: Sun Feb 04, 2007 2:21 pm
by feyd
#(?<!\\\\)_#

# - 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