Portrety Uliczne Nieznajomych - zobacz wyjątkową galerię portretów z warszawskich ulic
ZALOGUJ SIĘ
login:
hasło:
przypomnij hasło
załóż konto użytkownika
(i zobacz kilka porad gratis)
   
WYSZUKIWARKA I DZIAŁY
całe porady  tytuły
zaznacz działy do przeszukania
(brak wyboru = wszystkie działy)
PHP
MySQL >
PostgreSQL
SQLite
Perl
Java
XML
XSLT
XPath
WML
SVG
RegExp
Wyszukiwarki
Ochrona
VBScript
Google Plus
XHTML/CSS
JavaScript
Grafika
Flash
Photoshop
Windows
Linux
Bash
Apache
Procmail
E-biznes
Explorer
Opera
Firefox
Inne porady
   
KURSY, DOKUMENTACJE
Własne:
XHTML/CSS
JavaScript
ActionScript
WML, RSS, SSI
Pozostałe:
PHP
MySQL
Java API
więcej...
   
użytkowników online: 62
W CZYM MOGĘ POMÓC?


   
OPINIE UŻYTKOWNIKÓW
W takich dniach, jak ten, nie żałuję, że wykupiłem abonament. Korzystam z porad na tych stronach nawet kilkanaście razy w tygodniu i dzięki nim prace nad stronami dla klientów idą mi o wiele szybciej, a strony wyglądają bardziej profesjonalnie. Nie wiem, jak mogłem wcześniej pracować bez dostępu do porad w tym serwisie!

Wojciech Miszkiewicz

   
GALERIA FOTOGRAFII
   
PODRĘCZNIK PHP 5.x, 4.x, 3.x - częściowo spolszczony / źródło: www.php.net

[Spis] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [X] [W] [Z]

count_chars

(PHP 4, PHP 5)

count_chars --  Return information about characters used in a string

Description

mixed count_chars ( string string [, int mode] )

Counts the number of occurrences of every byte-value (0..255) in string and returns it in various ways. The optional parameter mode defaults to 0. Depending on mode count_chars() returns one of the following:

  • 0 - an array with the byte-value as key and the frequency of every byte as value.

  • 1 - same as 0 but only byte-values with a frequency greater than zero are listed.

  • 2 - same as 0 but only byte-values with a frequency equal to zero are listed.

  • 3 - a string containing all used byte-values is returned.

  • 4 - a string containing all not used byte-values is returned.

Przykład 1. count_chars() example

<?php

$data
= "Two Ts and one F.";

$result = count_chars($data, 0);

for (
$i=0; $i < count($result); $i++) {
   if (
$result[$i] != 0)
       echo
"There were $result[$i] instance(s) of \"" , chr($i) , "\" in the string.\n";
}

?>

This will output :

There were 4 instance(s) of " " in the string. 
There were 1 instance(s) of "." in the string. 
There were 1 instance(s) of "F" in the string. 
There were 2 instance(s) of "T" in the string. 
There were 1 instance(s) of "a" in the string. 
There were 1 instance(s) of "d" in the string. 
There were 1 instance(s) of "e" in the string. 
There were 2 instance(s) of "n" in the string. 
There were 2 instance(s) of "o" in the string. 
There were 1 instance(s) of "s" in the string. 
There were 1 instance(s) of "w" in the string.

See also strpos() and substr_count().




User Contributed Notes

Eric Pecoraro
27-May-2005 04:31

<?php

// Require (n) unique characters in a string
// Modification of a function below which ads some flexibility in how many unique characters are required in a given string.

$pass = '123456' ; // true
$pass = '111222' ; // false

req_unique($pass,3);

function
req_unique($string,$unique=3) {
   if (
count(count_chars($string,1)) < $unique) {
       echo
'false';
   }else{
       echo
'true';
   }
}

?>


Alex Gemmell
14-Feb-2005 07:03

Use: Great for checking for unique characters in a password.

I wanted to check that my website users, when registering a user account, chose a "valid" (not-so-easily-guessed) password.

One of the many checks was that the password must have 6 unique characters.  I was tying myself in knots with all kinds of recursive array calling when I was offered this wonderful one line solution:

function check_password($password) {

   #Have 6 unique characters
   if (count(count_chars($password, 1)) < 6) return false;

   #
   # other checks returning false as necessary
   #
   #

   return true;
}

I cannot claim this wonderful line for myself - a Mr Lynch suggested this to me.  Thanks!


seb at synchrocide dot net
17-May-2004 12:08

After much trial and error trying to create a function that finds the number of unique characters in a string I same across count_chars() - my 20+ lines of useless code were wiped for this:

<?
function unichar($string) {
$two= strtolower(str_replace(' ', '', $string));
$res = count(count_chars($two, 1));
return
$res;
}

/* examples :: */

echo unichar("bob"); // 2
echo unichar("Invisibility"); //8
echo unichar("The quick brown fox slyly jumped over the lazy dog"); //26

?>

I have no idea where this could be used, but it's quite fun


mlong at mlong dot org
30-Jan-2002 01:27

// Usefulness of the two functions

<?php
 $string
="aaabbc";

 
// You just want to count the letter a
 
$acount=substr_count($string,"a");

 
// You want to count both letter a and letter b
 
$counts=count_chars($string,0);
 
$acount=$counts[ord("a")];
 
$bcount=$counts[ord("b")];
?>


maotin at hongkong dot com
01-Feb-2001 02:04

Here are some more experiments on this relatively new and extremely handy function.

$string = 'I have never seen ANYTHING like that before! My number is "4670-9394".';

foreach(count_chars($string, 1) as $chr => $hit)
echo 'The character '.chr(34).chr($chr).chr(34).' has appeared in this string '.$hit.' times.<BR>';

#The result looks like
#The character " " has appeared in this string 11 times.

echo count_chars($string,3);
#The output is '!"-.034679AGHIMNTYabefhiklmnorstuvy'

echo strlen($string).' is not the same as '.strlen(count_chars($string, 3));

#This shows that '70 is not the same as 36'

As we can see above:

1)If you cares only about what is in the string, use count_chars($string, 1) and it will return an (associative?) array of what shows up only.

2) Either I misunderstood what the manul actually said, or it does not work the way it described: count_chars($strting, 3) actually returned a string of what characters are in the string, not a string of their byte-values (which is great because a string of numbers would be much harder to handle);

3)This is a short version of password checking: get the original string's length, then compare with the length of the string returned by count_chars($string,3). 

$length_of_string = strlen($string);
$num_of_chars = strlen(count_chars($string, 3));

$diff = ($length_of_string - $num_of_chars);

if ($diff)
echo 'At least one character has been used more than once.';
else
echo 'All character have been used only once.;

Note that since $num_of_chars gives no information about the actual number of occurance, we cannot go any further by the same rationale and say when $diff =2 then 2 characters showed up twice; it might be 1 character showd up 3 times, we have no way to tell (a good tolerance level setter, though).  You have to get the array and check the values if you want to have more control.

4) Final trick: now we have a primitive way to count the number of words in a string! (or do we have a fuction for that already?)


 

 
  © 1996-2012 & Reporter.plmiejscao serwisieabonamentwarunki korzystaniaRSSkontakt