RE: Insert Chars into a string

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: Mike Johnson [mailto:mike@xxxxxxxxxxxxxxxxx] 

> From: PartyPosters [mailto:sales@xxxxxxxxxxxxxxxxxx] 
> 
> > 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@xxxxxxxxxxxxxxxxx   (617) 886-5539

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux