我有以下字符串:
Over/Under 1.5
Over/Under 2.5
Over/Under 3.5
Over/Under 4.5
This is not Over/Under 1.5
Other text
对我来说,有效的文本如下
Over/Under 1.5
Over/Under 2.5
Over/Under 3.5
Over/Under 4.5
Over/Under X.X
X.X
是一个数字。
如何判断天气是否为有效字符串?
答案 0 :(得分:1)
if(preg_match('/Over\/Under \d\.\d/', $str)) {
}
答案 1 :(得分:1)
检查它是否与正则表达式匹配
Over/Under [0-9]*.[0-9]*
(如果是Over或Under,请选择(Over|Under)
答案 2 :(得分:1)
This es not Over/Under 1.5
是否匹配?如果是这样的话:
$words = array('Over/Under 1.5',
'Over/Under 2.5',
'Over/Under 3.5',
'Over/Under 4.5',
'This es not Over/Under 1.5',
'Other text');
foreach ($words as $word) {
if (preg_match('#.*Over/Under \d\.\d.*#', $word)) {
echo "Match $word\n";
}
}
如果没有,请将preg_match更改为
preg_match('#^Over/Under \d\.\d$#', $word);
像@Tokk写道,如果字符串匹配Over OR Under,那么你需要改为OR - |
preg_match('#^(Over|Under) \d\.\d$#', $word);
答案 3 :(得分:1)
使用正则表达式。我自己并不擅长,但这对我有用:
$吨
mp = array
(
"Over/Under 1.5",
"Over/Under 2.5",
"Over/Under 3.5",
"Over/Under 4.5",
"Over/Under 5.5",
"fdgdfgdf",
"Other"
);
$tmp2 = array_filter($tmp, function($element) { return preg_match('(Over\/Under [0-9]\.[0-9])', $element); });
print_r($tmp2);