Jump to content


Photo

Php Function To Split A Description Into Lines 68 Chars Long

php

  • Please log in to reply
7 replies to this topic

#1 kevinkhan

kevinkhan
  • Members
  • PipPipPip
  • Advanced Member
  • 142 posts

Posted 25 November 2012 - 05:00 PM

Im just wondering if anyone can help me out. Iv been trying to figure this out all day.
I have these variables

$booking['occasion']
$booking['date']
$booking['venue']

and I want to make a description out of them like this.

$description = "Deposit for supplying a DJ for an $occasion on the
$date in the $venue";

I want to display the description over 3 or 4 lines in a pdf file like this

$pdf->writeHTMLCell(139, 5, '20', '125', $line1, '', 1, 1, true, 'L', true);
$pdf->writeHTMLCell(139, 5, '25', '125', $line2, '', 1, 1, true, 'L', true);
$pdf->writeHTMLCell(139, 5, '30', '125', $line3, '', 1, 1, true, 'L', true);
$pdf->writeHTMLCell(139, 5, '35', '125', $line4, '', 1, 1, true, 'L', true);
$pdf->writeHTMLCell(139, 5, '40', '125', $line5, '', 1, 1, true, 'L', true);

I want to wrap the description to a next line after every 68 characters but only want to wrap after completed words

Can anyone help me create a function to split the desicprtion into 3 or 4 lines so far I have this code which I know is along the lines that I want.

$description = "Deposit for suppling a DJ and Equipment for a $occasion on the
$date in $venue";
$decriptionLength = strlen($description);
if($decriptionLength <= 68){
    $line1 = $description;    
}
elseif($decriptionLength > 68)
{
    $line1 = substr($description, 0, 68);
    $line2 = substr($description, 68, 136);
}

This code will wrap the text even in the middle of a word so I don’t want this.
I know this might be a lot to ask but if anyone can come up with some code I would be greatly appreciated.

#2 Pikachu2000

Pikachu2000
  • Staff Alumni
  • I hate everything.
  • 11,384 posts
  • LocationFuture Independent Republic of Texas
  • Age:106

Posted 25 November 2012 - 05:11 PM

wordwrap with the last parameter set to TRUE.

"Java" is to "Javascript" about the same as "fun" is to "funeral".

Why $_SERVER['PHP_SELF'] is bad. || Why ORDER BY RAND() is bad || Every problem can be solved with rm -rf *

Random Quote: "

Remember, you're unique. Just like everybody else!


#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 17,294 posts
  • LocationCheshire, UK
  • Age:67

Posted 25 November 2012 - 10:26 PM

This code will wrap the text even in the middle of a word so I don’t want this.


wordwrap with last parameter set to false.

TRUE will split words.

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.







moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 Pikachu2000

Pikachu2000
  • Staff Alumni
  • I hate everything.
  • 11,384 posts
  • LocationFuture Independent Republic of Texas
  • Age:106

Posted 25 November 2012 - 10:34 PM

Right, but only if the word is over 68 characters, otherwise it splits on preceding spaces. The way I interpreted it the OP wanted to split on spaces where possible, but avoid exceeding 68 chars in any case. Maybe not, though.

"Java" is to "Javascript" about the same as "fun" is to "funeral".

Why $_SERVER['PHP_SELF'] is bad. || Why ORDER BY RAND() is bad || Every problem can be solved with rm -rf *

Random Quote: "

Remember, you're unique. Just like everybody else!


#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 17,294 posts
  • LocationCheshire, UK
  • Age:67

Posted 26 November 2012 - 12:18 AM

Right now I can't think of a 68 character word (even "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch" - google it) :happy-04:

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.







moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#6 Pikachu2000

Pikachu2000
  • Staff Alumni
  • I hate everything.
  • 11,384 posts
  • LocationFuture Independent Republic of Texas
  • Age:106

Posted 26 November 2012 - 12:42 AM

Well, I seem to remember that town. In Wales, isn't it?

"Java" is to "Javascript" about the same as "fun" is to "funeral".

Why $_SERVER['PHP_SELF'] is bad. || Why ORDER BY RAND() is bad || Every problem can be solved with rm -rf *

Random Quote: "

Remember, you're unique. Just like everybody else!


#7 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 17,294 posts
  • LocationCheshire, UK
  • Age:67

Posted 26 November 2012 - 01:00 AM

Full marks!
http://llanfairpwllg...es/llandad4.wav

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.







moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#8 Pikachu2000

Pikachu2000
  • Staff Alumni
  • I hate everything.
  • 11,384 posts
  • LocationFuture Independent Republic of Texas
  • Age:106

Posted 26 November 2012 - 04:18 AM

Makes me want to run off and buy some leeks . . .

"Java" is to "Javascript" about the same as "fun" is to "funeral".

Why $_SERVER['PHP_SELF'] is bad. || Why ORDER BY RAND() is bad || Every problem can be solved with rm -rf *

Random Quote: "

Remember, you're unique. Just like everybody else!





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users