Dize Soru.

2 Cevap

Ben rastgele gibi dizeleri oluşturduk

H*P2[-%-3:5RW0j*;k52vedsSQ5{)ROkb]P/*DZTr*-UX4sp

Ne yapmak istediğiniz tüm Caps saymak, küçük harf, onlar oluşturulur gibi her dize sayısal ve özel karakterler olsun.

I am looking for an output similar to Caps = 5 Lower = 3 numneric = 6 Special = 4 Fictitious values of course. I have gone through the php string pages using count_char, substr_count etc but cant find what I am looking for.

Teşekkür ederim

2 Cevap

preg_match_all () Bir maçın olay sayısını döndürür. Sadece regex doldurmak gerekir istediğiniz bilgi her bit için ilişkilidir. Örneğin:

   $s = "Hello World"; 
   preg_match_all('/[A-Z]/', $s, $match);
   $total_ucase = count($match[0]);
   echo "Total uppercase chars: " . $total_ucase; // Total uppercase chars: 2

Sen kullanabilirsiniz ctype-functions

$s = 'H*P2[-%-3:5RW0j*;k52vedsSQ5{)ROkb]P/*DZTr*-UX4sp';
var_dump(foo($s));

function foo($s) {
  $result = array( 'digit'=>0, 'lower'=>0, 'upper'=>0, 'punct'=>0, 'others'=>0);
  for($i=0; $i<strlen($s); $i++) {
    // since this creates a new string consisting only of the character at position $i
    // it's probably not the fastest solution there is.
    $c = $s[$i];
    if ( ctype_digit($c) ) {
      $result['digit'] += 1;
    }
    else if ( ctype_lower($c) ) {
      $result['lower'] += 1;
    }
    else if ( ctype_upper($c) ) {
      $result['upper'] += 1;
    }
    else if ( ctype_punct($c) ) {
      $result['punct'] += 1;
    }
    else {
      $result['others'] += 1;
    }
  }
  return $result;
}

baskılar

array(5) {
  ["digit"]=>
  int(8)
  ["lower"]=>
  int(11)
  ["upper"]=>
  int(14)
  ["punct"]=>
  int(15)
  ["others"]=>
  int(0)
}