SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    quick way of determining last character of variable

    hi,

    I have a fairly simple problem and I wonder whether it could be done more efficiently.
    my purpose is to run a mysql search and run
    PHP Code:
    LIKE %keyword% AND
    LIKE %keywords
    so i want to establish whether the last character is already an 's'. my current method would be
    PHP Code:
    $searchtxt =  'keyword';

    $length strlen($searchtxt);

    $last_char substr($searchtxt,$length-1,$length); 
    I guess its not the worst block of code but is there a quicker way?
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I think you probably want OR in the query, not AND, and this is only slightly less messy:

    PHP Code:
    //method one
    $last_char $searchtxt{strlen($searchtxt)-1};

    //method two
    $last_char substr($searchtxt, -1); 

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2005
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $last_char substr($searchtxt, -1); 

  4. #4
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cheers that is a definite improvement, and I did have to use OR as well
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  5. #5
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So let me make sure I understand: you're trying to do a search for keywords, and you want to look for both the singular and plural of said keyword, correct? I.e. if I want to search for 'orange' you'll run a query something like this:
    Code:
    SELECT * FROM table WHERE keyword LIKE '%orange%' OR keyword LIKE '%oranges%'
    Do I have you right?

    If so, then simply chop off your pluralized keyword, i.e. the above search for 'orange' would look like
    Code:
    SELECT * FROM table WHERE keyword LIKE '%orange%'
    When using the LIKE operator, the '%' character is a wildcard that matches zero or more characters. "Zero or more characters" includes the letter 's' and anything that might come after it. Thus your query is doubling the amount of work your poor little SQL server has to do and not gaining you anything at all.

  6. #6
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hello, fortunately im using a dedicated server and I rarely have an issue with resources - having said that I don't want to throw those resources around willy nilly when I don't have to.... so heres the thing (using our citrus friend as an example)....

    a search is run for "oranges". 25 records match oranges, but 75 match orange.

    If you were to enter oranges, I trim the 's' as well and run the extra parameter. Is there a better way to tackle this? It is a common scenario for my database where there are a few results with 's' on the end, so the search is only working should the singular term be entered - but as I can see through stats, people love to enter the collective term oranges.
    PHP Code:
    if (isset($_GET['search2'])) {

    $searchtxt2 =  strip_tags($_GET['search2']);
    $searchtxt2 =  mysql_real_escape_string($searchtxt2);

    } else {

    $last_char substr($searchtxt, -1);

    if (
    $last_char == 's') {
    $length strlen($searchtxt);
    $searchtxt2 substr($searchtxt,0,$length-1);
    } else {
    $searchtxt2 $searchtxt.'s';
    }


    *edit
    hangon -- brainwave -- always trim the 's' hmm I will need to ponder if this could ever go wrong
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  7. #7
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $searchtxt rtrim($searchtxt's'); 
    That is the quickest way to trim the last s if you decide to go that way.
    I assume your variable has already been made lowercase.

  8. #8
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thats a negative - at no stage have I made the variable lower case. Presumably you say this because your code will only strip lower case s's and not upper case? in which case I need strtolower? (never used this one before)

    im starting to feel the need for a search input function.
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  9. #9
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can just use strolower with what I wrote before, giving you this:
    PHP Code:
    $searchtxt rtrim(strtolower($searchtxt), 's'); 
    or you can do it as two separate lines of code...whichever you're more comfortable with.

  10. #10
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cheers cranial, I ended up writing a function
    PHP Code:
    #########
    # CLEAN SEARCH INPUT
    #########

    function search_input($value)
    {
        
    $value trim($value);
        
    $value strip_tags($value);
        
    $value mysql_real_escape_string($value);
        
    $value strtolower($value);
            
        if (
    substr($value, -1) == 's'$value rtrim($value,'s');
        
        return 
    $value;


    I was pondering what the effect of building as you suggested - does it evaluate and return each result as such rtrim(strtolower(strip_tags($value))) = 1[result](2[result](3[result]) or does it sortof calculate everything at once?

    for example in my function, would the below return the exact same results time and time again as the above?
    PHP Code:
    #########
    # CLEAN SEARCH INPUT
    #########

    function search_input($value)
    {
        
    $value trim(strip_tags(mysql_real_escape_string(sttolower($value)));
        
        if (
    substr($value, -1) == 's'$value rtrim($value,'s');
        
        return 
    $value;


    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  11. #11
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wheeler, the order in which you pass values through different functions does make a difference.

    you should not modify a value after you have escaped it for a database query. escaping is the last thing you do.

    you should also be aware of what rtrim() will do if your word ends in more than one 's'

  12. #12
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    FYI, unless you build (or download) a massive list of English words with their pluralized counterparts, you're going to get problems.

    Let's say I want to search for "chess". If you rtrim() the 's's off, you'll end up running your search for "che", but I'm not looking for "cheese" nor "checkers". Even if you go with a method that does nothing but strip off the final one 's', a search for "checkers" would return "checkered flag", or a search for "chess" would return "chesapeake".

    Honestly, for the purposes of building a search, I'd say simply use whatever the user gives you, or else get yourself a database of words with their pluralized counterparts and do a substitution based on that. (What if I want to find "virii"?)
    PHP questions? RTFM
    MySQL questions? RTFM

  13. #13
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks all i accept the weaknesses in my coding, however because i'm dealing with a predicatable product database I think in this case I (and the users) can live with it, it seems to work well just to trim the last 's' off.

    Coincidentally today I got a newsletter informing me about google's new inbuilt search mechanism (co-op) which probably far surpasses anything most of us could do because we have free reign of google's search algorithms, including error handling, similar words etc etc etc etc. i'm predicting an avalanche usage of this new tool rather than inbuilt search mechanisms -- as long as you can live with the compulsary adwords (linked to your adsense account)

    in my case my inbuilt search is generally better than G's for searching the product database, but I can't argue with its usage as a site-wide search feature
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development


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
  •