PHP str_word_count() Function
Complete PHP String Reference
Definition and Usage
The str_word_count() function counts the number of words in a string.
Syntax
|
str_word_count(string,return,char)
|
| 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. Note: This parameter was added in PHP 5.1 |
Example 1
<?php
echo str_word_count("Hello world!");
?>
|
The output of the code above will be:
Example 2
<?php
print_r(str_word_count("Hello world!",1));
?>
|
The output of the code above will be:
Array
(
[0] => Hello
[1] => world
)
|
Example 3
<?php
print_r(str_word_count("Hello world!",2));
?>
|
The output of the code above will be:
Array
(
[0] => Hello
[6] => world
)
|
Example 4
str_word_count() without and with the char parameter:
<?php
print_r(str_word_count("Hello world & good morning!",1));
print_r(str_word_count("Hello world & good morning!",1,"&"));
?>
|
The output of the code above will be:
Array
(
[0] => Hello
[1] => world
[2] => good
[3] => morning
)
Array
(
[0] => Hello
[1] => world
[2] => &
[3] => good
[4] => morning
)
|
Complete PHP String Reference
Click here to design a Stunning Flash Website for Free
Wix is a revolutionary web design tool that provides anyone with the possibility to create professional and beautiful websites for free.
With e-commerce features, search engine visibility and many more professional tools, Wix is the ultimate solution for creating a spectacular site while saving tons of money.
 |
W3Schools' Online Certification Program
The perfect solution for professionals who need to balance work, family, and career building.
More than 4000 certificates already issued!
|
The HTML Certificate documents your knowledge of HTML, XHTML, and CSS.
The JavaScript Certificate documents your knowledge of JavaScript and HTML DOM.
The XML Certificate documents your knowledge of XML, XML DOM and XSLT.
The ASP Certificate documents your knowledge of ASP, SQL, and ADO.
The PHP Certificate documents your knowledge of PHP and SQL (MySQL).
|