tags:

views:

155

answers:

5

Is it possible to create a variable with a reg expression of some sort or some other type of code that might complete the below task?

For instance, the data in var test = parta but could also be equal to partm. Note i don't want an array...

I'm using this in a sql search, but I have 1000s of different models and series and only want to return certain series of models without returning series similar to the input...

Is there something that will allow that fifth character to be a list of desired characters...?

Another example i have model

two models in same series

515-n

515m

model in different series 1515

I don't want to return the 1515 if im searching the 515 series...

Note that this isn't the only combination... so the search cannot be specified to just this one... needs to be generic result.

A: 
preg_match('/\b515\b-?(?:n|m)/', $model)

Generically:

preg_match('/\b' . $model_number . '\b-?(?:' . join('|', $array_of_series_letters) . ')/', $model)
chaos
Having a preg_match for 1000 unique items is not what I was looking for...
payling
Ah. I thought that since you tagged your question PHP (only), you wanted a PHP solution.
chaos
+1  A: 

Normally you would use the SQL LIKE syntax to find information in the database. I'm not sure you would use PHP. If you would search for all models starting with 515 you would use something like:

SELECT * FROM `models` WHERE model_name LIKE '515%';

Notice that the percentage is only on the right side so this wouldn't find 1515.

Michiel
i use that now, except % on both sides. I could setup a special just for the categories that are hard coded and another sql for the user entered searches...
payling
A: 

You can use SQL wildcards to match some patterns, but it is nowhere near as flexible as regular expressions. Here are the wildcard rules available:

%    A substitute for zero or more characters

_    A substitute for exactly one character

[charlist]   Any single character in charlist

[^charlist]
or
[!charlist]  Any single character not in charlist

In your example, you could use any of the following within your SQL WHERE clause:

WHERE model LIKE '515%' <--matches 515 followed by zero or more characters

WHERE model LIKE '515-[nm]' <--matches 515-n and 515-m only

WHERE model LIKE '515%[nm]' <--matches 515n and 515-m, also would match 515Qm
Dustin Fineout
yeah... that would work if I had a seperate sql for every single product in the db.... that would be extreme considering there are over 3000 items in the db.. I was using the sql to do user entered search and hard coded searches but I think michiel idea will work if i create a seperate sql for the hardcoded searches.
payling
If you want user-entered search the best thing to do is offer the user different search cases (contains, begins with, ends with, equals) for their search query, and then process as follows ($query being your PHP variable): contains: "WHERE model LIKE '%$query%' begins: "WHERE model LIKE '$query%' ends: "WHERE model LIKE '%$query' exact: "WHERE model LIKE '$query'
Dustin Fineout
Another option is to allow users themselves to use wildcards in their queries. I sometimes allow users to use * as a wildcard, and then replace it with % for the SQL.
Dustin Fineout
A: 

I always recommend avoiding regular expressions if you can. In this case it is easily possible, you have a model and valid extensions to that model. Sou you only need to check if the string starts with the desired model-string and ends on either of the valid extensions:

$string = '515-n';
$possibleExtensions = array('-n', 'm');
$model = '515';
$isDesiredModel = strpos($string, $model) === 0 &&
    in_array(substr($string, strlen($model)), $possibleExtensions);
soulmerge
ohh this may work the way i want it to, let me work on it and i'll get back to ya
payling
Note: I had the args to strpos() in the wrong order, just fixed it.
soulmerge
A: 

I think this is forgiving enough to (almost) be a real-world solution:

function isModel($prefix,$input)
{
    return preg_match("/^" . $prefix . "\s*-?\s*?[nm]/", $input);
}

Some test cases:

isModel('5155', '5155n'); // "1"
isModel('515','515-n'); // "1"
isModel('789','789 - m'); // "1"
isModel('111','111    -n'); // "1"
karim79