I just want to display the words which has got more than 3 letters.

Discussion in 'PHP' started by baris22, Nov 1, 2008.

  1. #1
    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.
     
    baris22, Nov 1, 2008 IP
  2. keyaa

    keyaa Peon

    Messages:
    137
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #2
    SELECT column_foo FROM table_bar WHERE CHAR_LENGTH(word_baz) > 3;
    Code (markup):
     
    keyaa, Nov 1, 2008 IP
  3. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #3
    thanks for reply. How can i do this with php?
    any idea?

    thanks
     
    baris22, Nov 1, 2008 IP
  4. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #4
    This is PHP.

    u mean running the query?

    
    $query = mysql_query("SELECT column_foo FROM table_bar WHERE CHAR_LENGTH(word_baz) > 3");
    
    PHP:
     
    ads2help, Nov 1, 2008 IP
  5. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #5
    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:
     
    baris22, Nov 1, 2008 IP
  6. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #6
    preg_match_all('#\b([a-z]{3,})\b#i', $theString, $three_letter_words);
    print_r($three_letter_words);
    Code (markup):
     
    joebert, Nov 1, 2008 IP
  7. shineDarkly

    shineDarkly Banned

    Messages:
    241
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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
     
    shineDarkly, Nov 1, 2008 IP
  8. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #8
    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.
     
    joebert, Nov 1, 2008 IP
  9. shineDarkly

    shineDarkly Banned

    Messages:
    241
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #9
    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.
     
    shineDarkly, Nov 1, 2008 IP
  10. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #10
    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.
     
    joebert, Nov 1, 2008 IP
  11. shineDarkly

    shineDarkly Banned

    Messages:
    241
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #11
    then please try and do a benchmark with about 20k records and you will see what i mean :)
     
    shineDarkly, Nov 1, 2008 IP
  12. keyaa

    keyaa Peon

    Messages:
    137
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #12
    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.
     
    keyaa, Nov 2, 2008 IP
  13. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #13
    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.
     
    joebert, Nov 2, 2008 IP
    rohan_shenoy likes this.