简体   繁体   English

检查字符串是否仅包含数组元素(数字和单词)的组合

[英]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. 我什至无法弄清楚需要使用什么php函数,但是我在想一个正则表达式。 I attempted to use in_array and it didn't work. 我试图使用in_array,但没有用。 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". 有效输入示例:“ 10.5宽”或“ 3XL”。 Invalid input: "1X" or "2Wide" (missing the space in front of Wide). 无效的输入:“ 1X”或“ 2Wide”(在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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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