简体   繁体   中英

Check if string contains only combinations of array elements (numbers and words)

I've searched for hours and can't find any example of what I'm trying to do. I can't even figure out what php function needs to be used, but I'm thinking probably a regex. I attempted to use in_array and it didn't work. I don't know enough about regexes to even set up a test. Here's the problem... Say my array is:

$sizeopts = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.5',
 ' Wide', ' Narrow', 'XS', 'Small', 'Medium', 'Large', 'XL');

The textbox name is "size", and I want to only allow combinations of the above to be submitted. Example valid input: "10.5 Wide" or "3XL". Invalid input: "1X" or "2Wide" (missing the space in front of Wide). I have several different size arrays I need to validate. I'm not just using drop-downs because there are two many different possible combinations. Thanks for any help you can offer!

尝试这个:

$valid = preg_match("/[0-9]+(?:\.5)?(?: Wide| Narrow|XS|Small|Medium|Large|XL)/",$input);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM