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
}