I have some domains stored in a database and I have created a search to return matching domains based on what the user types.
For example if the user types "h" all domains containing a "h" should be returned.
The problem comes when a user searches for an "o" for example as "o" is contained in the extension (.com and .co.uk).
I am guessing that I am going to need some kind of regex to achieve what I am trying to do. Any help would be greatly appreciated!
Here is the current MySQL query I am using:
domain like '%".mysql_real_escape_string($_GET['keywords'])."%'
simplest solution: don't do the analysis when retrieving the data, do it when storing the data -- split the domain (e.g. example.com) into two columns, the domain (e.g. example) and the tld (e.g. com)
Hmmm yes I thought of doing it this way, but im integrating with an existing system and this will simply be too much work.
Is there a way using regex?
you could add two cols to the table and write a short query script to take the domain name, and split it and insert the results in the two new cols. Personally I would loose the ' . ' altogether and program for that on the view side. This would also allow you to do future searches if the visitor only wanted to look at .com names. You would then have the ability to get all the .com names from the table.
url | domain | extension
Just a suggestion.
A couple of questions
Is the part of the domain name you wish to match on effectively up to the first dot. That is hotgolf.com not www.hotgolf.com or www3.hot.golf.com.
When you have two or more characters for example "ho", do you want them as consecutive characters in the part of the domain name being searched.
The domains are always stored like hotgolf.com (so it will only be the first dot).
Yes when I have "ho" being searched it should return hotgolf.com.
Hope this makes sense.
okay, now you're in business!!
WHERE <font color='"Blue"'>SUBSTRING_INDEX(domain,'.',1)</font> LIKE ...
will that work for .co.uk domains? Or does that make it more complicated?
what happened when you tested it?
I think you just need to do this "select * from table where column LIKE '%$var' "
It returned ".com" domains but no ".co.uk"
I mean "SElect * from table where column LIKE '$var%'";
That won't work. Thanks anyway
then ur doing it wrong
CREATE TABLE zaggs
( domain VARCHAR(99)
INSERT INTO zaggs VALUES
( 'hotgolf.com' )
,( 'example.com' )
,( 'hotgolf.co.uk' )
,( 'puke.com' )
SELECT * FROM zaggs
WHERE SUBSTRING_INDEX(domain,'.',1) LIKE '%ho%'
SELECT * FROM zaggs
WHERE SUBSTRING_INDEX(domain,'.',1) LIKE '%uk%'