I have array:
$array = array(
"C:/path/something1/something2/dir",
"C:/path1/something/something2/dir2\nextdir",
"C:/path2/something/dir2\nextdir\next",
"C:/path/something3/something6/something7/dir5\nextdir2\next"
);
All that is before the last sign "/" with him to disappear.
I want something like that:
$array = array(
"dir",
"dir2\nextdir",
"dir2\nextdir\next",
"dir5\nextdir2\next"
);
I need regex
$new_array = preg_grep("/regex/", $array);
I have no idea how to write a regex.
I dont want like that:
foreach($array as $key => $val) {
$e = explode("/", $val);
$new_array[] = end($e);
}
preg_grep()
does not change/replace the values, it returns the items that match the given regular expression. If you must use regex and replace the values, take a look at preg_replace()
instead:
$array = preg_replace('~.*/~', '', $array);
var_dump($array);
Output
array(4) {
[0]=> string(3) "dir"
[1]=> string(12) "dir2\nextdir"
[2]=> string(17) "dir2\nextdir\next"
[3]=> string(18) "dir5\nextdir2\next"
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.