Reputation: 454
is there a way in PHP to count the lines a string will give with a given width and a given line-height?
For example, if I have this text:
Sed eget lorem lorem. Pellentesque tristique, quam vel fringilla porttitor,
neque elit suscipit nisl, id posuere magna libero congue ante.
= 2 lines
Sed eget lorem lorem. Pellentesque
tristique, quam vel fringilla
porttitor, neque elit suscipit nisl,
id posuere magna libero congue ante.
= 4 lines because the width is not the same
Edit:
My question was not clear.
As Jason McCreary said, I want to create a formula (based on font-size, string length, line-height) with PHP that will get me close.
If someone could send me in the right direction to do that I'd appreciate, because I tried to create that magical formula with no result.
I can't use JavaScript in the page.
Thanks!!!!
Upvotes: 1
Views: 2666
Reputation: 158
You can do it with only CSS
<div style="width: 10em; height: 1.2em; overflow: hidden; text-overflow: ellipsis;">
Now a long woooooooooooooooooooooooooooooooord
</div>
Upvotes: 0
Reputation: 454
Finally here is what I did:
$charWidth = 5.6;
$divWidth = 550;
$wrappedContent = wordwrap($originalContent, ($divWidth / $charWidth), "\r\n");
$explodedLines = explode("\r\n", $wrappedContent);
$nbOfLine = count($explodedLines);
echo $nbOfLine;
No need to say that this is very approximative.
Upvotes: 4
Reputation: 73001
No. PHP is a server side technology. It has no knowledge of the resulting HTML rendering.
Instead, you could:
Check out functions like strlen()
and substr_count()
to help get you started with a PHP formula. They will help with line length and number of lines respectively. The line-height and font-size should be constant.
Upvotes: 3