Counts how many times a substring occurs in a string in PHP

  275 views   5 months ago PHP

Use the PHP substr_count() function

You can use the PHP substr_count() function to find out how many times a given substring is appears or repeated inside a string. Let's try out an example to see how it works:

<?php
$quote = "Try not to become a man of success, but rather try to become a man of value.";
 
echo substr_count($quote, 'man'); // Outputs: 2
?>

 

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on harsukh21@gmail.com

Related Articles