$input = trim(fgets(STDIN)); $str = explode(" ", $input); $from = (int) $str[0]; $to = (int) $str[1]; for ($i = $from; $i <= $to; $i++) { if (($i % 3 === 0) || (preg_match('/^[0-9]*3[0-9]*$/', $i))) { echo $i; echo "\n"; } }