Jump to content

Archived

This topic is now archived and is closed to further replies.

CanMan2004

Wildcard Query

Recommended Posts

Hi all

I am trying to perform a query on a field on my sql database called pcode, basically the field holds the data for a postcode, so for example, the rows might look like

W1H 7YH
ST64 2HG
N12 2ES

I want to perform a query on the postcode field allowing rows to be found which contain any kind of match, so it ignores spaces placed in the search box or in the field.

For example, if someone does a search on "W1H7HG" then it would return the rows that start or contain the whole postcode "W1H 7HG",

again, if they did a search for "S E2 2 7Y H" then it would return the rows that start or contain the whole postcode "SE22 7YH",

and again if they did a search for "E H 4" then it would return the rows that start or contain the whole postcode "EH4" in the field

I hope that makes some sense, im basically trying to get around people putting extra spaces into the search or missing a space, I normally use the code

[code]pcode LIKE '%".$_GET['pcode']."%'[/code]

But it seems to NOT ignore additional spaces.

Is there a wildcard search to do what I want?

Thanks in advance

Ed

Share this post


Link to post
Share on other sites
Because the British establishment seems incapable of coming up with standard structures for code (be it phone area codes or postcodes) I store postcode fields twice in my postcode table, formatted and with no spaces for searching

[pre]| W1H 7YH  | W1H7YH  |
| ST64 2HG  | ST642HG  |
| N12 2ES  | N122ES  |[/pre]

Share this post


Link to post
Share on other sites
[quote author=Barand link=topic=107539.msg431612#msg431612 date=1157897351]
Because the British establishment seems incapable of coming up with standard structures for code (be it phone area codes or postcodes) I store postcode fields twice in my postcode table, formatted and with no spaces for searching

[pre]| W1H 7YH  | W1H7YH  |
| ST64 2HG  | ST642HG  |
| N12 2ES  | N122ES  |[/pre]
[/quote]

Wouldn't it be easier to do [code]str_replace(' ','',$string)[/code] on them?

Share this post


Link to post
Share on other sites
It would be, but the postcodes in the sql database have spaces in them, unless you can put

[code]str_replace(' ','',$string)[/code]

into the query, so it views the data in the sql database without spaces and does a search, is that possible? I imagine it's not as simple as

[code]$temp = str_replace(" ", "", postcode);
$sql = "SELECT * FROM users WHERE $temp LIKE '%".$_GET['postcode']."%'";[/code]

Then I could remove the spaces of the forms posted field and get a match

Can this be done?

Thanks in advance

Ed

Share this post


Link to post
Share on other sites
I'm useless at regex, but if there's a pattern to strip out spaces from a string you could have something like this (not sure of exact syntax for regex() )

.... WHERE REGEX(pcode, pattern) LIKE '%$search%'

Share this post


Link to post
Share on other sites
Just checked the MySQL manual - there is a str_replace equivalent

... WHERE REPLACE(pcode, ' ', '') LIKE '%$search%'

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.