Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialEmil Wallgren
11,737 PointsExcerpt in PDO
Hi Y'all!
After doing a fetchAll for the table for my blog, I am doing a foreach loop where I echo the results to the screen. This works fine for a blog listing...But what if I would like to limit the text of the blog to create an excerpt? Maybe 50 characters?
Got any ideas?
Regards!
/Emil
1 Answer
Greg Kaleka
39,021 PointsAs long as you can store your blog posts in a string, you could use substr()
, which takes a string and a length as parameters and returns a substring. You can optionally add a parameter for where to start (default is character 0).
For example:
$long_string = "You could use substr(), which takes a string and a length as parameters and returns a substring. You can optionally add a parameter for where to start (default is character 0).";
$short_string = substr($long_string, 9); // "You could"
Emil Wallgren
11,737 PointsEmil Wallgren
11,737 PointsThanks Greg :-) Worked perfectly!
Greg Kaleka
39,021 PointsGreg Kaleka
39,021 PointsNice! Happy to help :).