Nibiru Nibiru
Nibiru Nibiru

Reputation: 67

Count how many numbers and letters in the variable using PHP

I'd like to count how many numbers and letters are in the variable using PHP. Below if my code:

$lot_num = strtoupper('e1,1,2,3,4,5,7,8,9,10,11,12,13,14,15,16,17,18,e18');

echo 'END UNIT: '.substr_count($lot_num, 'E').'<br />';

the code will count how many letter E are there in my lot_num variable but i would also like to count how many numbers are in the variable. Supposed, E1 and E18 should not be included when counting numbers.

I hope you can help me guys.

Upvotes: 1

Views: 468

Answers (6)

Anandh L
Anandh L

Reputation: 1

1,first u have to seperate the string and stored into  array
2,then u can easily count the value of integers
<?php
$lot_num =explode(',',strtoupper('e1,1,2,3,4,5,7,8,9,10,11,12,13,14,15,16,17,18,e18'));//seperate string by ","
$arr_count=count($lot_num);
for($i=0;$i<$arr_count;$i++)
{
$get_num[]=$lot_num[$i];//saving seperated string value into array
}
$count=0;
for($j=0;$j<count($get_num);$j++)
{
if(is_numeric($get_num[$j]))//chect whether the value is integer or not
{
    $count++;
}
}
echo $count;

Upvotes: 0

harsh kumar
harsh kumar

Reputation: 165

$lot_num = strtoupper('e1,1,2,3,4,5,7,8,9,10,11,12,13,14,15,16,17,18,e18');
$array  =   explode(',', $lot_num);
$data=array();
foreach($array as $k=>$val){    
    if(is_numeric($val )){      
        $data['number'][] = $val;
    }else{      
        $data['string'][] = $val;
    }


}
echo count($data['number']);
echo count($data['string']);

Upvotes: 2

Andreas
Andreas

Reputation: 23958

Try this: explode on , to get array that can be counted.

https://3v4l.org/r6OKl

$lot_num = strtoupper('e1,1,2,3,4,5,7,8,9,10,11,12,13,14,15,16,17,18,e18');
$ecount = substr_count($lot_num, 'E');
$totcount = count(explode(",", $lot_num));

echo 'END UNIT: '.$ecount;
Echo "\ntotal count: ".  $totcount;
Echo "\nother count: ". Intval($totcount-$ecount);

No loops and no regex makes it a simple and quick solution.

Upvotes: 2

ArtisticPhoenix
ArtisticPhoenix

Reputation: 21661

Id use preg_match

    preg_match('/\b([0-9]+)\b/', $lot_num, $matches );

And matches would be like this.

      $mathes[1][1,2,3,4,5,7,8,9,10,11,12,13,14,15,16,17,18]

So you would

     $lot_num = strtoupper('e1,1,2,3,4,5,7,8,9,10,11,12,13,14,15,16,17,18,e18');


     $total = 0;
     if( preg_match('/\b([0-9]+)\b/', $lot_num, $matches )){
        $total = count( $mathes[1] );
     }

You can see how the Regx works here https://regex101.com/r/17psAQ/1

Upvotes: 0

Nanotech
Nanotech

Reputation: 21

you can use is_numeric() and

if (!preg_match("/^[a-zA-Z]$/", $param)) {
   // throw an Exception...
}

inside a loop

Upvotes: 0

toastrackengima
toastrackengima

Reputation: 8732

You could always turn it into an array and use a loop:

$lot_num = explode(',',strtoupper('e1,1,2,3,4,5,7,8,9,10,11,12,13,14,15,16,17,18,e18');

$count = 0;
for ($i=0;$i<count($lot_num);$i++) {
    if (is_int($lot_num[$i])) { //detects all numbers
        $count++;
    }
}

echo $count;

Upvotes: 2

Related Questions