• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / PHP / Get Word Count of String in php with str_word_count() Function

Get Word Count of String in php with str_word_count() Function

April 7, 2022 Leave a Comment

In php, we can get the word count and number of words in a string with the str_word_count() function.

$string = "This is a string";

echo str_word_count($string);

// Output:
4

When working with string variables in our code, the ability to easily get information about the variables is valuable.

One such piece of information is the number of words in a string variable.

We can use the php str_word_count() function to get the number of words in a string in our php code.

Just pass the string variable to str_word_count() and you’ll get the word count.

Below is a simple example showing you how to get the word count of a string in php.

$string = "This is a string";

echo str_word_count($string);

// Output:
4

Using str_word_count() to Get an Array of All Words in a String

The php str_word_count() function, by default, returns the number of words in a string.

You can change the default behavior by passing a value to the optional ‘return’ parameter.

If you pass ‘1’ or ‘2’ to ‘return’, then you will get an array which contains the words of the string variable. Passing ‘1’ returns just the words. If you pass ‘2’, you get an array where the keys are the position of the word and the value is the word.

Below are some examples showing different ways you can use str_word_count() in your php code.

$string = "This is a string";

echo str_word_count($string);
echo str_word_count($string, 1);
echo str_word_count($string, 2);

// Output:
4
Array
(
    [0] => This
    [1] => is
    [2] => a
    [3] => string
)
Array
(
    [0] => This
    [5] => is
    [8] => a
    [10] => string
)

Hopefully this article has been useful for you to learn how to get the word count of a string in php.

Other Articles You'll Also Like:

  • 1.  php array_key_exists() Function – Check if Key Exists in Array
  • 2.  php trim() Function – Remove Whitespace at Beginning and End of String
  • 3.  Remove Last Character from String Using php
  • 4.  php str_replace – Replace String in php
  • 5.  php atanh – Find Hyperbolic Arctangent of Number Using atanh() Function
  • 6.  How to Create an Empty Array in php
  • 7.  How to Reverse Array in php Without a Function or Other Variables
  • 8.  php range() – Create Array of Elements Within Given Range
  • 9.  php e – Using M_E and exp() Function to Get Euler’s Constant e
  • 10.  php array_slice() – Get Slice of Elements from Array

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy