将字符串分成几部分
我在表格中有一个问题列表,其中一些问题只有在满足某些条件时才会显示。 记录的标准可能像 4002=Y 一样简单,其中 4002 是问题编号,Y 是答案。 如果4002=Y则显示问题。
对于只有一个标准的记录我没有问题。
但有些记录具有如下标准:
402=Y OR 7003=Y OR 905=Y OR 7007=Y OR 7008=Y OR 7010=Y OR 7011=Y OR 7013=Y
在这种情况下,我需要评估每个选项以查看是否要显示问题。
其他问题也会有类似的字符串; 有的较短,有的较长。
我如何最好地分割字符串,以便我可以一次评估每个部分,并且仍然能够正确地比较它们?
我可以在某种程度上重新格式化数据,但如果可能的话我宁愿不这样做。
这是一个 regex() 任务吗(我对此还不太熟悉)? 我尝试过 list()
、split()
和 explode()
但收效甚微。
任何指示将不胜感激。
I have a list of questions in a table, some of which are only to be displayed if certain criteria are met. A record might have criteria as simple as 4002=Y where 4002 is the question number and Y is the answer. If 4002=Y then the question is to be displayed.
For records with only one criteria I have no problem.
But then there are records that have criteria like the following:
402=Y OR 7003=Y OR 905=Y OR 7007=Y OR 7008=Y OR 7010=Y OR 7011=Y OR 7013=Y
In this case I would need to evaluate each option to see if the question is to be displayed or not.
Other questions will have similar strings; some shorter, some longer.
How would I best split the string up so I can evaluate each section at a time and still be able to compare them correctly?
I can reformat the data to some degree, but I would prefer not to if at all possible.
Is this a regex()
task (I'm not very familiar with that yet)? I've tried list()
, split()
and explode()
with little success.
Any pointers would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您的输入字符串确实只是一堆用“ OR ”分隔的简单条件,那么一个简单的explode()确实可以解决问题:
但是如果它更复杂(如果您允许AND以及OR,比如说)那么您将需要一个相应更智能的解析器。
If your input string really is just a bunch of simple criteria separated with " OR ", then a simple explode() will indeed do the trick:
However if it is more complicated (if you allow AND as well as OR, say) then you will need a correspondingly smarter parser.
你可以试试
you can try
我首先对字符串运行正则表达式,将“分隔符”更改为“&” -- 特别是在“OR”周围可能有额外空格的情况下(在正则表达式中更容易表达,而不是尝试单独调整/修剪每个术语。然后使用
parse_str() 并且您有一个数组,其中每个索引都是问题编号,值为“Y”或“N”。
I'd first run a regex against the string to change the "separator" into "&" -- especially in a situation where there may be extra whitespace around the "OR"'s (easier to express that in regex, than it is to try to adjust/trim each term individually. Then use
parse_str()
and you have an array in which each index is the question number and the value is "Y" or "N".这假设您指定的格式,并且我假设可能的值是 Y(是)或 N(否)
This assumes the format you've specified and i'm assuming the possible values will either be Y (yes) or N (no)