Text formatting issues...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
icarpenter
Forum Commoner
Posts: 84
Joined: Mon Mar 07, 2005 8:12 am
Location: Kent, England

Text formatting issues...

Post by icarpenter »

Hi can anyone help?

I am trying to send an email in text format but I cannot get all of my values to display at equal intervals on the page without manually altering the tab amounts for each label. I intend to use the $emess variable in a loop but the labels may differ in length and I would like to display all values in the same area on the page without using HTML...

Does anyone no of an easy way this can be done?

Code: Select all

<pre>
<?
$value1='test data1';
$value2='test data2';
$value3='test data3';
$value4='test data4';

$to = "someone@somewhere.com";

$subject = "My subject";

$emess= "Test1:\t$value1\n";
$emess.= "Long label2:$label2\t$value2\n";
$emess.= "3:$label3\t$value3\n";
$emess.= "Test4:$label4\t$value4\n";

$headers .= "My Website <web@mysite.com>\r\n";

mail($to, $subject, $emess, $headers);
echo"$emess";
?>
</pre>
Thanks Ian...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

suggestion: don't use tab. Tab is varying size in differing clients. Instead, use sprintf() to align with spaces for you. :)
User avatar
icarpenter
Forum Commoner
Posts: 84
Joined: Mon Mar 07, 2005 8:12 am
Location: Kent, England

Post by icarpenter »

I have tried using the following but the formatting the data displayed in my second column is still inconsistant:

This is what gets printed:

Code: Select all

Test1:	test data1
Long label2: test data2 
3:	test data3
Test4:	test data4
This is what I am trying to acheive...

Code: Select all

Test1:	       test data1
Long label2:	 test data2 
3:	           test data3
Test4:	       test data4
I tried using:

Code: Select all

$value2='%f test data2';
sprintf($value2,'            ');
But this gave the same results as using \t as this was spacing from the last character of the left column.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you're supposed to use the alignment rules in sprintf().. %10s for instance..
User avatar
icarpenter
Forum Commoner
Posts: 84
Joined: Mon Mar 07, 2005 8:12 am
Location: Kent, England

Post by icarpenter »

I have just tried the following but this is still seems to be taking its reference point from the last character of the preceeding text...

I tried...

Code: Select all

$value1='test1';
$value2='test2';

$test = sprintf('%20s',$value1);
$test1 = sprintf('%20s',$value2);

$emess= "Long label2:$test\n";
$emess.= "3:$test1\n";

echo"$emess";
Do you know of anything else I could try?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

You can always count the length of your label, and compare it to a set value of how far the next column should be.

Code: Select all

$spaces = 30;

$output = ($spaces - count($word).$spaces.$label);
A rough, very rough, example ;).
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The label should be sent through sprintf's alignment stuff to pad out the right side for the rest of the line.
User avatar
icarpenter
Forum Commoner
Posts: 84
Joined: Mon Mar 07, 2005 8:12 am
Location: Kent, England

Eureka!

Post by icarpenter »

Thats got it :D ...Many Thanks Fayd,Jcart...

For those who are interested...

The following:

Code: Select all

$value1='test1';
$value2='test2';

$test1 = sprintf('%-20s',$label1='Long label 2:');
$test2 = sprintf('%-20s',$label2='3:');

$emess= "$test1 $value1\n";
$emess.= "$test2 $value2\n";
Prints...

Code: Select all

Long label 2:        test1
3:                   test2
Post Reply