-1

I have a string:

HELLO WORLD by Adam Smith published 1920 born 1890;

How I can check that year after 'born' 1890 not greater then year after published 1920? Thanks

user45669
  • 45
  • 1
  • 7

3 Answers3

0

To compare numbers with numbers, check: http://php.net/manual/en/language.operators.comparison.php

Eda
  • 218
  • 1
  • 3
  • 17
  • I think he asked how to extract those number from the string instead of actually comparing them. – AlexL Oct 25 '14 at 00:04
0

Regular expressions would do the trick to extract the 2 numbers from a string and then you compare the 2 numbers. See Extract numbers from a string

Community
  • 1
  • 1
AlexL
  • 1,699
  • 12
  • 20
0

Use regular expression to extract the digits from the letters and compare:

$str="HELLO WORLD by Adam Smith published 1920 born 1890";
$patt="/\d{4}/";
$res=preg_match_all($patt,$str,$matches);
if($matches[0] > $matches[1]){
    echo 'Year of birth is before year of publishing';
}else{
    echo 'Looks like time travelling: published before even born ...';
}

$matches holds: Array ( [0] => Array ( [0] => 1920 [1] => 1890 ) )

The preg_match_all function extracts all 4 digit character chain and lays that into the array $matches. But you have to be sure, that the sentence has no other year (or 4 digit stings) and that the published date is the first and birth date second argument in the sentence.

boulder_02
  • 301
  • 1
  • 6