1

I'm creating a simple database restore script, but I ran into a bump in the road.

Database dump files are created with the script as well, and have the following filename format: datestamp-dbname.sql

With the restore script, I need to remove the datestamp, the hyphen and the .sql, to just leave the dbname.

I currently have this, which removes the first part, but I need to remove the .sql as well.

$getfilename = $_GET['filename'];
$dbname = explode("-", $getfilename[2]);

Thanks.

3 Answers 3

4
list ($timestamp, $dbname) = explode('-', basename($filename, '.sql'));

basename() (with optional second argument) removes the file extension. The rest is trivial.

Another solution:

$dbname = substr($filename, strpos($filename, '-') + 1, -4);

Just take the string between - (excluding: +1) and 4 characters before the end of the string.

I would prefer the first solution, because it feels a little bit cleaner to me (and even more readable)

Sign up to request clarification or add additional context in comments.

Comments

0

The first element of an array has the index zero, not one. You want:

$tmp = explode('-', $getfilename);
$dbname = $tmp[1];

Comments

0
$getfilename = $_GET['filename'];
$dbname = explode("-", $getfilename);
$dbname[1] // this is what you need

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.