1

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, "\n");
?>

and

<?php
  $num =  count(preg_split('/\n|\r/',$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?

Jojodmo
  • 23,357
  • 13
  • 65
  • 107
  • 1
    Actually, your example is one-liner. – Lewis Jul 05 '14 at 18:55
  • @Orion But it appears as 3 lines, I just clarified the question. My problem is that text inside of the MySQL database is 1 line. – Jojodmo Jul 05 '14 at 18:57
  • It's got nothing to do with getting the string from a database, it's the definition of "a line". You should better search for a Javascript library. Everything on the server side won't work by your definition. – VMai Jul 05 '14 at 18:58
  • You can force it appear in one line on page with `width="100000000px"` ~~. – Lewis Jul 05 '14 at 18:58
  • @Orion But this is for forums, and you don't want to be seeing a post with all of the text on one line. – Jojodmo Jul 05 '14 at 19:01
  • 1
    I think it's better to count `words` or `string length` instead of `lines`. – Lewis Jul 05 '14 at 19:02
  • possible duplicate of http://stackoverflow.com/questions/783899/how-can-i-count-text-lines-inside-an-dom-element-can-i – VMai Jul 05 '14 at 19:05
  • @Orion It's for changing certain aspects of the forum dynamically based on how much room the text takes up, not displaying how much the user has posted, if it were for displaying how much the user posted, I would totally agree with you. Guess I should have clarified that – Jojodmo Jul 05 '14 at 19:05
  • You need to count words and words length to prevent it from breaking your design and decide from there when to make a new line if needed. – Prix Jul 05 '14 at 19:09
  • 1
    May be `responsive design` is what you're looking for. – Lewis Jul 05 '14 at 19:11

3 Answers3

2

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.

Cheruvian
  • 5,628
  • 1
  • 24
  • 34
1

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

drakonli
  • 2,304
  • 23
  • 29
  • I did clarify: `This is for formatting a forums website that I'm making, changing certain aspects dynamically based on how much room the text takes up` – Jojodmo Jul 05 '14 at 19:11
  • yes, but what aspects exactly? also, dinamically means php or js? – drakonli Jul 05 '14 at 19:12
  • Images and colors of certain text/shapes. I would prefer to use `php` (It's what I talk about in the question). – Jojodmo Jul 05 '14 at 19:14
  • 1
    I suggest you use js to count line numbers based on what i'v explained earlier and here's a link: http://stackoverflow.com/questions/783899/how-can-i-count-text-lines-inside-an-dom-element-can-i – drakonli Jul 05 '14 at 19:21
0

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.

Robbert
  • 6,481
  • 5
  • 35
  • 61