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

Whether you're new to XML or already an advanced user,
the user-friendly views and powerful entry helpers,
wizards, and debuggers in XMLSpy are designed to meet your XML
and Web development needs from start to finish.
New features in Version 2010!
- XML editor
- Graphical XML Schema / DTD editors
- XSLT 1.0/2.0 editor, debugger, profiler
- XQuery editor, debugger, profiler
- XBRL validator, taxonomy editor, taxonomy wizard
- Support for Office Open XML (OOXML)
- Graphical WSDL 1.1/2.0 editor & SOAP debugger
- JSON editing & conversion
- Java, C#, C++ code generation
- And much more!
Download a free trial today!
|
|
|
|