August 2, 2016
Count a Array of the Specific words from a string in PHP

Hi, friends in this tutorial I show you how to count an array of specific words from a string in PHP. PHP gives functions in that we count how many characters or words are included in a string or how many times a particular substring occurs in a string. We going use the following string to demonstrate PHP’s substr_count functions.
index.php
1 2 3 4 5 6 7 8 9 10 |
<?php //How to count a Array of Specific words from string in PHP $array_count = array("a", "here", "it", "humour", "like", "industry"); $text = "<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>"; echo $text . '<br/>'; foreach($array_count as $count_word) { echo '<h3>' . $count_word . ' occurance are ' . substr_count(strtolower($text), $count_word). " times. </h3>"; } ?> |
Download
Mraj
Creative Designer & Developer specialist by the spirit and a loving blogger by thoughts. If you have any questions let me drop an email with the article name to the following email id: [email protected]