SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Thread: regexp

  1. #1
    SitePoint Guru
    Join Date
    Apr 2001
    Location
    BC, Canada
    Posts
    630
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    regexp

    Im using the following query

    $result=mysql_query("select * from articles where article_body regexp'.*$keyword.*'");

    The result is a case sensitive search, is there a way to do the same, but not case sensitive?


    thanx

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $result=mysql_query("select * from articles where article_body regexp'.*$keyword.*'");

    to


    $result=mysql_query("select * from articles where article_body regexp'%$keyword%'");


    BTW you don't need to . in there since the expression is encapsulated by ""
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    SitePoint Guru
    Join Date
    Apr 2001
    Location
    BC, Canada
    Posts
    630
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey, when I use the % signs, I dont get any results??
    any ideas?

  4. #4
    imagine no limitations exbabylon's Avatar
    Join Date
    Dec 2000
    Location
    Idaho, USA
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    is there an advantage of this method over LIKE?
    Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.

    Exbabylon- Professional Internet Services

  5. #5
    SitePoint Guru
    Join Date
    Apr 2001
    Location
    BC, Canada
    Posts
    630
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no idea, how does LIKE work?

  6. #6
    imagine no limitations exbabylon's Avatar
    Join Date
    Dec 2000
    Location
    Idaho, USA
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Like so (no pun intended)

    SELECT * FROM table WHERE column_name LIKE '%$keyword%';

    Just selects a whatever has PART of that in it...
    Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.

    Exbabylon- Professional Internet Services

  7. #7
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Doh!

    Sorry yes you will want use LIKe for finding case insensitive searches. Basically it says match anything with whatever is between the % signs.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  8. #8
    imagine no limitations exbabylon's Avatar
    Join Date
    Dec 2000
    Location
    Idaho, USA
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Mr. Freddydoesphp,

    I have never heard of the regexp command before (not that I remember) in MySQL. How does that work? And is there any usefull purpose. If you have a link to a manual that would be appreciated, no need for you to re-word anything for me.

    Thanks!

    Alex
    Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.

    Exbabylon- Professional Internet Services

  9. #9
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    http://mysql.he.net/documentation/my...ce.html#IDX674

    You'll have to read up on it, I am not sure how it works off the top of my head.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  10. #10
    imagine no limitations exbabylon's Avatar
    Join Date
    Dec 2000
    Location
    Idaho, USA
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you! As I mentioned in another post I took the time to read through that page several times, but I guess I missed the REGEXP. However, as I looked through another page: MySQL Regular Expression Syntax I found that it will be quite usefull in the future when creating a search engine for a site or for providing searching of long articles in a database. No longer will LIKE be used for exerything

    Example For Those Who Care:
    Code:
    SELECT id,column,author FROM table WHERE column REGEXP '[[:<:]]$search_string[[:>:]]' AND date + 0 > CURDATE() - 2;
    That will match only the KEYWORDS... not just whatever.

    Off the top here is what one might have for a search engine using REXEXP:
    PHP Code:
    // search.php

    // Find out what type of search to do. Look for any or exact.
    if($type == "any"){
        
    $keyword explode($keyword);
        
    $e 0;
        foreach(
    $keyword as count){
            
    $e++;
        }
        
    $i 0;
        
    $search "";
        foreach(
    $keyword as words){
            if(
    $i $e 1){
                
    $search .= "'[[:<:]]$words[[:>:]]' or ";
                
    $i++;
            } else {
                
    $search .= "'[[:<:]]$words[[:>:]]'";
            }
        }
    } else {
        
    $search "'[[:<:]]$keyword[[:>:]]'";
    }

    if(
    $date){
        
    $date_search "";
        if(
    $date == "newer"){
            
    $date_search .= " and (date > '$old_date')";
        } elseif(
    $date == "older"){
            
    $date_search .= " and (date < '$old_date')";
        } else {
            
    $date_search .= " and (date > '$old_date' and date < '$new_date')";
        }

    $sql "SELECT id,content,title,author,email FROM table WHERE (content REXEXP $search) or (title REXEXP $search)$date_search";
    $result mysql_query($sql);
    if(!
    $result){
        echo(
    "Database Error");
    } else {
        while(
    $row mysql_fetch_array($result)){
            
    extract($row);
            echo(
    "<a href=\"display.php?id=$id\">$title</a>- by $author<br>" substring($content0200) . "... <a href=\"display.php?id=$id\">[Read More]</a><p>");
        }

    // The Odds Are That The Above Code Will Not Work.It is merely for examples sake, and may or may not function properly.

    God Bless, and thanks Freddy,

    Alex
    Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.

    Exbabylon- Professional Internet Services


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •