PHP

PHP Menu

PHP

str_word_count() Function - Definition, Syntax, Parameters, Examples

Definition

The str_word_count() function counts the number of words in a string.

Syntax

str_word_count(string, return, char)

Parameters

Parameter Description
string Required. Specifies the string to check.
return Optional. Specifies the return value of the str_word_count() function. Possible values:
0 - Default. Returns the number of words found
1 - Returns an array with the words from the string
2 - Returns an array where the key is the position of the word in the string, and value is the actual word
char Optional. Specifies special characters to be considered as words.

Example

<?php
echo str_word_count("Welcome to PHP!"); echo "<br>"; print_r(str_word_count("Welcome to PHP!", 2));

Introduction

PHP Basics

PHP Advance

PHP OOP

PHP Functions and Methods