Jump to content

Limiting characters displayed from database help


ow-design

Recommended Posts

Hello,

 

I need help as i cannot figure this out with other coding i have tried.

 

If you have a look at the following site: baseyouthproject.org.uk

you will see under 'Ask Auntie Annie' that i need a title pulled from a mysql database but only to be displayed with limited characters to neatly be displayed over 3 lines.

 

I could really do with help with this and if possible i also need help to pull the newest 3 titles to be displated underneath each other as shown on site.

 

If anybody could help it would be greatly appreciated as this one has really got me!

 

Thank you.

Link to comment
Share on other sites

What ignace posted will work to query a substring of the value, but it will not take into consideration where words begin and end. Typically in these situations you want the content split between words, otherwise what get's displayed can havfe a very different meaning than what it should.

For example, this

Danielle Smith has a great asset in her ability to manage multiple projects simultaneously.

Could become this

Danielle Smith has a great ass...

I would suggest that you instead pull the entire string from the database and use PHP to break the text between words at a length equal to or less than the specified length. Here is a script which will do that (however, I'm sure there is a more efficient way to do this with a regex expression):

function part($string, $maxChar)
{
    if (strlen($string) <= $maxChar) { return $string; }
    $string = substr($string, 0, $maxChar+1);
    $string = substr($string, ' ', strrpos($string, ' '));
    return $string . '...';
}

Edit: Also it will be impossible to ensure that you will get exactly enough content to fill exactly three lines unless you change the font being used to a fixed-width font. That is where each character has exactly the same with as every other character. So, ten 'W's will be the same with as 10 'I's. Typically that is not a good font type to use for "copy" on a page, but is good for code.

 

Fixed Width:

WWWWWWWWWW

IIIIIIIIII

 

Non Fixed-Width:

WWWWWWWWWW

IIIIIIIIII

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.