evie001001 Posted March 6, 2008 Share Posted March 6, 2008 I am new to PHP to please bear with me! I am displaying information from my database (bindings) on my page... in the form of text. Is there anyway I could cut the text short to display only a few characters? For example, i'd like the job descriptions in this page: http://impserver.bournemouth.ac.uk/~emoore/jobs.php to be cut short so that a user can click 'more' and navigate to a separate page with full descriptions. Any ideas? Thanks Evie Quote Link to comment Share on other sites More sharing options...
bradkenyon Posted March 6, 2008 Share Posted March 6, 2008 http://www.php.net/substr substr($text, 0, 32); $text is the string you want to cut, then 0 is the starting point, 0 being the beginning, then 32 being the 32nd character in the string. Quote Link to comment Share on other sites More sharing options...
craygo Posted March 6, 2008 Share Posted March 6, 2008 use substr() $text = substr($databasefield, 0, 100); change 100 to the number of characters you would like to display Ray Quote Link to comment Share on other sites More sharing options...
richardw Posted March 6, 2008 Share Posted March 6, 2008 here is one more solution, its an old one of mine and code heavy but it gives flexibility to trim where you want and set a trimming spread before the break <?php $posttext = "Soon you will know just how easy it is to build unncessaarily long routines. Take a look at counting words and seperating at a space or a period. This routine works well based on the trimto length"; $str_len = strlen($posttext); if ($str_len >= 75) { $str_len = trim(strlen($posttext)); $trimto = 65; $select = 8; // eight characters either way to a break $i = 0; for ($i = 0; $i < $trimto+$select; $i++) { $current_char = substr($posttext,$i,1); if ($current_char == " " ) { $cutlength = $i; } } $textout = (substr($posttext, $str_len= 0, $cutlength)); } echo $textout; ?> 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.