PHP从字符串中获取特定的整数

I want to extract the ID which is the first integer in the string below:

ID:40021223
CSQ(0-31): 31
Online

I only want the to get 40021223

$str = 'ID:40021223';   //pass any string
echo $output = getID($str); 

function getID($str) {
   $res = explode(':',$str);  //split string with :
   if(in_array('ID',$res)) {  //if array contain Id then it will give the exact output else null
      return $res[1];
   } else {
      return null;
   }
}

Try the below code

$str = "ID:40021223
CSQ(0-31): 31
Online";

$filteredNumbers = array_filter(preg_split("/\D+/", $str));
$firstOccurence = reset($filteredNumbers);
echo $firstOccurence;

This code will output the first set of numbers from any string

Here the output will be: 40021223

Try something like this

$matches = [];
$x = "ID:40021223
CSQ(0-31): 31
Online";

preg_match("/(id:\K\d+)/i", $x, $matches);
$id = $matches[0]

This is saying " (id:\d+) find me the expression with id: followed by any number of digits, but we add the \K to reset the match start so u will be able to get the following part only.