Jump to content

Truncating strings to a specific number of lines (javascript/jquery)

I am retrieving a string from the YouTube API (the video description) and shortening it in my PHP code to 170 characters. However, when it displays, it's normally fine, but if it contains a URL, it will wrap and leave a lot of whitespace because I'm trying to display it in a narrow box, which results in some content overflowing. Is it possible to use javascript or JQuery to truncate the string on the client side so that it fits in the box and still ends in an ellipsis if truncated?

I can upload a screenshot if that would help.

HTTP/2 203

Link to comment
Share on other sites

Link to post
Share on other sites

Upload a screenshot or post a link to the site.

 

Why not hide overflow using CSS?

I would just hide the overflow, but it wouldn't put in an ellipsis. I will do that if there's no other practical way though.

ja5xsC6.png

It's currently not live on the internet.

HTTP/2 203

Link to comment
Share on other sites

Link to post
Share on other sites

I would just hide the overflow, but it wouldn't put in an ellipsis. I will do that if there's no other practical way though.

ja5xsC6.png

It's currently not live on the internet.

 

If you dont need the links, then you could use a regular expression to remove them. if you DO need the links there then you could replace use a regular expression to replace them with a hyperlink instead.

 

http://php.net/str_replace

http://www.php.net/manual/en/function.preg-replace.php

Link to comment
Share on other sites

Link to post
Share on other sites

If you dont need the links, then you could use a regular expression to remove them. if you DO need the links there then you could replace use a regular expression to replace them with a hyperlink instead.

 

http://php.net/str_replace

http://www.php.net/manual/en/function.preg-replace.php

It's supposed to be a sample of the description, like you get on the youtube subscription feed/search results. However, the links make it wrap onto the next line because they're too long. Thanks for the guidance on turning them into real links though, I will put that in. But is there any way to truncate the description if it's too long (on the left image it would cut down to everything above the blue line).

HTTP/2 203

Link to comment
Share on other sites

Link to post
Share on other sites

It's supposed to be a sample of the description, like you get on the youtube subscription feed/search results. However, the links make it wrap onto the next line because they're too long. Thanks for the guidance on turning them into real links though, I will put that in. But is there any way to truncate the description if it's too long (on the left image it would cut down to everything above the blue line).

yes, you could use substr() to shorten the string to a number of characters, and also replacing a url like: http://youtube.com/?watch=whatever with a link using http://y2u.be/ would help too.

 

 

Or perhaps expand the container element using a javascript animation when the user clicks/mouses over it or something

Link to comment
Share on other sites

Link to post
Share on other sites

yes, you could use substr() to shorten the string to a number of characters, and also replacing a url like: http://youtube.com/?watch=whatever with a link using http://y2u.be/ would help too.

 

 

Or perhaps expand the container element using a javascript animation when the user clicks/mouses over it or something

But shortening the number of characters won't help because I don't know how much to shorten it by. In the image, both descriptions have the same number of characters, but because one has a URL, it has wrapped onto a new line and the description is longer, overflowing out of the box.

The text is from the youtube API, so I have no control over what's actually in it, and I would rather not actually mess around with links and things in the descriptions of other people's videos.

HTTP/2 203

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×