<?phpclass Solution { public function myAtoi($str) { if (!$str) return 0; $str = trim($str); $first = $str[0]; $sign = 1; // 正整数 $res = 0; if ($first == '+') { $start = 1; } else if ($first == '-') { $start = 1; $sign = -1; } else { $start = 0; } for ($i = $start; $i < strlen($str); $i++) { if (!is_numeric($str[$i])) { return $res * $sign; } $res = $res * 10 + $str[$i]; if ($res >= pow(2, 31)) { if ($sign > 0) { return (pow(2, 31) - 1) * $sign; } return pow(2, 31) * $sign; } } return $res * $sign; }}