Closed Thread
Results 1 to 1 of 1




  

Thread: PHP Snippet Share: Regex & Explode

      
  1. #1
    Permanently Banned West will become famous soon enoughWest will become famous soon enough
    Join Date
    November 4, 2006
    Posts
    264
    Rep Power
    0
    Feedback Score
    0

    Default PHP Snippet Share: Regex & Explode

    Hello, I was just playing around with some database display information. I have a variable that I should tell you what they are first.

    -- The $e[array] is an events database call
    -- The others are pretty explanatory.

    Anyways I wanted to take a DATE TIME entry in the database. It looks like so YEAR/MO/DA 08:08:00 PM. I wanted this to do different things, and figured you could make some use of it. I plan to make this more efficiant with time, but pretty simply does what I needed for the time being.

    I wanted YEAR-MO-DA 08:08:00 PM to first of all, leave out the time. So let's count how many chars are in YEAR/MO/DA

    Y1-E2-A3-R4-|-5|-M6-07-|-8|-D9-A10 .. 10 characters in the 'needed' string. So let's limit the length of $datestart and $dateend.

    PHP Code:
    $datestart substr($e['datestart'], 010); // Show only the date
    $dateend   substr($e['dateend'], 010);  // Show only the date 
    substr does all the work there..

    Now, it's showing the date only. I needed it to remove the -'s so I did... and replaced them with /.

    PHP Code:
      $reg_ex "\-";  // Look for what?
      
    $replace_word "/"// Replace with what?

      
    $datestart ereg_replace($reg_ex$replace_word$datestart); 
      
    $dateend ereg_replace($reg_ex$replace_word$dateend); 
    Now that I have replaced the dashes with slashes I needed to completely chang the format of YEAR/MO/DA to MO/DA/YEAR so let's go ahead and use explode to do that.

    PHP Code:
       $datestart_pieces explode("/"$datestart);
        
    $year   =  $datestart_pieces[0]; 
        
    $month $datestart_pieces[1];
        
    $day    $datestart_pieces[2];
           
    $datestart $month.'/'.$day.'/'.$year;

       
    $dateend_pieces explode("/"$datestart);
        
    $year   =  $dateend_pieces[0]; 
        
    $month $dateend_pieces[1];
        
    $day    $dateend_pieces[2];
            
    $dateend $month.'/'.$day.'/'.$year
    Then call show what we've done.

    PHP Code:
     echo $datestart.' - '.$dateend
    This all looks like this.

    PHP Code:
    $datestart substr($e['datestart'], 010); // Show only the date
    $dateend   substr($e['dateend'], 010);  // Show only the date

    $reg_ex "\-";  // Look for what?
    $replace_word "/"// Replace with what?

      
    $datestart ereg_replace($reg_ex$replace_word$datestart); 
      
    $dateend ereg_replace($reg_ex$replace_word$dateend);

       
    $datestart_pieces explode("/"$datestart);
        
    $year   =  $datestart_pieces[0]; 
        
    $month $datestart_pieces[1];
        
    $day    $datestart_pieces[2];
           
    $datestart $month.'/'.$day.'/'.$year;

       
    $dateend_pieces explode("/"$dateend);
        
    $year   =  $dateend_pieces[0]; 
        
    $month $dateend_pieces[1];
        
    $day    $dateend_pieces[2];
            
    $dateend $month.'/'.$day.'/'.$year;
            
            echo 
    $datestart.' - '.$dateend
    Enjoy!

    EpiphanySales added 11 Minutes and 11 Seconds later...

    Ha. Was just notified by someone else that it can be more efficiantly this way. Glad I am so ignorant.

    PHP Code:
    $dates explode('-'$datestart);
    $newdatestart "$dates[1]/$dates[2]/$dates[0]"
    Last edited by West; Jun 22nd, 2008 at 4:45 pm. Reason: Automerged Doublepost

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Some Pics To Share
    By dgridley in forum Photography
    Replies: 12
    Last Post: Aug 9th, 2011, 5:49 am
  2. Browser Share
    By grim in forum HTML & Website Design
    Replies: 6
    Last Post: Oct 9th, 2006, 3:07 pm

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
WebTalkForums
WebTalkForums
Recent Forum Threads