开发者

How to remove time stamp and date from a string

开发者 https://www.devze.com 2023-03-19 11:34 出处:网络
I have added time stamp and date when the details should create. 开发者_如何学Cand the time stamp and date will add in database. When i\'ll retrieve it from db it wanna to show only that string name w

I have added time stamp and date when the details should create. 开发者_如何学Cand the time stamp and date will add in database. When i'll retrieve it from db it wanna to show only that string name without time stamp and date ? how to make it?

Adding Method....

   $imagename = $_POST['imagename'];
   $imageNameExt = $_POST['imageNameExt'];
   $newImgName = trim($imagename).'_'.date('Y_m_d_H_i_s').'.'.trim(strtolower($imageNameExt));

   Here consider the $imagename = "abc"; and $imageNameExt = "jpg"; when it'll insert into db the name as $newImgName = "abc_2011_07_12_18_47_02.jpg"....

Now i need to retrieve from db..How should i print only the name "abc" with out the time stamp and date??

Thanks in advance.

Update : (I couldn't post this as a answer bcoz am under 100 reputation.)

found the answer for my question:

While am retrieving it'll come in the name of image_path,

$image_path = $rows[0];
$tmp = "_".date('Y');
$ImgOrgName = explode($tmp,$image_path);
$ImgOrgName_core = $ImgOrgName[0];

The Image name "abc" will come in the string "$ImgOrgName_core".

Try it...i'm easily getting the answer when i use like this.


You should store the original image name and the timestamp information as separate columns in the database. This way you will always have access to each piece of data, and you can concatenate them as you currently do before inserting any time you wish.

You can also hammer your existing code into submission by simply getting the string and counting underscores from the end until you find the 6th (there are 5 in the timestamp so the 6th from the end separates the timestamp from the name), or you can simply remove the last 20 characters from the name with substr (the timestamp is 19 chars if all numbers are padded with zeroes, plus 1 char for the separator underscore), but this kind of solution is messy and prone to breakage. I would never recommend it, so I don't give code for it (and anyway, cutting off the last 20 characters is trivial).


You cat replace date with regexp:

$imgNameNoDate = preg_replace('#_\d{4}_\d{2}_\d{2}_\d{2}_\d{2}_\d{2}#', '', $imgName);

But better way is to change your database to store original name and datetime information.


While I agree with the commenters that it would be much, much better to store the timestamp in its own column, here's how to do specifically what you want.

$underscorePos = strpos($imageNameFromDB, '_');
if ($underscorePos !== false)
{
    $imageNameOnly = substr($imageNameFromDB, 0, $underscorePos);
}


found the answer for my question:

While am retrieving it'll come in the name of image_path,

$image_path = $rows[0];
$tmp = "_".date('Y');
$ImgOrgName = explode($tmp,$image_path);
$ImgOrgName_core = $ImgOrgName[0];

The Image name "abc" will come in the string "$ImgOrgName_core".

Check it...i'm easily getting the answer when i use like this.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号