A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearPHP+MySQL: searching using wildcards - page 1
User InfoPosts
PHP+MySQL: searching using wildcards#1
So, I have the following rows in the DB:

1 | /users/

2 | /users/admin/

3 | /users/admin/*

4 | /users/admin/mike/

5 | /users/admin/steve/docs/

The input URL is /users/admin/steve/, and the goal is to find the URL match from the DB.

I want to return #3 as the correct row, since the wildcard "*" specifies that anything can go in place of the asterisk. What would be the most efficient method for doing this?

Here's my initial thoughts, but I'm sure they could be improved upon:

Make a query to see if there's an exact URL match
If no matches, then retrieve all rows with "*" as the last character, in reverse order (so the more specific URLs take precedence)
For each row, if it (minus the "*") matches the input URL, then return it
If nothing is found, then we're SOL

posted date: 2008-12-12 20:22:00

Re: PHP+MySQL: searching using wildcards#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-12 20:22:01

Re: PHP+MySQL: searching using wildcards#3
If I understand this correctly, something like this should work: SELECT COALESCE( (SELECT whatever FROM your query to see if there is an exact url), (SELECT next approximation), 'SOL')

posted date: 2008-12-12 20:27:00

Re: PHP+MySQL: searching using wildcards#4
Here's how I'd do it:SELECT * FROM mytable AS mWHERE <input-url> = m.urlpattern OR <input-url> REGEXP REPLACE(m.urlpattern, '*', '.*');The REPLACE() is to change the globbing-style wildcard into an equivalent regular-expression wildcard.

posted date: 2008-12-12 20:29:00

Re: PHP+MySQL: searching using wildcards#5
That's perfect, thanks!

posted date: 2008-12-12 20:35:00

select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.