match an exact word within a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nse111
    New Member
    • Jul 2008
    • 21

    match an exact word within a string

    Hey yaaaaaaa guys n gals!

    I want to know how I can match a whole word within a string using php.

    these are the results returned by the mysql query:

    tree:sweet:hous e:gate:tent
    candy:lollipop: choco:street:ch ild

    i want to search the word 'tree' wich I take from the user dynamically.

    when I use the following code:

    [CODE=PHP]
    if (isset($_POST['findtag']))

    {

    $tags = $_POST['txttags'];
    $result = mysql_query("se lect tags from tags") or die("MYSQL Returned: " . mysql_error());
    while($row = mysql_fetch_ass oc($result))
    {
    $temptag=$row['tags'];
    $strr2 = strstr($temptag , $tags);
    echo "$strr2"."< br />";

    }

    }
    [/CODE]

    I get the output as:

    tree:sweet:hous e:gate:tent
    tree

    here,
    the last line of the output for the searched criteria('tree' ) obviously comes from
    'street' in 'candy:lollipop :choco:street:c hild'

    but I want to view only data rows that has the whole word 'tree'

    what im trying to do is allow users to use tags to search for a image.
    tags fro an image is stored in a single field in a during uploading time (done by administrator) seperated by a delimeter.

    thaaaaaaaaaaaan ks in advance!!!
    if there's a better way in PHP to do this please let me know.
    Last edited by ak1dnar; Aug 5 '08, 02:31 PM. Reason: Fixed <CODE=PHP> with [CODE=PHP] [/CODE]
  • coolsti
    Contributor
    • Mar 2008
    • 310

    #2
    Is there any reason why you do not do the filtering of results in your mysql query (by modifying the query with an appropriate WHERE clause) as opposed to trying to doing this afterwards in PHP?

    Comment

    • nse111
      New Member
      • Jul 2008
      • 21

      #3
      Originally posted by coolsti
      Is there any reason why you do not do the filtering of results in your mysql query (by modifying the query with an appropriate WHERE clause) as opposed to trying to doing this afterwards in PHP?

      This is why..

      the 'tags' field in my table stores all the tags for a particular image.

      like,

      example: cherry:tree:gar den:hotel:mount ain

      so after accepting a tag/tags from a user n assigning it to a variable/variables
      how do I query my database?

      can you pls help?

      Comment

      • nse111
        New Member
        • Jul 2008
        • 21

        #4
        pls help!!! someone...

        Comment

        • henryrhenryr
          New Member
          • Jun 2007
          • 103

          #5
          Originally posted by nse111
          I want to know how I can match a whole word within a string using php.
          Code:
          $str='tree:sweet:house:gate:tent'
          if (strpos($str,'sweet')>=0) {
            //is there
          }
          This is a bit blunt and will fail on words like 'ate' which will match 'gate' but it's quicker than strstr(). You can use preg_match:

          Code:
          $str='tree:sweet:house:gate:tent'
          if (preg_match($regex,$str)>0) {
            //at least one match
          }
          ps. you need the right regular expression in there - I don't know the exact one of hand. Try googling regular expression forums - there are loads out there and there are regular expressions which will match whole words.

          what im trying to do is allow users to use tags to search for a image.
          While the above two options work for PHP, you should really use SQL if allowing users to search for the image is what you want.

          You can use regular expressions in SQL

          Code:
          ... WHERE tags REGEXP [[:<:]]sweet[[:>:]]
          this will match whole words (that's the bit in the square brackets - meaning start (<) and end (>) of a word)

          You can combine with a LIKE clause to speed this up as regular expressions tend to be a bit slower:

          Code:
          ... WHERE tags LIKE (%sweet%) AND tags REGEXP [[:<:]]sweet[[:>:]]
          Hope that's helpful...
          Last edited by henryrhenryr; Aug 4 '08, 09:19 AM. Reason: typo

          Comment

          • ak1dnar
            Recognized Expert Top Contributor
            • Jan 2007
            • 1584

            #6
            If you want to match a user input with your mysql table field values, just create a String array and use PHP's in_array function to match your user input with the array elements.

            [code=php]
            <?php
            $table_records = "tree:sweet:hou se:gate:tent"; // Your My SQL Records
            $string_array = explode(":",$ta ble_records);

            $user_input = "tree"; // Change this value and see how its working

            if (in_array($user _input, $string_array)) {
            print "Matching Tag Found";
            }else{
            print "Sorry No matching Tag";
            }
            ?>
            [/code]

            By the way is there any problem with your keyboard's letter "a"?

            Comment

            Working...