RE: [PHP] Insert Chars into a string
| From: | Mike Johnson | Date: | Thu, 14 Apr 2005 18:09:42 +0000 |
| Subject: | RE: [PHP] Insert Chars into a string | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-213106@lists.php.net to get a copy of this message | ||
From: PartyPosters [mailto:sales@partyposters.co.uk]
> Hello,
> I have a variable that contains a filename, I want to be able
> to insert the date and time just before for the ".jpg"
> for example if my filename is 'pic.jpg' I want it to read
> 'pic_Monday 15th of January 2003 05:51:38 AM.jpg (or if
> anyone else knowshow to write the time and time all in
> numbers it would be appreciated as I'm using ate("l dS of F Y
> h:i:s A") which obviously is a bit long.
First off, I'd probably use preg_replace(). Maybe something like:
$pattern = '/^(.*?)(\.jpg)$/';
$replacement = '\1' . date('l dS of F Y h:i:s A') . '\2';
$filename = preg_replace($pattern, $replacement, $filename);
I haven't tested that, but I think it should work.
As for the date format, if it doesn't need to be human-readable, might I
suggest date('U')? It's the number of seconds since the epoch; it's
easily convertable to human-readable format. If that doesn't fly,
perhaps MySQL's datetime format, which is a 14-digit int, achievable
with date('YmdHis'). I may be a holdover from DOS, but I shudder at
spaces in filenames. :)
HTH!
--
Mike Johnson Smarter Living, Inc.
Web Developer www.smartertravel.com
mike@smarterliving.com (617) 886-5539