Reputation: 3335
I'm familiar with PHP truncating text based on the maximum number of characters reached, however I am wanting to tweak this from characters to limit text to 10 lines before truncating it.
How could I go about in achieving this?
Here is what I'm using at the moment to limit the number of characters:
<?php $str = $profile['bio'];
$max = 510;
if(strlen($str) > $max) {
$str = substr($str, 0, $max) . '...'; } ?>
<?php echo $str ?>
Upvotes: 1
Views: 3703
Reputation: 2098
I think your best bet is to use pure css to limit the height of your text/container.
What is a "line" of text? Pure text written in a form field? Text from editor perhaps full of html tags inside? Utf8 text with foreign characters?
I fail to see a common pattern to the phrase "line of text" so as to use whatever method to limit its length (thus its height).
If you still want to limit it with php, then i suggest using length limiters. There are countless posts here and on the web in general. But you should be careful of encoded data (non latin ones)
Upvotes: 1
Reputation: 96159
e.g.
<?php
$subject = data();
$p = "![\r\n]+!";
$subject = preg_split($p, $subject, 11);
$subject = array_slice($subject, 0, 10);
echo join("\r\n", $subject);
function data() {
return <<< eot
Mary had a little lamb,
whose fleece was white as snow.
And everywhere that Mary went,
the lamb was sure to go.
It followed her to school one day
which was against the rule.
It made the children laugh and play,
to see a lamb at school.
And so the teacher turned it out,
but still it lingered near,
And waited patiently about,
till Mary did appear.
"Why does the lamb love Mary so?"
the eager children cry.
"Why, Mary loves the lamb, you know."
the teacher did reply.
eot;
}
prints
Mary had a little lamb,
whose fleece was white as snow.
And everywhere that Mary went,
the lamb was sure to go.
It followed her to school one day
which was against the rule.
It made the children laugh and play,
to see a lamb at school.
And so the teacher turned it out,
but still it lingered near,
Upvotes: 0
Reputation: 39532
Use explode()
to turn the text into an array of lines, array_slice()
to limit the amount of lines, and then implode()
to put it all back together again:
<?php
$text = "long\nline\ntext\nhere";
$lines = explode("\n", $text);
$lines = array_slice($lines, 0, 10); //10 is how many lines you want to keep
$text = implode("\n", $lines);
?>
Upvotes: 2
Reputation: 21272
You can use this function:
<?php
// Original PHP code by Chirp Internet: www.chirp.com.au
// Please acknowledge use of this code by including this header.
function truncateLongText ($string, $limit, $break=".", $pad="...") {
// return with no change if string is shorter than $limit
$string = strip_tags($string, '<b><i><u><a><s><br><strong><em>');
if(strlen($string) <= $limit)
return $string;
// is $break present between $limit and the end of the string?
if ( false !== ($breakpoint = strpos($string, $break, $limit)) ) {
if($breakpoint < strlen($string) - 1) {
$string = substr($string, 0, $breakpoint) . $pad;
}
}
return $string;
}
Example usage:
$text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.";
echo truncateLongText($text, 10);
// Lorem Ipsum is simply dummy text of the printing and typesetting industry...
Upvotes: -1