arrays - PHP preg_split both slashes -
i have string this: "c:\users\jamie/desktop/test.txt"
i want use preg_split
split string array this, using code:
$arr = preg_split("/(\\|\/)/", "c:\users\jamie/desktop/test.txt");
what expect is:
array(0=>"c:",1=>"users",2=>"jamie",3=>"desktop",4=>"test.txt")
but got is
array(0=>"c:\users\jamie/desktop/test.txt")
how can expected result using preg_split
?
simple:
$parts = preg_split('~[\\\\/]~', $filename);
the ~
delimiters. regular expressions can use pretty delimiter. so, instead of /.../
can ~...~
.
when working backslashes have double-escape them, \\\\
. regular expression engine come out \\
. in other words, both backslashes in \\
need escaped, hence \\\\
.
you can use following, if prefer.
$parts = preg_split('/[\\\\\/]/', $filename);
edit. little confusing. maybe explain in way:
- in php
\\\\
seen 2 escaped backslashes, i.e.\\
. - to regex engine
\\
seen 1 escaped backslash, i.e.\
.
Comments
Post a Comment