php function Count_chars () returns all information for a string

Source: Internet
Author: User

Instance

Returns a string that contains all the different characters used in "Hello world!" (Mode 3):

<?php$str = "Hello world!"; Echo Count_chars ($STR, 3);? >

Definition and usage

The Count_chars () function returns information about the characters used in the string (for example, the number of times an ASCII character appears in a string, or whether a character has been used in a string).

Grammar

Count_chars (String,mode)

Parameter description

string is required. Specifies the string to check.

mode is optional. Specifies the return mode. The default is 0. There are the following different return modes:

0-Array, ASCII value is the key name, the number of occurrences is the key value

1-Array, ASCII value is the key name, the number of occurrences is a key value, only the number of occurrences greater than 0 is listed

2-Array, ASCII value is the key name, the number of occurrences is a key value, only the number of occurrences equal to 0 is listed

3-string with all the different characters used

4-A string with all the different characters that have not been used

Technical details

Return value: Depends on the specified mode parameter.

PHP version: 4+

More examples

Example 1

Returns a string containing all characters not used in "Hello world!" (Mode 4):

<?php$str = "Hello world!"; Echo Count_chars ($STR, 4);? >

Example 2

In this example, we will use Count_chars () to check the string, with the return mode set to 1. Mode 1 Returns an array with the ASCII value as the key name and the number of occurrences of the key value:

<?php$str = "Hello world!"; Print_r (Count_chars ($STR, 1));? >

Example 3

Counts the number of times an ASCII character appears in a string another instance:

<?php$str = "PHP is pretty fun!!"; $strArray = Count_chars ($str, 1), foreach ($strArray as $key + $value) {echo "the character <b>". Chr ($key). " </b> was found $value time (s) <br> ";}?" >

Count_chars instances

<?php$data  =  "Both Ts and one F."; foreach (Count_chars ($data,  1) as  $i  = +  $val) {   Echo  "There were  $val  instance (s) of" "  ,  chr ($i),  " \ "in the String.<br/>";}? >

Operation Result:

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.