473,511 Members | 14,933 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

match an exact word within a string

21 New Member
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 9702
coolsti
310 Contributor
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 New Member
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 New Member
pls help!!! someone...
Aug 4 '08 #4
henryrhenryr
103 New Member
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 Recognized Expert Top Contributor
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

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

Similar topics

20
5742
by: Ravi | last post by:
Hi, I have about 200GB of data that I need to go through and extract the common first part of a line. Something like this. >>>a = "abcdefghijklmnopqrstuvwxyz" >>>b = "abcdefghijklmnopBHLHT"...
6
56798
by: Mark Findlay | last post by:
I am trying to figure out how to set up my reg exp search so that the search will only match on the exact word. Here is the current problem code: Word1 = "RealPlayer.exe" Word2 = "Player.exe"...
19
2131
by: Tom Deco | last post by:
Hi, I'm trying to use a regular expression to match a string containing a # (basically i'm looking for #include ...) I don't seem to manage to write a regular expression that matches this. ...
1
3213
by: mike c | last post by:
I have a search app that searches local HTML files for a specified term. I then display the pages that contain the term. I would like to highlight the search term within the HTML when it is...
2
1881
by: ShadowOfTheBeast | last post by:
Hello All, i have been cracking my skull on how to seach a particular reg exp match within a string? it does not seem to happen except the whole arbitrary string is the exact match of the regular...
4
11212
by: jmdaviault | last post by:
I want to do the equivalent of SELECT id from TABLE WHERE text='text' only fast solution I found is: SELECT id,text from TABLE WHERE MATCH(text) AGAINST('value' IN BOOLEAN MODE) HAVING...
12
4544
by: Johnny Williams | last post by:
I'm struggling to create a regular expression for use with VB .Net which matches a person's name in a string of words. For example in "physicist Albert Einstein was born in Germany and" I want...
12
2650
by: ross.oneill | last post by:
Hi, Is there any function in php that will match a word exactly and if it finds it, it returns true. For example if I search for "CA" strVar = "Bob is from Los Angeles CA" - return true ...
2
7207
by: Slippy27 | last post by:
I'm trying to modify a find/replace script which iterates through a file A and makes replacements defined in a csv file B. My original goal was to change any line in file A containing a search string...
0
7137
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7349
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7417
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7074
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5659
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5063
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4734
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.