Results 1 to 5 of 5

Thread: replacing split() with explode()

  1. #1
    Join Date
    Jan 2012
    Posts
    52
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Question replacing split() with explode()

    PHP Code:
    function dateconvert($date,$func){
      if (
    $func == 1){ //insert conversion
        
    list($day$month$year) = split('[/.-]'$date);
        
    $year=trim($year);
        
    $date "$year-$month-$day";
        return 
    $date;
      }
      if (
    $func == 2){ //output conversion
        
    list($year$month$day) =split('[-.]'$date);
        if(
    trim($date)!==''$date "$day/$month/$year";
        return 
    $date;
      }

    This function is written with split() function. how can i replace it with explode(). because php 5.3.0 does not support split.
    Last edited by keyboard; 10-02-2012 at 08:35 AM. Reason: Format: Php Tags

  2. #2
    Join Date
    May 2012
    Location
    Hitchhiking the Galaxy
    Posts
    1,013
    Thanks
    47
    Thanked 139 Times in 139 Posts
    Blog Entries
    1

    Default

    an alternative to split(), is preg_split(), ie:
    Code:
    preg_split("/[|-|.|]/", $date)
    Last edited by keyboard; 10-02-2012 at 09:09 AM. Reason: Remove bbcode warning
    "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program." - Linus Torvalds
    Anime Views Forums
    Bernie

  3. #3
    Join Date
    Jan 2012
    Posts
    52
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default

    PHP Code:
    <?php
    function dateconvert($date,$func){
      if (
    $func == 1){ //insert conversion
        
    list($day$month$year) = preg_split("/[|/|.|-|]/"$date);
        
    $year=trim($year);
        
    $date "$year-$month-$day";
        return 
    $date;
      }
     if (
    $func == 2){ //output conversion
        
    list($year$month$day) = preg_split("/[|-|.|]/"$date);
        if(
    trim($date)!==''$date "$day/$month/$year";
        return 
    $date;
      }
    }
    ?>
    i used the function preg_split in this way. but it doesn't work. can u please find out the error and recorrect it?
    Last edited by jscheuer1; 03-28-2013 at 06:49 AM. Reason: Format

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,030
    Thanks
    44
    Thanked 3,210 Times in 3,172 Posts
    Blog Entries
    12

    Default

    Try:

    PHP Code:
    <?php 
    function dateconvert($date,$func){ 
      if (
    $func == 1){ //insert conversion 
        
    list($day$month$year) = preg_split("/[\/.-]/"$date); 
        
    $year=trim($year); 
        
    $date "$year-$month-$day"
        return 
    $date
      } 
     if (
    $func == 2){ //output conversion 
        
    list($year$month$day) = preg_split("/[-.]/"$date); 
        if(
    trim($date)!==''$date "$day/$month/$year"
        return 
    $date
      } 

    ?>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. The Following User Says Thank You to jscheuer1 For This Useful Post:

    megha_3000 (03-28-2013)

  6. #5
    Join Date
    Jan 2012
    Posts
    52
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default

    ALHAMDULILLAH Thank You so much.

Similar Threads

  1. explode
    By james438 in forum PHP
    Replies: 9
    Last Post: 03-22-2012, 06:05 AM
  2. split explode?
    By bluewalrus in forum PHP
    Replies: 7
    Last Post: 05-31-2009, 01:20 PM
  3. Explode Function Help
    By DeSaaD37 in forum PHP
    Replies: 4
    Last Post: 03-16-2009, 04:59 PM
  4. php explode question
    By centenial in forum PHP
    Replies: 3
    Last Post: 10-10-2006, 09:43 PM
  5. explode.js
    By chloedearest in forum JavaScript
    Replies: 7
    Last Post: 08-10-2006, 06:21 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
  •