RE: [PHP] splitting text after 25 words

From: Date: Wed, 08 Aug 2001 13:49:43 +0000
Subject: RE: [PHP] splitting text after 25 words
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-61825@lists.php.net to get a copy of this message
For future reference using explode and counting the words is not the correct way to do this. Your array could be huge, and if you have multiple requests to the page this would be a horrible idea. I believe the correct way to do this is to use MySQL inherent function SUBSTRING_INDEX(str,delim,count) So for example: SELECT SUBSTRING_INDEX(column_name, ' ', 20) FROM table_name would return the first 20 words. The php way is over-doing it. If you can perform the logic in your mysql query, you should. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - From MySQL manual: http://www.mysql.com/doc/S/t/String_functions.html SUBSTRING_INDEX(str,delim,count) Returns the substring from string str before count occurrences of the delimiter delim. If count is positive, everything to the left of the final delimiter (counting from the left) is returned. If count is negative, everything to the right of the final delimiter (counting from the right) is returned: mysql> select SUBSTRING_INDEX('www.mysql.com', '.', 2); -> 'www.mysql' mysql> select SUBSTRING_INDEX('www.mysql.com', '.', -2); -> 'mysql.com' This function is multi-byte safe. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Hope this helps! Robert V. Zwink DAID Development LLC http://www.zwink.net/daid.php -----Original Message----- From: Justin French [mailto:justin@indent.com.au] Sent: Wednesday, August 08, 2001 7:25 AM To: Chris Lambert; php Subject: Re: [PHP] splitting text after 25 words Thanks to all who replied, this solution by Chris was bar far the easiest to implement. Really appreciate it :) Chris Lambert wrote: > $teaser = explode(" ", $article, 26)); > array_pop($teaser); > $teaser = implode(" ", $teaser); Justin French -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net For additional commands, e-mail: php-general-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net

« previous php.general (#61825) next »