Jump to content

aligning text in an email


mihomes

Recommended Posts

Okay, this email is basically an order confirmation email which contains cart contents that is sent to me and the customer.  I prefer to send the emails in plain/text so there are no html issues and it is cleaner.  Here is an example of the email sent :

 

Date: Wed Dec 31, 2008 10:41 am
Invoice ID: 5B92BBD94
Coupon: hohoho
IP: 111.111.111.111

<!-- BEGIN CARTITEMS -->
2 W4556 Blue Widgets $62.55 $125.10
<!-- BEGIN CARTITEMS -->
1 f4578f Red Widgets $109.35 $109.35
<!-- BEGIN CARTITEMS -->
1 d45789 Green Widgets $166.05 $166.05
<!-- END CARTITEMS -->

Out of stock items (these will ship separately at no additional charge when.... and so on

 

The above comes to me in the form of a variable $message and then is sent to myself and the customer in an email.  What I want to do is align the cart items in columns.  I attempted to do this with tabs, however, I noticed not all email clients insert the tabs (such as outlook express) even when sent in plain/text format.

 

So, what I think I need to do is use str_pad for each of the items.  Removing the tag lines <!-- is no problem at all and can be done with str_replace.  How the heck can I go about aligning the items though?  There is qty, id, desc, price, and total for each cart item.  I'm thinking if I use str_pad somehow for each I can insert a certain number of whitespaces for each by knowing the longest possible string length of each column.  I do not even need to figure this out as I know already what these are.  So... any help on this.  I am pretty quick when it comes to coding, but not familiar with php, what functions are available, or how to go about this.

 

Any help would be really appreciated.

Link to comment
Share on other sites

Well, a hard way is to get the longest strlen() of each "column" and pad the shorter row data with spaces... and view them with a monospaced font.

 

That's kinda of what I am leaning for - as of now I am just inserting spaces, but of course, nothing is aligned.  The part that is getting me is grabbing just that section out of the variable.

Link to comment
Share on other sites

Consider this example.  If you could get your data to be in the same form, you could use something like this:

(it may not be the best way, but it is a possibility)

<?php
error_reporting(E_ALL);

$rows = array("2|W4556|Blue Widgets|$62.55|$125.10",
              "1|f4578f|Red Widgets|$109.35|$109.35",
              "1|d45789|Green Widgets|$166.05|$166.05");
$data = array();

foreach($rows as $row){
  $data[] = split('\|',$row);
}
$lengths = array(1,1,1,1,1);

foreach($data as $d){
  foreach($d as $key => $val){
    $dataLen = strlen($d[$key]);
    $curLen = strlen($lengths[$key]);

    $lengths[$key] = ($dataLen > $curLen ) ? $dataLen : $curLen;
  }
}

echo '<pre>';
foreach($data as $d){
  foreach($d as $key => $val){
    $len = $lengths[$key] + 2;
      echo str_pad($val, $len, " ");
  }
  echo "\n";
}
echo '</pre>';
?>

Link to comment
Share on other sites

Consider this example.  If you could get your data to be in the same form, you could use something like this:

(it may not be the best way, but it is a possibility)

<?php
error_reporting(E_ALL);

$rows = array("2|W4556|Blue Widgets|$62.55|$125.10",
              "1|f4578f|Red Widgets|$109.35|$109.35",
              "1|d45789|Green Widgets|$166.05|$166.05");
$data = array();

foreach($rows as $row){
  $data[] = split('\|',$row);
}
$lengths = array(1,1,1,1,1);

foreach($data as $d){
  foreach($d as $key => $val){
    $dataLen = strlen($d[$key]);
    $curLen = strlen($lengths[$key]);

    $lengths[$key] = ($dataLen > $curLen ) ? $dataLen : $curLen;
  }
}

echo '<pre>';
foreach($data as $d){
  foreach($d as $key => $val){
    $len = $lengths[$key] + 2;
      echo str_pad($val, $len, " ");
  }
  echo "\n";
}
echo '</pre>';
?>

There lies the problem - since the entire content of the email comes to me as $message I don't know how to go about grabbing simply that section (cart contents) to perform something like this... then... inserting it back in to boot.

 

My logic is to create a string starting at <!-- BEGIN and ending at <!-- END.  That would be the entire cart contents. Copy string to another variable where I can strip out everything but "2|W4556|Blue Widgets|$62.55|$125.10" and save as an array then perform what I need to as far as whitespaces to create yet a third string then do a str_replace on copied string with new string on $message.  Well, lets see how this goes lol

Link to comment
Share on other sites

I guess I didn't read clearly enough, sorry.

 

If the message is like that, you could use a regular expression to pull the data btwn the first <!-- BEGIN CARTITEMS--> and <!-- END CARTITEMS -->

 

something like preg_match('/<!-- BEGIN CARTITEMS -->(.)+<!-- END CARTITEMS -->/'); maybe..

from there you could str_replace(<!-- BEGIN CARTITEMS -->)

 

then a split on \n or \r\n depending.. and then you could format the non blank lines.. the only difficult is that you can't just straight "space" split/delimit it since the item name can have spaces..

 

See what you want to do, or perhaps someone has a better idea.

Link to comment
Share on other sites

Part way there:

$str = <<<EOD
Date: Wed Dec 31, 2008 10:41 am
Invoice ID: 5B92BBD94
Coupon: hohoho
IP: 111.111.111.111

<!-- BEGIN CARTITEMS -->
2 W4556 Blue Widgets $62.55 $125.10
<!-- BEGIN CARTITEMS -->
1 f4578f Red Widgets $109.35 $109.35
<!-- BEGIN CARTITEMS -->
1 d45789 Green Widgets $166.05 $166.05
<!-- END CARTITEMS -->

Out of stock items (these will ship separately at no additional charge when.... and so on
EOD;

$match = array();
preg_match('/<!-- BEGIN CARTITEMS -->(.+)<!-- END CARTITEMS -->/si', $str, $match);
$match = explode("\n",$match[0]);

echo '<pre>';
print_r($match);
echo '</pre>';

Link to comment
Share on other sites

Not the prettiest, but it does the job the way I am given the contents... I did this in html so I could test it.  Only thing left is to string replace the original part with $new.  Although, I just realized that this whole thing is pointless without using a fixed width font  :-\ haha so really wont serve the purpose in a plain/text email.

 

preg_match('/<!-- BEGIN CARTITEMS -->(.+)<!-- END CARTITEMS -->/si', $message, $match);
$match = explode("|",$match[0]);

$count = 0;
$new .= "<font face='Courier'>";
while ($count < count($match)-1){
$count++;
$new .= str_pad($match[$count++], 10, " ", STR_PAD_LEFT);
$new .= str_pad($match[$count++], 15, " ", STR_PAD_LEFT);
$new .= str_pad($match[$count++], 50, " ", STR_PAD_LEFT);
$new .= str_pad($match[$count++], 20, " ", STR_PAD_LEFT);
$new .= str_pad($match[$count++], 20, " ", STR_PAD_LEFT);
$new .= "<br>";
}
$new .= "</font>";
echo $new;
;?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.