PHP str_word_count() Function Last Updated : 21 Jun, 2023 Comments Improve Suggest changes Like Article Like Report The str_word_count() function is a built-in function in PHP and is used to return information about words used in a string like total number word in the string, positions of the words in the string etc. Syntax: str_word_count ( $string , $returnVal, $chars ) Parameters Used: $string:This parameter specifies the string whose words the user intends to count.This is not an optional parameter.$returnVal:The return value of str_word_count() function is specified by the $returnVal parameter. It is an optional parameter and its default value is 0. The parameter can take below values as required:0 :It returns the number of words in the string $string.1 :It returns an array containing all of the words which are found in the string.2 :It returns an associative array with key-value pairs, where the key defines the position of the word in the string and the value is the word itself.$chars:This is an optional parameter which specifies a list of additional characters which shall be considered as a ‘word’. Return Type: The return type of function depends on the parameter $returnVal and return the values as described above. Below programs explain the working of str_word_count() function: Calculating the number of words in a string: To Display only the number of words in a string,the str_word_count() function should be executed in the following way: php <?php $mystring = "Twinkle twinkl4e little star"; print_r(str_word_count($mystring)); ?> Output: 5Find the words in a string: To return an array containing the words in a string,the str_word_count() function should be executed in the following way: php <?php $mystring = "Twinkle twinkl4e little star"; print_r(str_word_count($mystring, 1)); ?> Output: Array ( [0] => Twinkle [1] => twinkl [2] => e [3] => little [4] => star )Find words in a string along with numeric position of the words: To return an array containing the words in a string along with numeric position of the words,the str_word_count() function should be executed in the following way: php <?php $mystring = "Twinkle twinkl4e little star"; print_r(str_word_count($mystring, 2)); ?> Output: Array ( [0] => Twinkle [8] => twinkl [15] => e [17] => little [24] => star ) Find words in a string when some special character are considered as word: To return an array containing the words in a string where a character shall be considered as a word, the str_word_count() function should be executed in the following way: php <?php $mystring = "Twinkle twinkl4e little star"; print_r(str_word_count($mystring, 2 ,4)); ?> Output: Array ( [0] => Twinkle [8] => twinkl4e [17] => little [24] => star ) Comment More infoAdvertise with us Next Article PHP str_word_count() Function S Shubrodeep Banerjee Follow Improve Article Tags : Misc Competitive Programming Web Technologies PHP DSA PHP-string PHP-function +3 More Practice Tags : Misc Similar Reads PHP addcslashes() Function The addcslashes() function is a built-in function in PHP. The addcslashes() function is used to add backslashes before some specified characters in a given string. Syntax: string addcslashes($string, $characters) Parameters: This function accepts two parameters as shown in the above syntax and are d 2 min read PHP addslashes() Function The addslashes() function is an inbuilt function in PHP and it returns a string with backslashes in front of predefined characters. It does not take any specified characters in the parameter. The predefined characters are: single quote (')double quote (")backslash (\)NULL Note: The addslashes() func 2 min read PHP bin2hex() Function The bin2hex() function in PHP converts a string to hexadecimal values. The conversion is done byte-wise with the high-nibble first. Note: It is not for converting strings representing binary digits into hexadecimal. Syntax: bin2hex($string) Parameters: This function accepts a single parameter $strin 1 min read PHP chop() Function The chop() in PHP is used to remove white spaces or any other specified characters from the end of a string. Syntax: string chop($string, $character) Parameters: This function accepts two parameters as shown in the above syntax and are described below: $string : It is used to specify the string whic 2 min read PHP chr() Function The chr() function is a built-in function in PHP and is used to convert a ASCII value to a character. It accepts an ASCII value as a parameter and returns a string representing a character from the specified ASCII value. The ASCII value can be specified in decimal, octal, or hex values. Octal values 2 min read PHP chunk_split() Function The chunk_split() function is a built-in function in PHP. The chunk_split() function is used to split a string into smaller chunks of a specific length. Syntax: string chunk_split($string, $length, $end) Parameters: This function accepts three parameters as shown in the above syntax and are describe 2 min read PHP convert_uudecode() Function The convert_uudecode() is a built in function in PHP. This function decode a uuencoded string encoded using convert_uuencode() function. The uudecode() functions makes string into printable form. Syntax: string convert_uudecode(string) Parameters: The uuencoded string which will be decoded. Return T 1 min read PHP convert_uuencode() Function The convert_uuencode() is a built-in function in PHP. The convert_uuencode() function encodes a string using the uuencode algorithm. Uuencode encoding translates all strings (including binary data) into printable characters which makes them safe for network transmissions. Syntax: String convert_uuen 1 min read PHP count_chars() Function The count_chars() is an inbuilt function in PHP and is used to perform several operations related to string like the number of an ASCII character occurs in a string. Syntax : count_chars(string,return_mode); Parameters: The count_chars() function takes two parameters string and return_mode as explai 2 min read PHP crc32() Function The crc32() function helps us to calculate a 32-bit crc or cyclic redundancy checksum polynomial for a string. The function uses the CRC32 algorithm.This function can be used to validate data integrity. However, to ensure that we get the correct string representation from the crc32() function, we ne 2 min read Like