471,071 Members | 1,358 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,071 software developers and data experts.

match an exact word within a string

21
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:house:gate:tent
candy:lollipop:choco:street:child

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

when I use the following code:

Expand|Select|Wrap|Line Numbers
  1. if (isset($_POST['findtag']))
  2.  
  3.             {
  4.  
  5.             $tags = $_POST['txttags'];
  6.             $result = mysql_query("select tags from tags") or die("MYSQL Returned: " . mysql_error());
  7.             while($row = mysql_fetch_assoc($result))
  8.                 {
  9.                 $temptag=$row['tags'];
  10.                 $strr2 = strstr($temptag, $tags);
  11.                 echo "$strr2"."<br />";
  12.  
  13.                 }
  14.  
  15.             }
  16.  
I get the output as:

tree:sweet:house:gate:tent
tree

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

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.

thaaaaaaaaaaaanks in advance!!!
if there's a better way in PHP to do this please let me know.
Aug 4 '08 #1
5 9399
coolsti
310 100+
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?
Aug 4 '08 #2
nse111
21
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:garden:hotel:mountain

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?
Aug 4 '08 #3
nse111
21
pls help!!! someone...
Aug 4 '08 #4
henryrhenryr
103 100+
I want to know how I can match a whole word within a string using php.
Expand|Select|Wrap|Line Numbers
  1. $str='tree:sweet:house:gate:tent'
  2. if (strpos($str,'sweet')>=0) {
  3.   //is there
  4. }
  5.  
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:

Expand|Select|Wrap|Line Numbers
  1. $str='tree:sweet:house:gate:tent'
  2. if (preg_match($regex,$str)>0) {
  3.   //at least one match
  4. }
  5.  
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

Expand|Select|Wrap|Line Numbers
  1. ... WHERE tags REGEXP [[:<:]]sweet[[:>:]]
  2.  
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:

Expand|Select|Wrap|Line Numbers
  1. ... WHERE tags LIKE (%sweet%) AND tags REGEXP [[:<:]]sweet[[:>:]]
  2.  
Hope that's helpful...
Aug 4 '08 #5
ak1dnar
1,584 Expert 1GB
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.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $table_records = "tree:sweet:house:gate:tent"; // Your My SQL Records
  3. $string_array = explode(":",$table_records);
  4.  
  5. $user_input = "tree"; // Change this value and see how its working
  6.  
  7. if (in_array($user_input, $string_array)) {
  8.     print "Matching Tag Found";
  9. }else{
  10.     print "Sorry No matching Tag";
  11. }
  12. ?>
  13.  
By the way is there any problem with your keyboard's letter "a"?
Aug 5 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

20 posts views Thread by Ravi | last post: by
6 posts views Thread by Mark Findlay | last post: by
19 posts views Thread by Tom Deco | last post: by
4 posts views Thread by jmdaviault | last post: by
12 posts views Thread by Johnny Williams | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.