First Words
If you have ever need to grab the first few words of a string, then this simple function allows you to specify exactly how many words you want and even has a tail that defaults to three dots. This, of course, can be changed to a read more link or anything you like. Enjoy!
The output from the above script will look like this:
Heather was hoping to hop ...
I have recieved many requests from PHPRO.ORG regulars for a method of achieving this same result when SELECTing from a MySQL database, rather than SELECT a whole bunch of text and then drag it into PHP as a variable and then manipulate it. So lets imagine we have a table named animals with three fields, animal_id, animal_name, and animal_description. The animal_description field is a LARGETEXT field and we wish only to snarf the first fifteen words. Our SQL query would look like this:
SELECT
animal_id,
animal_name,
SUBSTRING_INDEX(animal_description,' ', 15) AS first_fifteen
FROM animals
animal_id,
animal_name,
SUBSTRING_INDEX(animal_description,' ', 15) AS first_fifteen
FROM animals
0 comments:
Post a Comment