Jump to content


Photo

Need help combing strings into one string always same length


  • Please log in to reply
4 replies to this topic

#1 Hardwarez

Hardwarez
  • Members
  • PipPipPip
  • Advanced Member
  • 37 posts

Posted 13 October 2006 - 04:16 PM

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..

	$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;
	}


#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 13 October 2006 - 07:18 PM

Here is a short script that does what I think you're trying to do:
<?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";
}
?>

This script echos the result, so you will have to modify it to make it work with your input & output.

Ken

#3 Hardwarez

Hardwarez
  • Members
  • PipPipPip
  • Advanced Member
  • 37 posts

Posted 16 October 2006 - 12:00 PM

Thank you! 

#4 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 16 October 2006 - 12:32 PM

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.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#5 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 16 October 2006 - 01:38 PM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users