RE: [PHP] Insert Chars into a string
| From: | Mike Johnson | Date: | Thu, 14 Apr 2005 18:13:43 +0000 |
| Subject: | RE: [PHP] Insert Chars into a string | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-213107@lists.php.net to get a copy of this message | ||
From: Mike Johnson [mailto:mike@smarterliving.com]
> 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.
Actually, it occurs to me that strrpos() might be faster.
$filename = substr($filename, 0, strrpos('.jpg'))
. date('YmdHis')
. substr($filename, strrpos('.jpg'), strlen($filename));
Might need to tweak it, but that should work.
--
Mike Johnson Smarter Living, Inc.
Web Developer www.smartertravel.com
mike@smarterliving.com (617) 886-5539