How could I get the number of lines, from a LONGTEXT
String that we retrieved from a MySQL database, using HTML, or PHP? For example, Let's say I have this text:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam imperdiet condimentum libero at eleifend. Mauris ac justo in purus tempor iaculis vitae at turpis. Cras quis facilisis libero. Nam molestie neque id lacus cursus rutrum. Aliquam vestibulum blandit diam, a placerat
I would like to be able to find out how many lines, or how much "room" the text takes up.
It is one line, yet, it appears as 3 lines, so, I would like to get 3
, by doing something like $num = count_lines($string);
using PHP, or something more complicated than that.
This is for formatting a forums website that I'm making, changing certain aspects dynamically based on how much room the text takes up
I've tried things like:
<?php
$num = substr_count($string, "
");
?>
and
<?php
$num = count(preg_split('/
|/',$str));
?>
But they don't work because I'm getting the string out of a MySQL database.
Also, I would like the number to change based on the margin. As you can see, the number of lines in:
jsfiddle.net/n58z74pg is different from the number of lines in jsfiddle.net/6nkvLzzn because the text has a style of margin-right:10cm
.
So, is there any way to get the number of lines in a string using only HTML, or PHP? Maybe by counting the number of pixels that the text takes up, then doing some basic math to find out the number of lines?
It sounds like you just need the height of the text, getting the number of lines would mean you still have to multiply that by lineHeight.
To get the actual width (in pixels) of a div on your page you can simply use the following JS
document.getElementById("DivID").clientHeight;
Resizing in PHP is going to be harder, and you also have to consider the fact that then you have to make a web call every time you need to resize things. (Not very web 2.0) PHP (your server) typically won't actually know the width of user's browsers etc... Users can explicitly resize things so usually graphics and layout should be handled clientside.
If there are no line breaks in the string you retrieve from database, then it logically can't be devided in 3 lines. Think about it: "line", apart from being broken by a line break, is a relative thing. In order to know how many lines will the string take up you have to know what size of container and size of letters will be in your website.
This question will be much easier answered if you clarified why you needed to know
As others have mentioned, you can't limit the number of lines from the database because you won't know how much text will represent three lines. This is what I would do. Retrieve the entire text and put it into a div like so
<div class="post"><?PHP echo $longtext; ?></div>
Then in your CSS do this
div.post {
height: 3em;
overflow: hidden
}
This will give you a div that is three lines high (3em) and hides anything more. You may have to adjust a little liek 3.25em or so to adjust for line spacing.