dannybrazil Posted April 2, 2008 Share Posted April 2, 2008 hello does some one knows how can i make the text quierd from my databas to show lets say 10(char) and .... example : instead of : hello my name is danny and i love football i want : hello my name is danny... and its a link so when they click it it goes to another page that they can see it all thanks Quote Link to comment Share on other sites More sharing options...
Caesar Posted April 2, 2008 Share Posted April 2, 2008 <?php $str = 'hello my name is danny and i love football'; $short_desc = '<a href="index.php?p='.$id.'" target="_parent">'.substr($str, 0, 10).'...</a>'; // Change the '10' to whatever number of chars you wish to show ?> PHP substr() function. Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 2, 2008 Share Posted April 2, 2008 Well, you would probably want to include some logic to only show elipses if the value goes beyond 10 characters. Also, when using that functionality it is typically done only at word breaks - otherwise you might have some unintended consequences: <?php $text = "I need an association between point A and point B"; echo substr($str, 0, 13) . '...'; //Output "I need an ass..." ?> To do this properly you would need to decide that if 10 characters (or whatever you use) will break a word, do you want to take one less word or one more word. This code will take the last word that meets or exceeds the length given <?php function partialString($input, $length) { $words = explode(' ', $input); $output = ''; while ((strlen($output)+1)<=$length && count($words)>0) { $output .= array_shift($words) . ' '; } $output = trim($output); $elpise = (strlen($output)<strlen($input))?' ...':''; return $output . $elpise; } $str = 'hello my name is danny and i love football'; echo partialString($str, 10); //Output "hello my name ..." ?> Quote Link to comment Share on other sites More sharing options...
gluck Posted April 2, 2008 Share Posted April 2, 2008 hello does some one knows how can i make the text quierd from my databas to show lets say 10(char) and .... example : instead of : hello my name is danny and i love football i want : hello my name is danny... and its a link so when they click it it goes to another page that they can see it all thanks use substring() directly in the query and you wouldn't need to do any php logic. Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 2, 2008 Share Posted April 2, 2008 use substring() directly in the query and you wouldn't need to do any php logic. But, will you know whether the string was shortened or not so you know whether or not to show the elipses? Also, it won't prevent words from being cut inappropriately. Quote Link to comment 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.