RE: [PHP] Splitting Text

From: Date: Tue, 07 Aug 2001 21:49:39 +0000
Subject: RE: [PHP] Splitting Text
Groups: php.general 
Request: Send a blank email to php-general+get-61708@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

« previous php.general (#61708) next »