0

I have a script that allows a user to submit a search query, usually in the form of a Site Name or using a device's MAC Address. I want to keep the search as simple as possible, so only want the one search field, and don't want to clutter it up with select fields or radio buttons for the user to specify which they're searching for. I want to take care of that automatically on the back-end.

Here is how I expect a Site Name to be searched by, in one of these formats:

The letters and numbers are just examples, can be anything, just in this format.

TX-DAL0099A1
TX-DAL0099A
TX-DAL0099
DAL0099A1
DAL0099A
DAL0099

OR By MAC Address

00 11 22 33 44 55
00:11:22:33:44:55
00.11.22.33.44.55
00-11-22-33-44-55
001122334455

I'm looking for the best way to first check if it's a Site Name (the first examples), and then if not, make sure it's a MAC address. I don't want to run a preg_replace function on the search query to strip any misc characters, like the hyphens, periods, colons, etc. because it's important to have the prefixed TX- portion of the Site Name - if it is in fact a Site Name that's being searched for.

Note: I would need to know if the Site Name does have the prefix, as well as the ending A/A1 from the examples. Those are important.

I hope that made sense. =/

Oh, and I'm doing this in PHP - forgot to mention that.

UPDATE: Would this code snippet work?

if(preg_match("/^([0-9A-F]{2}[:-\s]?){5}([0-9A-F]{2})$/", $input)) {
    // SEARCH IS A MAC ADDRESS
}
else if((($query = $mysqli->query("SELECT * FROM `table` WHERE `site` LIKE '%$input%'")) !== false) && $query->num_rows() > 0) {
    // SEARCH IS A SITE AND FOUND ROW IN DATABASE
}
else {
    // NEITHER SITE NOR MAC, OR COULDN'T BE FOUND
}
Skryn
  • 155
  • 3
  • 12
  • 1
    Be aware of the fact that the above regex will also accept the following mac address 0F-A0-FF:86 DE 84, the regex given by pilcow on http://stackoverflow.com/questions/4260467/what-is-a-regular-expression-for-a-mac-address should work better – bkwint Mar 29 '13 at 07:22
  • Thanks for that tip, bkwint. – Skryn Mar 29 '13 at 08:12

3 Answers3

4

Here are some snippets that should help.

See if we have a MAC address (regex pattern is taken (and tweaked) from What is a regular expression for a MAC Address?):

if(preg_match("/^([0-9A-F]{2}[:-\s]?){5}([0-9A-F]{2})$/", $input)) {
   // We have a MAC address
}

See if we have a dash in the input (indicating a prefix) and what it is:

$siteNamePieces = explode("-",$input);
if(count($siteNamePieces) >= 2) {
   // Site name has a prefix, and it is $siteNamePieces[0]
}

See if we end with A or A1:

if(substr($input, -1) == "A") {
   // Ends in A
} else if(substr($input, -2) == "A1") {
   // Ends in A1
}

I expect these will need to be adapted base on the different types of endings you're looking for, whether dashes can appear without necessarily meaning it's a prefix, etc. I'm guessing you need to pull off the prefix and the ending and get the core site name value for querying purposes.

Once you know what kind of data you're dealing with (mac address, site name) you can query your database appropriately.

Hopefully these will get you moving in the right direction!

Community
  • 1
  • 1
jszobody
  • 28,495
  • 6
  • 61
  • 72
  • Thanks! That first one especially helps me. I updated my original question using your's and Codeius' examples to put together something I think might work. Could you check it to see if there's any potential issues with it? – Skryn Mar 29 '13 at 05:15
2

The below SQL query will find rows where site is similar to the input, or mac is exactly the input.

SELECT * FROM `table` WHERE `site` LIKE '%$input%' OR `mac` = '$input';
Jakob Pogulis
  • 1,150
  • 2
  • 9
  • 19
  • This is a possibility. However, currently, my data is stored in text files. I am slowly migrating to a database back-end, but there's well over 100,000 entries to deal with, and I haven't quite gotten around to importing it all... and all that. But, I have a question, if someone searched for "TX-DAL0091A1" will that match an entry that's simply "DAL0091" or "DAL0091A" for example? I'm assuming that's what the LIKE parameter does, but I just want to be sure. And then I guess it falls back to the MAC, if nothing found from the LIKE search, right? Sorry for the questions. – Skryn Mar 29 '13 at 04:59
  • Well yes, it will match DAL0091 and DAL0091A, the percentage characters defines the fuzzy parts. The query will match everything that is LIKE a site (as above), and everything that is EQUAL to the mac. It is possible that you'll get several responses in return with the above query. – Jakob Pogulis Mar 29 '13 at 05:02
  • Thanks! I updated my original question using your's and jszobody's examples to put together something I think might work. Could you check it to see if there's any potential issues with it? – Skryn Mar 29 '13 at 05:16
  • If you want to use the LIKE syntax but on PHP strings, I [once created in an answer a function that converts the LIKE syntax into a pcre regex](http://stackoverflow.com/a/7818797/367456) for the quesiton [Wildcards in array search](http://stackoverflow.com/q/7818760/367456) - It probably is of use to you. – hakre May 01 '13 at 09:36
0

you can try with variable1 ==== varaible2

it will compare the string with data type as it has three equals character else it will only compare the values