I have a PHP web application which an option of generating invoices. The Problem is i need to print the invoices using the dot matrix printer in draft mode. I have tries, but it prints like a normal printer with bigger fonts. simply it prints in the format as same in the page.
I need to print like receipts. Do I need to format anything with the PHP?
Well, I think you need to print in raw mode.
I do not claim I have completely solved this issue but here are my tries.
Firstly, there should be a "generic / text only" driver on the system where the dot matrix printer is connected to, probably in port LPT1.
Secondly, you should send ESCP commands to the printer and not use the build in processes of php to print.
You could for example create an array of 135 * 66 characters, where 135 is the usual page width in characters and 66 the usual number of printing lines (on a 24 pin dot matrix printer with a 8-1/2" x 12" paper size). You should be careful to fill the array with characters and not with strings, lie this:
$GRAND_ARRAY = array();
$maxCharactersPerLine = 135;
$maxLines = 66;
//initialize the array with spaces and the last column of \r\n
for($i=0; $i<$maxLines; $i++){
$pos = $i*$maxCharactersPerLine + ($maxCharactersPerLine-1) + $i + 1;
$value = "\r\n";
$this->printChars($GRAND_ARRAY, $pos, $value);
}
//make an array that fits every one character of the page
//plus a column for the "next line" character
$totalCells = $maxCharactersPerLine * $maxLines + $maxLines - 1;
//what you want to print
$value = "name:"; //here is a string with 5 characters
//where you want to print it on the page
//$line = 2
//$column = 5
//consider that array starts at 0 and not 1
//$pos = ($column - 1) * $maxCharactersPerLine + $line + $column - 2;
$pos = 275; //position is 5th character in line 2
//do not just $GRAND_ARRAY[$pos] = $value because you should
//make sure that every array cell has only one character
$this->printChars($GRAND_ARRAY, $pos, $value, 1);
// also included a flag if you want to print only the $limit characters of $newstring
public function printChars(&$mainArray, $start, $newString, $limit=0){
$j = $start;
$charsArray = $this->mb_str_split($newString);
//$len = mb_strlen($newString, "UTF-8"); //can't remember why not this
$len = count($charsArray);
for($i=0; $i<$len; $i++){
if($limit > 0 && $limit < $i){
return;
}else{
$mainArray[$j] = $charsArray[$i];
$j = $j + 1;
}
}
}
//this I found here on s.o. i think.
public function mb_str_split( $string ) {
# Split at all position not after the start: ^
# and not before the end: $
return preg_split('/(?<!^)(?!$)/u', $string );
}
After creating the array I used qz-print plugin (https://code.google.com/p/jzebra/) that includes javascript functions and a java applet in order to send this array to the "generic / text only" dot matrix printer in the client.
The javascript function I use is:
//just before that, I turn the array into string str.
function printESCP2(str, type, quality, pitch, line_spacing, topOffset, commands) {
if (notReady()) { return; }
qz.appendHex("x1Bx40"); //ESC # for reset settings
var j = commands.length;
for(var k=0;k<j;k++){
console.log(':'+commands[k]);
qz.appendHex(commands[k]);
}
if(topOffset){
if(topOffset > 0){
alert("topOffset: "+topOffset);
for(var k=0; k<topOffset; k++){
qz.appendHex("x0D"); //CR for carriage return
qz.appendHex("x0A"); //LF for line feed
}
}
}
if(quality == <?php echo SettingsPrinter::FONT_QUALITY_LQ; ?>){
alert("font quality: LQ");
qz.appendHex("x1Bx78x31"); // ESC x 1 for LQ (31=1)
}else{
alert("font quality: draft");
qz.appendHex("x1Bx78x30"); // ESC x 0 for Draft (30=0)
}
if(line_spacing == <?php echo SettingsPrinter::PRINTER_LINE_SPACING_NARROW; ?>){
//alert("line spacing: narrow (1/8)");
qz.appendHex("x1Bx30"); //ESC 0 for line spacing minimum (1/8)
}else{
//alert("line spacing: normal (1/6)");
qz.appendHex("x1Bx32"); //ESC 2 for line spacing normal (1/6)
}
switch(pitch){
case '<?php echo SettingsPrinter::FONT_PITCH_10; ?>':
alert("pitch: 10");
qz.appendHex("x1Bx50"); //ESC P for pitch 10 cpi
break;
case '<?php echo SettingsPrinter::FONT_PITCH_12; ?>':
alert("pitch: 12");
qz.appendHex("x1Bx4D"); //ESC M for pitch 12 cpi
break;
case '<?php echo SettingsPrinter::FONT_PITCH_15; ?>':
alert("pitch: 15");
qz.appendHex("x1Bx67"); // ESC g for pitch 15 cpi
break;
case '<?php echo SettingsPrinter::FONT_PITCH_17; ?>':
alert("pitch: 17");
qz.appendHex("x1Bx50"); //ESC P for pitch 10 cpi
qz.appendHex("x0F"); // SI for condensed resulting in pitch 17 cpi
break;
case '<?php echo SettingsPrinter::FONT_PITCH_20; ?>':
alert("pitch: 20");
qz.appendHex("x1Bx4D"); //ESC M for pitch 12 cpi
qz.appendHex("x0F"); // SI for condensed resulting in pitch 20 cpi
break;
default:
alert("pitch unknown -- not set");
break;
}
qz.append(str);
qz.appendHex("x0D"); //CR for carriage return
qz.appendHex("x1Bx40"); //ESC # for reset settings
window["qzDoneAppending"] = function() {
// Tell the apple to print.
qz.print();
// Remove any reference to this function
window['qzDoneAppending'] = null;
};
}
One can actually create the array of characters-to-print as a two dimension array and ommit the last column of \r\n as I think this is not well accepted by all dot matrix printers. In that case one should do
//for each line of the array
qz.append($line); //line-1 turned into string, line-2 etc
qz.appendHex("x0D"); //CR for carriage return after each line
One problem that I came across is that if the user chose 17CPI then every character holds less space in the paper. This means that you cannot depend on one space characters to spread your data across the entire page in this case.
If anyone thinks they can contribute to this they are most welcome.
Related
Good evening
I created a while adding spaces in a variable . it's exactly what I need, but now I need to centralize the text between the spaces:
here is the code in PHP:
$psn = "ABCDEFGH";
$psnSize = strlen($psn);
while($psnSize <= 20)
{
$psn = $psn." ";
$psnSize++;
}
The max size is 20 characters, and I need all psn's have the same size (20 characters)
I'm getting the result but the spaces are added to the end of the text and now I want to distribute the spaces between the text to get it centralized.
Thank you so much.
You can use all native functions to achieve this:
function createLRPadding($str, $chars = 20)
{
# Count the length
$strlen = strlen($str);
# Don't do anything if you are at the max characters
if($strlen >= $chars)
return $str;
# Get left and right balance
$len = ($chars - $strlen) / 2;
# Create fills on the left and right of the string and implode them to make one string
return implode('', array_fill(0,round($len, 0, PHP_ROUND_HALF_UP),' ')).$str.implode('', array_fill(0,round($len, 0, PHP_ROUND_HALF_DOWN),' '));
}
To use:
echo createLRPadding('Cool string');
To adjust the padding, add a second parameter:
echo createLRPadding('Cool string', 40);
The first example (default 20 pad) will write:
Cool string
Is there a term for the idea of storing large numbers as letters? For example let's say I have the (relatively small) number 138201162401719 and I want to shrink the number of characters (I know this does not help with saving disk space) to the fewest possible number of characters. There are 26 letters in the English alphabet (but i count them as 25 since we need a zero letter). If I start splitting up my large number into pieces that are each 25 or less I get:
13, 8, 20, 11, 6, 24, 0, 17, 19
If I then count the numbers of the alphabet a=0, b=1, c=2, d=3... I can convert this to:
NIULGYART
So I went from 15 digits long (138201162401719) to 9 characters long (NIULGYART). This could of course be easily converted back to the original number as well.
So...my first question is "Does this have a name" and my second "Does anyone have PHP code that will do the conversion (in both directions)?"
I am looking for proper terminology so that I can do my own research in Google...though working code examples are cool too.
This only possible if you're considering to store your number before processing as a string. Because you can't store huge number as integers. You will lost the precision (13820116240171986468445 will be stored as 1.3820116240172E+22) so the alot of digits are lost.
If you're considering storing the number as a string this will be your answer:
Functions used: intval, chr and preg_match_all.
<?php
$regex = '/(2[0-5])|(1[0-9])|([0-9])/';
$numberString = '138201162401719';
preg_match_all($regex, $numberString, $numberArray, PREG_SET_ORDER);
echo($numberString . " -> ");
foreach($numberArray as $value){
$character = chr (intval($value[0]) + 65);
echo($character);
}
?>
Demo
This is the result:
138201162401719 -> NIULGYART
Here's how I would do it:
Store the big number as a string and split it into an array of numbers containing one digit each
Loop through the array extract 2-digit chunks using substr()
Check if the number is less than 26 (in which case, it is an alphabet) and add them to an array
Use array_map() with chr() to create a new array of characters from the above array
Implode the resulting array to get the cipher
In code:
$str = '138201162401719';
$arr = str_split($str);
$i = 0; // starting from the left
while ($i < count($arr)) {
$n = substr($str, $i, 2);
$firstchar = substr($n, 0, 1);
if ($n < 26 && $firstchar != 0) {
$result[] = substr($str, $i, 2);
$i += 2; // advance two characters
} else {
$result[] = substr($str, $i, 1);
$i++; // advance one character
}
}
$output = array_map(function($n) {
return chr($n+65);
}, $result);
echo implode($output); // => NIULGYART
Demo.
As an alternative, you could convert the input integer to express it in base 26, instead of base 10. Something like (pseudocode):
func convertBase26(num)
if (num < 0)
return "-" & convertBase26(-num) // '&' is concatenate.
else if (num = 0)
return "A"
endif
output = "";
while (num > 0)
output <- ('A' + num MOD 26) & output // Modulus operator.
num <- num DIV 26 // Integer division.
endwhile
return output
endfunc
This uses A = 0, B = 1, up to Z = 25 and standard place notation: 26 = BA. Obviously a base conversion is easily reversible.
strtr() is a magnificent tool for this task! It replaces the longest match as is traverses the string.
Code: (Demo)
function toAlpha ($num) {
return strtr($num, range("A", "Z"));
}
$string = toAlpha("138201162401719");
echo "$string\n";
$string = toAlpha("123456789012345");
echo "$string\n";
$string = toAlpha("101112131415161");
echo "$string\n";
$string = toAlpha("2625242322212019");
echo "$string";
Output:
NIULGYART
MDEFGHIJAMDEF
KLMNOPQB
CGZYXWVUT
Just flip the lookup array to reverse the conversion: https://3v4l.org/YsFZu
Merged: https://3v4l.org/u3NQ5
Of course, I must mention that there is a vulnerability with converting a sequence of letters to numbers and back to letters. Consider BB becomes 11 then is mistaken for eleven which would traslate to L when converted again.
There are ways to mitigate this by adjusting the lookup array, but that may not be necessary/favorable depending on program requirements.
And here is another consideration from CodeReview.
I have been trying to do the same thing in PHP without success.
Assuming I'm using the 26 letters of the English alphabet, starting with A = 0 down to Z as 25:
I find the highest power of 26 lower than the number I am encoding. I divide it by the best power of 26 I found. Of the result I take away the integer, convert it to a letter and multiply the decimals by 26. I keep doing that until I get a whole number. It's ok to get a zero as it's an A, but if it has decimals it must be multiplied.
For 1 billion which is DGEHTYM and it's done in 6 loops obviously. Although my answer demonstrates how to encode, I'm afraid it does not help doing so on PHP which is what I'm trying to do myself. I hope the algorithm helps people out there though.
I'm trying to build some PHP code to send SMS through telnet to a SIM server, but I'm having trouble in sending concatenated messages.
I've read some things about using padding bits to make the coded message septets into octets, but I don't fully understand how it works.
I have a class that receives the phone number, the message (already split in 153chars maximum), the total number of SMSs and the order number of the present part of text.
It works as long as I have that '20' added before the $hexmessage. But I get a junk char in the beginning of the first part (before the 1st letter of my msg), and the same junk char replacing the first letter of the second part! (using '20' so it would show a blank space, but it shows a triangle)
I can't understand why, or what I'd had to change for it to work properly.
I hope that someone can help me understand what am I doing wrong.
Here's what I've got so far:
<?php
// Generate PDU string
public function generatePDUm($receiverNumber,$message, $sms_count, $msg_nr) {
//Append filler digit if number is national
if( strlen($receiverNumber)==9){
$nacional=1;
$receiverNumber = $receiverNumber."F";
$network = substr($receiverNumber, 0, 2); //NETWORK code, used to decide the SIM Card to be used
//Check for international flags and set the number type accordingly
}else{
$nacional=0;
if(substr($receiverNumber, 0, 1)=='+'){
$network = substr($receiverNumber, 4, 2); //NETWORK code, used to decide the SIM Card to be used
$receiverNumber = substr($receiverNumber, 1, 12); //remove international indicator
}
else if(substr($receiverNumber, 0, 2)== '00'){
$network = substr($receiverNumber, 5, 2); //NETWORK code, used to decide the SIM Card to be used
$receiverNumber = substr($receiverNumber, 2, 12); //remove international indicator
}
}
/* Flag the network to be used */
switch ($network){
case "92":
$network="TMN";
break;
case "96":
$network="TMN";
break;
case "91":
$network="VODAFONE";
break;
case "93":
$network="OPTIMUS";
break;
}
// Receiver number must be 10 characters long ('national nr' + filler digit) or less than 13 ('351'+'national nr'). (Portugal)
if( strlen($receiverNumber) < 10 || strlen($receiverNumber) > 12) {
// Error, not 10 or over 12 numbers long (Code 1)
$this->setErrorCode(1);
return false;
}
// Message must be 2 characters long at least
if( strlen($message) < 2 ) {
// Error, message too short (Code 2)
$this->setErrorCode(2);
return false;
}
// Message can't be longer than 153 characters. 3SMS.
if( strlen($message) > 153 ) {
// Error, message too long (Code 3)
$this->setErrorCode(3);
return false;
}
// Length of servicecenter number (00 = automatically fixed by phone)
$serviceCenterNumberLength = '00';
// SMS-? : 04=sms-deliver(recieve), 11=sms-submit, 01 = dont know but it works, 41 = SMS-SUBMIT + UDH bit (for extended/concatenated SMS)
// You can try to change this if your phone does not work with 01 command try to use 11 command
$smsType = '41';
// TP Message Reference: (placeholder), let the phone set the message reference number itself
$messageRef = '00';
// Number length. If national -> 9, if international -> 12
if($nacional==1){
$numberLength = '09';
}else{
$numberLength = '0C';
}
// Type of phone adress: (81=Unknown=10dec, 91=InternationalFormat, 92=National?)
if($nacional==1){
$numberType = '81';
}else{
$numberType = '91';
}
// Get the PDU version of the number
$number = $this->getNumberAsPDU( $receiverNumber );
// TP-PID (Protocol Identifier)
$protocolId = '00';
// TP-DCS (Data coding scheme)
$dataCodingScheme = '00';
// TP-Validity-Period (timestamp), AA=4days expiry, disabled for SonyEricsson support.
// $validityPeriod = 'A0';
// $validityPeriod = 'AA'; // Add this if the PDU command fails
/*user data header information (05 - User Data header info length
* 00 - Information element identifier for a concatenated short message
* 03 - Information element data length
* 00 - Reference number, auto
* 0.$sms_count - total SMS nr
* 0.$msg_nr - current SMS order */
$udhi = '050003000'.$sms_count.'0'.$msg_nr;
// echo 'UDHinfo: '.$udhi."\n";
// Data length of message (in hex format)
$dataLength = $this->strToHexLen($message);
// echo 'DATA LENGHT: '.$dataLength."\n\n";
// Convert message, string > 7bits > 8bits > hex
$hexMessage = $this->bit7tohex( $this->strto7bit( $message ) );
// Create the complete PDU string
$pdu = $serviceCenterNumberLength . $smsType . $messageRef . $numberLength .
$numberType . $number . $protocolId . $dataCodingScheme . $dataLength .
$udhi . '20' .$hexMessage;
/*
* Generate the length of var $pdu (pdu/2 minus 1) as pdu format requests
* The -1 is because we don't count the first two characters '00', needed for this command: 'cmgs=24'
*/
$cmgslen = strlen($pdu)/2-1;
// Build data array to return with required information
$data = array();
$data['pdu'] = $pdu;
$data['cmgslen'] = $cmgslen;
$data['rede'] = $network;
// Return the data array with PDU information
return $data;
}
// Generate PDU formatted cellphone number
private function getNumberAsPDU($number) {
// Length of number divided by 2 handle two characters each time
$length = strlen( $number )/2;
// Set counter to 1 for strlen
$i = 1;
$pduNumber = '';
// Loop to handle every 2 characters of the phone number. 06 12 34 56 78
while ($i <= $length) {
// Get 2 characters of the complete string depending on the number of the current loop.
// Then reverse these 2 characters and put them in var $pduNumber (06 = 60)
$pduNumber .= strrev( substr( $number,$i*2-2,2) );
// Counter + 1
$i++;
}
// Return the generated number
return $pduNumber;
}
/* Function to convert ascii character to 8 bits
* Much more efficient than holding a complete ASCII table
* Thanks to Mattijs F.
*/
private function asc2bin($input, $length=8) {
$bin_out = '';
// Loop through every character in the string
for($charCount=0; $charCount < strlen($input); $charCount++) {
$charAscii = ord($input{$charCount}); // ascii value of character
$charBinary = decbin($charAscii); // decimal to binary
$charBinary = str_pad($charBinary, $length, '0', STR_PAD_LEFT);
$bin_out .= $charBinary;
}
// Return complete generated string
return $bin_out;
}
// String to 7 bits array
private function strto7bit($message) {
$message = trim($message);
$length = strlen( $message );
$i = 1;
$bitArray = array();
// Loop through every character in the string
while ($i <= $length) {
// Convert this character to a 7 bits value and insert it into the array
$bitArray[] = $this->asc2bin( substr( $message ,$i-1,1) ,7);
$i++;
}
// Return array containing 7 bits values
return $bitArray;
}
// Convert 8 bits binary string to hex values (like F2)
private function bit8tohex($bin, $padding=false, $uppercase=true) {
$hex = '';
// Last item for counter (for-loop)
$last = strlen($bin)-1;
// Loop for every item
for($i=0; $i<=$last; $i++) {
$hex += $bin[$last-$i] * pow(2,$i);
}
// Convert from decimal to hexadecimal
$hex = dechex($hex);
// Add a 0 (zero) if there is only 1 value returned, like 'F'
if($padding && strlen($hex) < 2 ) {
$hex = '0'.$hex;
}
// If we want the output returned as UPPERCASE do this
if($uppercase) {
$hex = strtoupper($hex);
}
// Return the hexadecimal value
return $hex;
}
// Convert 7 bits binary to hex, 7 bits > 8 bits > hex
private function bit7tohex($bits) {
$i = 0;
$hexOutput = '';
$running = true;
// For every 7 bits character array item
while($running) {
if(count($bits)==$i+1) {
$running = false;
}
$value = $bits[$i];
if($value=='') {
$i++;
continue;
}
// Convert the 7 bits value to the 8 bits value
// Merge a part of the next array element and a part of the current one
// Default: new value is current value
$new = $value;
if(key_exists(($i+1), $bits)) {
// There is a next array item so make it 8 bits
$neededChar = 8 - strlen($value);
// Get the char;s from the next array item
$charFromNext = substr($bits[$i+1], -$neededChar);
// Remove used bit's from next array item
$bits[$i+1] = substr($bits[$i+1], 0, strlen($bits[$i+1])-$neededChar );
// New value is characters from next value and current value
$new = $charFromNext.$value;
}
if($new!='') {
// Always make 8 bits
$new = str_pad($new, 8, '0', STR_PAD_LEFT);
// The 8 bits to hex conversion
$hexOutput .= $this->bit8tohex($new, true);
}
$i++;
}
// Return the 7bits->8bits->hexadecimal generated value
return $hexOutput;
}
// String to length in Hex, String > StringLength > Hex
private function strToHexLen($message) {
// Length of the string (message)
$length = strlen( $message )+7; //+7 for UDH. the UDH is a total of (number of octets x bit size of octets) 6 x 8 = 48 bits long. Therefore a single bit of padding has to be prepended to the message. The UDH is therefore (bits for UDH / bits per septet) = (48 + 1)/7 = 7 septets in length.
// Hex value of this string length
$hex = dechex($length);
// Length of the hex value
$hexLength = strlen($hex);
// If the hex strng length is lower dan 2
if($hexLength < 2) {
// Add a 0 (zero) before it
$hex = '0'.$hex;
}
// Return the hex value in UPPERCASE characters
return strtoupper($hex);
}
}
?>
As you are already aware, creating concatenated SMS messages requires you to add a UDH before your text message. The UDH becomes part of your payload, thus reducing the number of characters you can send per segment.
As it has become part of your payload, it needs to confirm with your payloads data requirement - which is 7 bit. The UDH however, is 8 bit, which clearly complicates things.
Consider the UDH of the following:
050003000302
05 is the length of the UDH
00 is the IEI
03 is the IEDL (3 more octets)
00 is a reference (this number must be the same in each of your concatenated message UDH's)
03 is the maximum number of messages
02 is the current message number.
This is 6 octets in total - equating to 48 bits. This is all and well, but since the UDH is actually part of your SMS message, what you have to do is add more bits so that the actual message starts on a septet boundary. A septet boundary is every 7 bits, so in this case, we will have to add 1 more bit of data to make the UDH 49 bits, and then we can add our standard GSM-7 encoded characters.
You can read up more about this from Here
These questions are all over the place and no one seems to be able to answer them in way that makes any sense. Zero padding usually just makes things worse. I think easiest way to go around this design flaw in GMS standard is to use 8 bit encoding or 16-bit UCS2 even that it means less characters. In that way you don't need to care about difference in byte boundaries which is the reason creating concatenated SMS is so hard.
I'm trying to write a function that'll convert an integer to a string like this, but I can't figure out the logic... :(
1 = a
5 = e
27 = aa
28 = ab
etc...
Can anyone help? I'm really niffed that I can't wrap my head around how to write this... :(
Long list of them here:
/*
* Convert an integer to a string of uppercase letters (A-Z, AA-ZZ, AAA-ZZZ, etc.)
*/
function num2alpha($n)
{
for($r = ""; $n >= 0; $n = intval($n / 26) - 1)
$r = chr($n%26 + 0x41) . $r;
return $r;
}
/*
* Convert a string of uppercase letters to an integer.
*/
function alpha2num($a)
{
$l = strlen($a);
$n = 0;
for($i = 0; $i < $l; $i++)
$n = $n*26 + ord($a[$i]) - 0x40;
return $n-1;
}
I'll add this answer to sum up the comments regarding the misuse of base-26.
A common first reaction when confronted with this problem is to think "There are 26 letters, so this must be base-26! All I need to do is map each letter to its corresponding number".
But this is not base-26. It's easy to see why: there is no zero!
In base-26, the number twenty-six is the first number with two digits, and is written "10". In this counting system, twenty-six has a single digit, "Z", and the first two-digit number is twenty-seven.
But what if we make A=0, ..., Z=25? This way we have a zero and the first two-digit number becomes twenty-six. So far so good. How do we write twenty-six now? That's "AA". But... isn't A=0? Ooops! A = AA = AAA = "0" = "00" = "000".
You will have to use base_convert to convert your numbers to a 26 base:
base_convert(35, 10, 26);
That gives you the individual components in numbers from 1 - p, so 35 becomes 19 (1 * 26 + 9). Then you have to map the individual components to your desired set, so 1 => a, 9 => i, a => j, etc. and 19 becomes ai.
Well, you're pretty much converting from base 10 to base 26. Base 10 has digits 0-9, whereas base 26 can be expressed with "digits" A-Z. Conversion from base-10 is easy - see e.g. this: http://www.mathsisfun.com/base-conversion-method.html
Edit: actually, base-26 fails to account for multiple equivalent ways to write 0 ( 0 = 00 = 000).
void convert(int number)
{
string str = "";
while(number)
{
char ch;
ch = (number - 1) % 26 + 65;
str = ch + str;
number = (number-1) / 26;
}
cout << str << endl;
}
I have a PHP loop that adds data into a table cell. However, I want to apply a static size to the table cell, so if more data is returned than can fit inside the cell I want the excess characters to be cut off and end with a "..."
For example, one data entry has 270 characters, but only the first 100 are displayed in the table cell. Follow by a "..."
Any ideas on how to do this?
Thanks!
if (strlen($str) > 100) $str = substr($str, 0, 100) . "...";
You can use mb_strimwidth
printf('<td>%s</td>', mb_strimwidth($cellContent, 0, 100, '…'));
If you want to truncate with respect to word boundaries, see
Truncate a multibyte String to n chars
You can also control content display with the CSS property text-overflow: ellipsis
http://www.quirksmode.org/css/textoverflow.html
Unfortunately, browser support varies.
function print_dots($message, $length = 100) {
if(strlen($message) >= $length + 3) {
$message = substr($message, 0, $length) . '...';
}
echo $message;
}
print_dots($long_text);
$table_cell_data = ""; // This would hold the data in the cell
$cell_limit = 100; // This would be the limit of characters you wanted
// Check if table cell data is greater than the limit
if(strlen($table_cell_data) > $cell_limit) {
// this is to keep the character limit to 100 instead of 103. OPTIONAL
$sub_string = $cell_limit - 3;
// Take the sub string and append the ...
$table_cell_data = substr($table_cell_data,0,$sub_string)."...";
}
// Testing output
echo $table_cell_data."<br />\n";