hello, I want to make a function which will display all the words which has got more than 3 characters from database. Can anybody help me? Thank you all.
This is PHP. u mean running the query? $query = mysql_query("SELECT column_foo FROM table_bar WHERE CHAR_LENGTH(word_baz) > 3"); PHP:
I mean as a function using php. not a sql query. I am working on this. but no luck function jumbleWords($theString){ $data = spliti(" ", $theString); $toReturn = ""; shuffle($data); foreach ($data as $word) { if(strlen($word) >= 3){ $toReturn .= $word . " "; } } return $toReturn; } PHP:
preg_match_all('#\b([a-z]{3,})\b#i', $theString, $three_letter_words); print_r($three_letter_words); Code (markup):
he wants it coming from the db so i think you need a where condition SELECT column_foo FROM table_bar WHERE CHAR_LENGTH(word_baz) > 3; i think that one is the correct approach
That depends on what the data looks like inside the database. You don't want to be using substring type functions in SQL queries if it's a TEXT/LONGTEXT column. Doing so would basically make indexes useless.
well, if it OP does not have a gazillion data then it's perfectly ok, also mysql works a lot quicker and more efficient than php so, if i guess if you do a benchmark using the query as against the function you created, the query will perform a lot faster. by the way preg_match_all is an overkill here.
preg_match_all is the best solution given what I know about the situation. It will return every 3+ letter word in a string despite punctuation, linebreaks, etc. Trying to emulate this with SQL is going to work slopily at best, not to mention port from system-to-system about as easily as stuffing a square block through a round hole.
Why the !$%§ would you pump all of your strings into memory and then filter out the small ones if SQL can do this for you? A PHP filtering approach is just wrong in this case.
Because MySQLs memory is better used maintaining indexes. If you start doing string substitutions in SQL you're going to start flushing index caches and slowing down the performance of the rest of the application. I'm sorry to say it, but you guys are just wrong. Use an unbuffered query & work with each row in PHP from there.