ow-design Posted August 1, 2009 Share Posted August 1, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/168402-limiting-characters-displayed-from-database-help/ Share on other sites More sharing options...
ignace Posted August 1, 2009 Share Posted August 1, 2009 SELECT substr(column, 0, length) as alias .. Quote Link to comment https://forums.phpfreaks.com/topic/168402-limiting-characters-displayed-from-database-help/#findComment-888347 Share on other sites More sharing options...
Psycho Posted August 1, 2009 Share Posted August 1, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/168402-limiting-characters-displayed-from-database-help/#findComment-888401 Share on other sites More sharing options...
.josh Posted August 1, 2009 Share Posted August 1, 2009 Alternatively... $length = 30; $string = "some random long line of text"; $string = wordwrap($string,$length); $string = explode("\n",$string); $string = $string[0] . "..."; Quote Link to comment https://forums.phpfreaks.com/topic/168402-limiting-characters-displayed-from-database-help/#findComment-888406 Share on other sites More sharing options...
ignace Posted August 1, 2009 Share Posted August 1, 2009 Danielle Smith has a great ass... I have no problem with that if it's true Quote Link to comment https://forums.phpfreaks.com/topic/168402-limiting-characters-displayed-from-database-help/#findComment-888417 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.