$str = '$agentid=1010303&listingid'; if(preg_match_all('/agentid\=(\d+)&listingid/',$str,$matches)){ $num = $matches[1][0]; }