Keyword Match, reward available.

Discussion in 'PHP' started by blueparukia, Jan 26, 2008.

  1. #1
    OK, I have a mysql column called "description". In it is some text like:

    .

    And another one:

    Now, say a variable is equal to "internet" or "random", it returns the first one, else if it is equal to "rainbow" or "rainbow swims", it returns the second one. How do I match that to the description? I suppose it will involve regex :(.


    So basically you need to pull the description from the databse (which I know how to do) and if that variable has any words that are in the description, to match it to it.

    Now, I know this is hard for anyone, but if you do it I will, umm....service you sexually.

    No, I will give you rh1no.com (unused domain I bought for a friend), and rep.


    Thanks a lot,

    BP
     
    blueparukia, Jan 26, 2008 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    $keywords = 'rainbow swims';
    
    $keywords = preg_replace('~[^\w-\. ]+', ' ', trim($keywords));
    $keywords = preg_quote($keywords);
    $keywords = array_unique(explode(' ', $keywords));
    
    $query = sprintf("SELECT [field_names] FROM [table_name] WHERE `description` REGEXP '%s'", implode('|', $keywords));
    
    PHP:
    Give this a try.

    (Untested)
     
    nico_swd, Jan 27, 2008 IP
  3. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #3
    Thank you, but before I try it I have a question:

    What is that? I know it has something to do with escape strings, but where does it get its value from?


    Thanks a helluva lot though,

    BP
     
    blueparukia, Jan 27, 2008 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    It's for the string formatting using sprintf(). It's just to make the code more readable.
     
    nico_swd, Jan 27, 2008 IP
  5. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #5
    Hmm, this doesn't seem to work with mysql_fetch_array, I get a non valid MySQL Result resource, its probably something simple. Are the tables supposed to be in the []?.

    
    //Set the count to 0
    $counter = 0;
    
    //Select the right database  
    mysql_select_db($dbname) or die (mysql_error());
    
    $dk = $_POST['searchbox'];
    
    $dk = preg_replace('#~[^\w-\. ]+#', ' ', trim($dk));
    $dk = preg_quote($dk);
    $dk = array_unique(explode(' ', $dk));
    
    $keywords = sprintf("SELECT * FROM `forums` WHERE `sitedescripton` REGEXP '%s'", implode('|', $dk));
    require('header.php');
    
    //Output the data in divs.
    while($row = mysql_fetch_array($keywords))
    
    PHP:
     
    blueparukia, Jan 27, 2008 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    I'm not exactly sure why you modified my code, but the ~ shouldn't be in the regex line if you want to use # as delimiter.

    And you have to use mysql_query() on the query string before passing it to mysql_fetch_array().

    
    $keywords = sprintf("SELECT * FROM `forums` WHERE `sitedescripton` REGEXP '%s'", implode('|', $dk));
    $keywords = mysql_query($keywords) OR die(mysql_error());
    
    PHP:
    My example just showed how to prepare the query string.

    And no, the tables shouldn't be in brackets.
     
    nico_swd, Jan 27, 2008 IP
  7. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #7
    I have never felt this way abot another man before :p

    I adding the #s because you didn't have the "~" at the end, and I thought ~ did something, and I lost my cheatsheet :p

    But yeah, it works (well),

    Thanks so much,

    Josh
     
    blueparukia, Jan 27, 2008 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    Hehe, oops, yes I missed that.


    Anyway, glad you got it working.
     
    nico_swd, Jan 27, 2008 IP
  9. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #9
    So am I. Anyway, you want the domain? Its a crap domain, yes, but I can pint it at your nameservers should you want it.
     
    blueparukia, Jan 27, 2008 IP
  10. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #10
    Nah, thanks. It's alright. I've got far too many domains already anyway. :p
     
    nico_swd, Jan 27, 2008 IP
  11. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #11
    OK, this is not crucial, but would it be possible to select from 2 or more columns?

    Thanks
     
    blueparukia, Jan 28, 2008 IP
  12. zerostar07

    zerostar07 Peon

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    big regexps may be too slow, why don't you do it with simple 'LIKE' queries

    $words = explore($query);
    $res = mysql_query("SELECT * FROM somewhere WHERE (description LIKE '%$query".join( "%' OR DESCRIPTION LIKE('%", $words)."%' ");
     
    zerostar07, Feb 2, 2008 IP