Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-08-2006, 09:49 PM   PM User | #1
Ultragames
Regular Coder

 
Join Date: Aug 2002
Location: Oregon, United States of America
Posts: 882
Thanks: 1
Thanked 9 Times in 9 Posts
Ultragames has a little shameless behaviour in the past
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!!
Ultragames is offline   Reply With Quote
Old 02-08-2006, 09:59 PM   PM User | #2
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf will become famous soon enoughraf will become famous soon enough
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
raf is offline   Reply With Quote
Old 02-08-2006, 10:20 PM   PM User | #3
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 855
Thanks: 2
Thanked 2 Times in 2 Posts
Element is an unknown quantity at this point
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.
Element is offline   Reply With Quote
Old 02-08-2006, 10:29 PM   PM User | #4
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf will become famous soon enoughraf will become famous soon enough


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
raf is offline   Reply With Quote
Old 02-08-2006, 10:39 PM   PM User | #5
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 855
Thanks: 2
Thanked 2 Times in 2 Posts
Element is an unknown quantity at this point
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..
Element is offline   Reply With Quote
Old 02-08-2006, 11:06 PM   PM User | #6
GJay
Senior Coder

 
Join Date: Sep 2005
Posts: 1,791
Thanks: 5
Thanked 36 Times in 35 Posts
GJay is on a distinguished road
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 :|
GJay is offline   Reply With Quote
Old 02-08-2006, 11:42 PM   PM User | #7
Ultragames
Regular Coder

 
Join Date: Aug 2002
Location: Oregon, United States of America
Posts: 882
Thanks: 1
Thanked 9 Times in 9 Posts
Ultragames has a little shameless behaviour in the past
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!!
Ultragames is offline   Reply With Quote
Old 02-08-2006, 11:49 PM   PM User | #8
Ultragames
Regular Coder

 
Join Date: Aug 2002
Location: Oregon, United States of America
Posts: 882
Thanks: 1
Thanked 9 Times in 9 Posts
Ultragames has a little shameless behaviour in the past
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!!
Ultragames is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:09 AM.


Advertisement
Log in to turn off these ads.