Fatemeh Namkhah
Fatemeh Namkhah

Reputation: 711

Comparison with regex and get count of diffrence in php

I have a multidimensional array and want to comparison with regex and get count of difference value.

Array:

Array ( 
    [0] => Array ( 
        [SaleId] => 11^ 
    ) 
    [1] => Array ( 
        [SaleId] => 222@ 
    ) 
    [2] => Array ( 
        [SaleId] => 333% 
    ) 
    [3] => Array ( 
        [SaleId] => %%$ 
    ) 
) 

PHP Code:

$number = count(!preg_match("/[0-9][a-z][A-z][@.+-_]/",$SaleId));
echo $number;

Explain for regex:

  1. Lowercase letter
  2. Uppercase letter
  3. number
  4. special character @.+-_

but the $number return 1 for any case! Help me plz

Return output = 1

i want to return 3

Upvotes: 1

Views: 53

Answers (1)

Vigneswaran S
Vigneswaran S

Reputation: 2094

preg_match() will true or false.so if match found means count the value as given below.you are passing entire array.we should pass array with index.so use for loop try this

<?php
$arr = array(
0 => array(
    'SaleId' => '11^' ),
1 => array(
    'SaleId' => '222@'),
2 => array(
    'SaleId' => '333%'),
3 => array(
    'SaleId' => '%%$' ),
 );

 //print_r($arr);exit();
 $not_match_count=0;
 $match_count=0;
 for($i=0;$i<sizeof($arr);$i++){
$name=$arr[$i]['SaleId'];
if (!preg_match("/^[0-9a-zA-Z.\@\+\-\_]*$/",$name)) {
 $not_match_count=$not_match_count+1;
}
else{
$match_count=$match_count+1;
}
}
echo $not_match_count."<br>".$match_count;// your expected output
?>

Upvotes: 2

Related Questions