danny232 Posted July 10, 2013 Share Posted July 10, 2013 I've got a script running where I submit data into a form and that posts it into the database then posts the information onto a php page. I'm wanting the results returned to cutoff after 20 characters with "...." after it to show there is more (there will be then a 'click here to see more') I've tried searching for this numerous times but the results seem to direct to here: http://php.net/manual/en/function.substr.php I've read through that but that seems to discount everything and displays an error message saying there's too much to display. Can anyone help please? Quote Link to comment https://forums.phpfreaks.com/topic/280046-cutoff-string/ Share on other sites More sharing options...
DavidAM Posted July 10, 2013 Share Posted July 10, 2013 substr() returns a portion of a string. If you post some code and the (entire) error message, we may be able to help. Also, you might post the value being passed IN to the function and the value it RETURNS along with telling us what you EXPECT it to return. Quote Link to comment https://forums.phpfreaks.com/topic/280046-cutoff-string/#findComment-1440241 Share on other sites More sharing options...
litebearer Posted July 11, 2013 Share Posted July 11, 2013 teasers... http://www.brightcherry.co.uk/scribbles/php-how-to-show-an-excerptteaser-from-a-string/ Quote Link to comment https://forums.phpfreaks.com/topic/280046-cutoff-string/#findComment-1440247 Share on other sites More sharing options...
danny232 Posted July 12, 2013 Author Share Posted July 12, 2013 Thanks for the advice. Here is the code: <?php // Make a MySQL Connection $query = "SELECT * FROM jobs ORDER BY id DESC"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ echo "<strong>"; echo $row['title']. " <br /> </strong>". $row['body'] . " <br /><br />Salary: ". $row['wage'] . " <br />Closing Date: ". $row['expiry'] . " <br /><br />To apply for this position, please email your CV to ". $row['apply']; echo "<br /><br />"; } ?> <?php $charLimit = 25; $myText = "$body"; $teaserText = substr($myText,0,$charLimit) . "... "; echo $teaserText; ?> I understand this won't work the way I want it to due to the ordering but i've tried altering the code around and I get several errors. Basically I want it to read from the table "jobs" and input the row from the column "body" but only display 30 words then have "..." at the end. Quote Link to comment https://forums.phpfreaks.com/topic/280046-cutoff-string/#findComment-1440520 Share on other sites More sharing options...
Solution fastsol Posted July 12, 2013 Solution Share Posted July 12, 2013 What errors are you getting exactly. Based on what you posted, $body is not defined anywhere. Are you sure it's not supposed to be $row['body'] instead. Also you say you want the first 30 "words", substr() can't count the words, ony the characters. If you truly need the words then you'll need to do some more processing to achieve that. Here is a better example I think of your code <?php // Make a MySQL Connection $query = "SELECT * FROM jobs ORDER BY id DESC"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ $charLimit = 25; $teaserText = substr($row['body'],0,$charLimit) . "... "; echo "<strong>"; echo $row['title']. " <br /> </strong>". $teaserText . " <br /><br />Salary: ". $row['wage'] . " <br />Closing Date: ". $row['expiry'] . " <br /><br />To apply for this position, please email your CV to ". $row['apply']; echo "<br /><br />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/280046-cutoff-string/#findComment-1440525 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.