Split a text by a backslash \ ?

user723220 picture user723220 · Apr 25, 2011 · Viewed 23.1k times · Source

I've searched for hours. How can I separate a string by a "\"

I need to separate HORSE\COW into two words and lose the backslash.

Answer

Phoenix picture Phoenix · Apr 25, 2011
$array = explode("\\",$string);

This will give you an array, for "HORSE\COW" it will give $array[0] = "HORSE" and $array[1] = "COW". With "HORSE\COW\CHICKEN", $array[2] would be "CHICKEN"

Since backslashes are the escape character, they must be escaped by another backslash.