Yogesh Chauhan's Blog

Learn to Implement Estimated Reading Time using PHP Part 1: The Basics

in PHP on November 16, 2019

If you already know the basic functions, skip to part 2.

Learn to Implement Estimated Reading Time using PHP Part 2: Final Implementation with Source Code

That can be easily done in PHP. For that we need to use str_word_count() Function. Let’s look at the basics of the function. 

Syntax:


str_word_count(string,return value,characters)
  • In the syntax above, string is required and it’s the whole blog article you want to be considered as reading material. 
  • return value is optional and it returns possible values for different return values from 0 to 2. 
  • characters is optional as well and it asks for the special characters you want to count as words.

For example the following PHP statement will return 2 as there are 2 words in the string.


echo str_word_count("Hello world!");

Now, let’s focus on our main example. Let’s say I have a article and it is stored in a variable $article from your database.

In that saved $article variable you might have some header tags like <h1> or paragraph tag like <p> and we don’t want to count those tags in time to read as user won’t be able to see them at all. So we need to remove those tags. For that we will use strip_tags() function.

Let’s take a look at the syntax.


strip_tags(string,allowed tags)

As you can see, it takes 2 arguments and one of them is the actual string and the other one asked if you want to keep some tags by allowing them, which is optional.

Now, the following example will return us Hello world without any tags even if the string has tags in it.


echo strip_tags("Hello <h1>world!</h1>");

The strip_tags removes HTML, XML and PHP tags. Also, HTML comments are always removed using this. You can’t use allow to keep the comments.

Learn more in PART 2 of this article. 

Learn to Implement Estimated Reading Time using PHP Part 2: Final Implementation with Source Code


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
3 ways to pass a variable in url() function in SCSS (Sass)SCSSadd_filter function in WordPressWordPressHow to scroll contents of HTML body horizontally and vertically using JavaScript?JavaScriptHow to Block IPs and User Agents using code in Drupal or WordPress?Drupal10 Usability Blunders to AvoidUI/UXHow to apply style to the first-child using jQuery?jQuery