Mokus
Mokus

Reputation: 10400

Getting the useful words from a word list

I have the following strings:

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

For me the valid texts are the following

Over/Under 1.5
Over/Under 2.5
Over/Under 3.5
Over/Under 4.5
Over/Under X.X

where the X.X is a number.

How can I make a decision weather is a valid string or not?

Upvotes: 0

Views: 78

Answers (4)

Pelshoff
Pelshoff

Reputation: 1464

Use a regular expression. I'm not very good at them myself, but this worked for me:

$t

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);

http://php.net/manual/en/function.preg-match.php for more info

Upvotes: 1

sberry
sberry

Reputation: 132018

Does This es not Over/Under 1.5 match? If so:

$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";
  }
}

If not, change the preg_match to

preg_match('#^Over/Under \d\.\d$#', $word);

Like @Tokk writes, if the string should match on Over OR Under, then you need to change to an OR - |

preg_match('#^(Over|Under) \d\.\d$#', $word);

Upvotes: 1

Tokk
Tokk

Reputation: 4502

Check if it matches the Regex

Over/Under [0-9]*.[0-9]*

(if it is Over OR Under, choose (Over|Under)

Upvotes: 1

Dogbert
Dogbert

Reputation: 222128

if(preg_match('/Over\/Under \d\.\d/', $str)) {

}

Upvotes: 1

Related Questions