Ultragames 02-08-2006, 09:49 PM 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.
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?
Element 02-08-2006, 10:20 PM you could simply use str_replace()
<?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($str, 0, strlen($str)-1);
}
return $str;
}
echo returnDates('023');
?>
Thats a function using it.
:confused:
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...
Element 02-08-2006, 10:39 PM 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.
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 1 :
echo $days[$key];
break;
case 2 :
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 :|
Ultragames 02-08-2006, 11:42 PM if( strlen($row['days']) > 1 ){ // 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]+1 ){ $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.
Ultragames 02-08-2006, 11:49 PM 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?
$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!
|
|