Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Aug 2002
    Location
    Oregon, United States of America
    Posts
    882
    Thanks
    1
    Thanked 9 Times in 9 Posts

    ranges of arrays

    Heres the info I have:
    "02346"
    "12356"
    "016"

    THese numbers represent days. 0: Sunday, 6: Saturday, etc.

    What i want to do is take a set of days, lets use 02346 and turn it into human readable. It would read like so:
    "Sunday, Tuesday through Thursday, and Saturday."

    I have made an ugly loop to do this. It turns the string into an array, goes through the array, and compares the next array value to the current one to see if it within a given range, then resetting the array position.

    it works, but its ugly. Does anyone have any ideas on how to do this a little shorter way? I was hoping theres be some regular expression or preg_replace way of doing this or something, but im not tallented enough to do this.
    If I'm postin here, I NEED YOUR HELP!!

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    why do you need to start from a composit value?
    (i personally never use them because they always, sooner or later, create problems)

    and does it need to be such a textual representation? can't you allways display all weekdays and then represent the included days in a different colour or so?
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #3
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    you could simply use str_replace()

    PHP Code:
    <?php

    function returnDates($str) {
      
    $numbers = array(0,1,2,3)
      
    $replacements = array('Sunday, ',
                                     
    '',
                                     
    'Tuesday ',
                                     
    'through ');
      
    $str str_replace($numbers$replacements$str);
      if(
    $str{strlen($str)} == ' ') {
        
    $str substr($str0strlen($str)-1);
      }
      return 
    $str;
    }

      echo 
    returnDates('023');

    ?>
    Thats a function using it.

  • #4
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts


    just ran that code cause i couldn't quite see how that could work.
    echo returnDates('023'); gives 'Sunday, Tuesday through'
    I don't really get what you're intending to demonstrate...
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #5
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Exactly what he asked, changing the numbers to whatever he wants, its just an example, I don't know what he wants what turned into, since I don't know how the 'through' would be able to be added without it being an option int he range on numbers. But yeah, he would obviously change both arrays to whatever. From what he is asking it seems like his script is setting up a range of numbers in one value and parse it into english.
    Last edited by Element; 02-08-2006 at 10:41 PM.

  • #6
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    PHP Code:
    function parse($string) {
        
    $days=array('monday','tuesday','wednesday','thursday','friday','saturday','sunday');
        
    $groups=array();
        
    $gid=$cur=$string{0};
        
    $groups[$cur][]=$cur;
        for(
    $i=1;$i<strlen($string);$i++) {
            if(
    $string{$i}==$cur+1) {
                
    $groups[$gid][]=$string{$i};
                
    $cur=$string{$i};
            }
            else {
                
    $gid=$cur=$string{$i};
                
    $groups[$gid][]=$string{$i};
            }

        }
        foreach(
    $groups as $key=>$group) {
            
    $count=count($group);
            switch(
    $count) {
                case 
    :
                    echo 
    $days[$key];
                    break;
                case 
    :
                    echo 
    $days[$group[0]].' & '.$days[$group[1]];
                    break;
                default :
                    echo 
    $days[$group[0]].' through '.$days[$group[$count-1]];        
                    break;    
                    
            }
            echo 
    ', ';
        }

    parse('02346');
    parse('02356');
    will give:
    monday, wednesday through friday, sunday,
    monday, wednesday & thursday, saturday & sunday,
    No idea how it compares to your current attempt though...don't know how I'd do it any more elegantly :|

  • #7
    Regular Coder
    Join Date
    Aug 2002
    Location
    Oregon, United States of America
    Posts
    882
    Thanks
    1
    Thanked 9 Times in 9 Posts
    PHP Code:
    if( strlen($row['days']) > ){ // If it happens on more than one day
                
    $days str_split($row['days']);
                
    // Go through the days...
                
    for( $x=0$x<sizeof($days); $x++ ){
                    
    // If the next day in the list comes after this one...
                    
    if($days[$x+1] == $days[$x]+1){
                        
    $temp .= $dayNames[$days[$x]]. ' through ';
                        
    // Skip ahead to the end of the range
                        
    while( $days[$x+1] == $days[$x]+){ $x++; }
                        
    $temp .= $dayNames[$days[$x]];
                    
    // Otherwise print the day
                    
    } else {
                        
    $temp .= $dayNames[$days[$x]];
                    }
                }
            
    // If it only happens on one day
            
    } else {
                
    $temp .= 'on '.$dayNames$row['days'] ];
            } 
    My code is ugly, but it works.

    However, your code is cleaner, and out puts nicer. GJay, you understood exactly what i needed. Thanks for everyones help.

    Now i just need to look at the two scripts, and see which is more effective.
    If I'm postin here, I NEED YOUR HELP!!

  • #8
    Regular Coder
    Join Date
    Aug 2002
    Location
    Oregon, United States of America
    Posts
    882
    Thanks
    1
    Thanked 9 Times in 9 Posts
    GJay, could you help me out a little and explain what this part does, so I can write this kind of thing on my own in the future?

    PHP Code:
    $gid=$cur=$string{0}; 
        
    $groups[$cur][]=$cur
        for(
    $i=1;$i<strlen($string);$i++) { 
            if(
    $string{$i}==$cur+1) { 
                
    $groups[$gid][]=$string{$i}; 
                
    $cur=$string{$i}; 
            } 
            else { 
                
    $gid=$cur=$string{$i}; 
                
    $groups[$gid][]=$string{$i}; 
            } 

        } 
    Thanks again!
    If I'm postin here, I NEED YOUR HELP!!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •