tags:

views:

23

answers:

3

I need to check if any strings follow the same format as these.

Val-Pak
*Client Referral*
Gift Certificate

It needs to be done in regular expression. I can't find how to include the * and -.

Thanks

A: 

Check out this site for information on how to write Regex (regular expressions), it goes through how to escape characters like * and -

Fox
A: 

The quick answer is you escape them with backslashes:

/Val\-Pak/

/\*Client\sReferral\*/

/Gift\sCertificate/

But it might help to read some regex tutorials, because understanding the big picture will help you in the long run.

Jeff B
A: 
preg_grep('/Val-Pak|\*Client Referral\*|Gift Certificate/', $myArray);

preg_grep filters an array of strings and returns only those that contain one of the strings you mentioned. If you need to check only a single string, then preg_match will do.

If you need to escape arbitrary strings for inclusion in a regex pattern, then preg_quote comes in handy.

In any case, the asterisk, *, is a special character in regular expressions and needs to be escaped with a \.

Joey