Hardwarez Posted October 13, 2006 Share Posted October 13, 2006 Ok, I am combining varables to make a label printing file. I need the width (or count) of the varables to always =40. For some reason I cant seem to get it to work. I need to add spaces between varables when total count is < 40 and I need to clip one varable when the total cound is over 40.Here is what I have..[code] $a=strlen($jobnumber); $n = 40; $n -= $a; $n -= 5; //max length value description, minus some pad space between varables if (strlen($descript) < $n){ $a=strlen($descript); $b=$n-$a; //number of chars extra that need to be added while ($b){ $descript = $descript . " "; //add spaces to end of string to make = n --$b; } } if (strlen($descript) > $n){ //clip string to lenght n $a=substr($descript, 0, $n); //Copy out max lenght of description to a $descript=$a; }//combine description and jobnumber on one line and add the minimum space between varables $descript = $descript . ' ' . $jobnum; $thedata = $client; \\ CSV FILE . ',' . $descript . ',' . $county . "\n"; fwrite($fh, $thedata); --$i; }[/code] Link to comment https://forums.phpfreaks.com/topic/23866-need-help-combing-strings-into-one-string-always-same-length/ Share on other sites More sharing options...
kenrbnsn Posted October 13, 2006 Share Posted October 13, 2006 Here is a short script that does what I think you're trying to do:[code]<?php$d = array('Short',str_pad('This string is 34 characters long',34,'*'), 'This is a very long description that just goes on and on and on and should get trumcated');foreach($d as $descrip) { $jobnumber = rand(1,99999); $testlen = strlen($descrip . ' ' . $jobnumber); switch(true) { case ($testlen == 40): echo '[' . $descrip . ' ' . $jobnumber . ']'; break; case ($testlen < 40): $padlen = 40 - $testlen + strlen($jobnumber); $newstr = $descrip . ' ' . str_pad($jobnumber,$padlen,' ',STR_PAD_LEFT); echo '['. $newstr .']'; break; default: $trunc = strlen($descrip) - ($testlen - 40); $descrip = substr($descrip,0,$trunc); $newstr = $descrip . ' ' . $jobnumber; echo '['.$newstr.']'; } echo "\n";}?>[/code]This script echos the result, so you will have to modify it to make it work with your input & output.Ken Link to comment https://forums.phpfreaks.com/topic/23866-need-help-combing-strings-into-one-string-always-same-length/#findComment-108552 Share on other sites More sharing options...
Hardwarez Posted October 16, 2006 Author Share Posted October 16, 2006 Thank you! Link to comment https://forums.phpfreaks.com/topic/23866-need-help-combing-strings-into-one-string-always-same-length/#findComment-109457 Share on other sites More sharing options...
redarrow Posted October 16, 2006 Share Posted October 16, 2006 kenrbnsn not sure but i think there might be a possabilty that the same number might come up twice from the rand() statement.so how does one solve that problam ha. Link to comment https://forums.phpfreaks.com/topic/23866-need-help-combing-strings-into-one-string-always-same-length/#findComment-109472 Share on other sites More sharing options...
kenrbnsn Posted October 16, 2006 Share Posted October 16, 2006 I was just using rand() to get a value for the $jobnumber variable, since I didn't know where this variable was being initialized in the OP's script. It was just an example.Ken Link to comment https://forums.phpfreaks.com/topic/23866-need-help-combing-strings-into-one-string-always-same-length/#findComment-109506 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.