Reputation: 1864
I am using a filemangement system and to protect the url. Manipulation of the url string is not allowed and leads to killing the page. I want to make an exception for that when I delete files.
When deleting files, the url string always looks like this:
example.com/sfm?delete=uploads/sfm/c4ca4238a0b923820dcc509a6f75849b/folder1
or
example.com/sfm?delete=uploads/sfm/c4ca4238a0b923820dcc509a6f75849b/file.jpg
or
example.com/sfm?delete=uploads/sfm/c4ca4238a0b923820dcc509a6f75849b/folder1/file.jpg
So the minimum of the string contains always:
example.com/sfm?delete=uploads/sfm/c4ca4238a0b923820dcc509a6f75849b
( the hash is a md5 hash of a userid)
So how can I grab that minimum url string?
Upvotes: 0
Views: 40
Reputation: 9583
Use implode
, array_slice
, explode
.
Explode will extract the string to array, array slice cut the array upto 4th item, and implode will grab the array items to a new string.
$str = 'example.com/sfm?delete=uploads/sfm/c4ca4238a0b923820dcc509a6f75849b/folder1 ';
echo $output = implode('/', array_slice(explode("/", $str), 0, 4)); //example.com/sfm?delete=uploads/sfm/c4ca4238a0b923820dcc509a6f75849b
Upvotes: 1
Reputation: 167162
I would just use explode()
. Even if the hash is not of the same size, if you follow the same structure, I would just do this:
$delete = explode("/", $_GET["delete"]);
print_r($delete);
Outputs to:
Array
(
[0] => uploads
[1] => sfm
[2] => c4ca4238a0b923820dcc509a6f75849b
[3] => folder1
)
The $delete[2]
will give me the hash. I guess this would be the best way, even if the URL changes.
Upvotes: 1