1

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.

3
  • 1
    preg_match('/\b([0-9]+)\b/', $lot_num, $matches ) Commented Jun 29, 2017 at 5:23
  • @ArtisticPhoenix Hi. thanks for the quick response. unfortunately, the code you gave didn't work. but thanks for trying to help me. Commented Jun 29, 2017 at 5:28
  • it does to work, you just implemented it wrong, of course I didn't test it either but it will work. See regex101.com/r/17psAQ/1 AND my answer for a fuller solution. Commented Jun 29, 2017 at 5:30

6 Answers 6

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This is what I've been looking for. Thanks so much.
@Nibiru your welcome. Please accept the answer if you find it correct :-)
2

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;

1 Comment

It works just like @Andreas' answer. I just need to deduct it with my End unit variable. thanks :)
2
$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']);

Comments

0

you can use is_numeric() and

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

inside a loop

Comments

0

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

Comments

0
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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.