December 03th, 2008

Parsing a number from a string in PHP

Written by Dave BarkerTopics: Php, Code, Howto

Another quick PHP snippet, a function that returns the first integer found in the string given:

// Take a string and return the first number found
function parse_int($string) {
  ereg("([0-9]+)", $string, $results);
  if (is_array($results))
    return (int)$results[0];
  else
    return NULL;
}