Home arrow PHP arrow Page 3 - Strings: Trimming and Counting

Counting the Total Number of Words in a String - PHP

In this conclusion to a five-part series on strings and regular expressions in PHP, you'll learn about padding and stripping a string, trimming characters from a string, counting the characters in a string, and more. This article is excerpted from chapter nine of the book Beginning PHP and Oracle: From Novice to Professional, written by W. Jason Gilmore and Bob Bryla (Apress; ISBN: 1590597702).

TABLE OF CONTENTS:
  1. Strings: Trimming and Counting
  2. Counting Characters and Words
  3. Counting the Total Number of Words in a String
  4. Taking Advantage of PEAR: Validate_US
By: Apress Publishing
Rating: starstarstarstarstar / 1
July 15, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

The functionstr_word_count()offers information regarding the total number of words found in a string. Its prototype follows:

mixed str_word_count(string str [, int format])

If the optional parameterformatis not defined, it will simply return the total number of words. Ifformatis defined, it modifies the functionís behavior based on its value:

1: Returns an array consisting of all words located instr.

2: Returns an associative array, where the key is the numerical position of the word instr, and the value is the word itself.

Consider an example:

<?php
    $summary = <<< summary
    In the latest installment of the ongoing Developer.com PHP series,
    I discuss the many improvements and additions to PHP 5's
    object-oriented architecture.
summary;
   $words = str_word_count($summary);
   printf("Total words in summary: %s", $words);
?>

This returns the following:

--------------------------------------------
Total words in summary: 23
--------------------------------------------

You can use this function in conjunction witharray_count_values()to determine the frequency in which each word appears within the string:

<?php
$summary = <<< summary
In the latest installment of the ongoing Developer.com PHP series,
I discuss the many improvements and additions to PHP 5's
object-oriented architecture.
summary;
  
$words = str_word_count($summary,2);
   $frequency = array_count_values($words);
   print_r($frequency);
?>

This returns the following:

--------------------------------------------
Array ( [In] => 1 [the] => 3 [latest] => 1 [installment] => 1 [of] => 1
[ongoing] => 1 [Developer] => 1 [com] => 1 [PHP] => 2 [series] => 1
[I] => 1 [discuss] => 1 [many] => 1 [improvements] => 1 [and] => 1
[additions] => 1 [to] => 1 [s] => 1 [object-oriented] => 1
[architecture] => 1 )
--------------------------------------------



 
 
>>> More PHP Articles          >>> More By Apress Publishing
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: