In my PHP code, I have class name with namespace assigned in the string, for example:
$my_class_name; // = "Aaa\Bbb\Ccc"; // this is not in source code, just var dump
I need just the middle name, ‘Bbb’ in my case. I tried to use this:
$result_array = preg_split("/\\/", $my_class_name);
However, it does not work. I need to use tripple backslash in the regexp "/\\\/" to make it work. My question is: Why do I need three of them? I have always escaped backslash special function by doubling it.
You want to have a literal backslash in your regex, so you must escape it. But then you also want to put it inside a PHP string, which means that you must escape it once more.
The sequence
\\\/gets broken down into\\(one literal backslash character) and\/(a backslash followed by a slash; as per PHP string escaping rules, that is not a valid escape sequence and so is recognized as the pair or character literals\/)Four backslashes would also be translated to two backslash characters, so specifying the pattern as the string literal
"/\\\\/"is equivalent to specifying it as"/\\\/".But why are you using
preg_splitinstead ofexplode('\\', $my_class_name)?