search
HomeDaily ProgrammingPHP KnowledgeHow to count the number of occurrences of a specified string in PHP

PHP counts the number of times a specified string appears. We can do this directly through the substr_count function in PHP. The substr_count function means counting the number of occurrences of a string.

How to count the number of occurrences of a specified string in PHP

Let’s use a simple code example to introduce PHP to count the number of occurrences of a specified string (including letters and Chinese characters):

1. The code example for counting English words is as follows:

<?php
$quote = "PHP中文网,是一个学习PHP的好地方!<br>我就是在PHP中文网学习的PHP";
echo $quote;
echo "<br>";
echo "<br>";
echo "以上段落中出现了".substr_count($quote,&#39;PHP&#39;)  ."次PHP!";

In the above code, the result of counting "PHP" is as follows:

How to count the number of occurrences of a specified string in PHP

2. The code example for counting the number of Chinese words is as follows:

<?php
$quote = "PHP中文网,是一个学习PHP的好地方!<br>我就是在PHP中文网学习的PHP";
echo $quote;
echo "<br>";
echo "<br>";
echo "以上段落中出现了".substr_count($quote,&#39;中文网&#39;)  ."次“PHP中文网”!";

In the above code, the result of counting "PHP Chinese Network" is as follows:

How to count the number of occurrences of a specified string in PHP

substr_count Syntax:

int substr_count ( string $haystack , string $needle [, int $offset = 0 [, int $length ]] )

substr_count() Returns the number of times the substring needle appears in the string haystack. Note that needle is case-sensitive.

The parameters:

haystack means searching in this string.

needle represents the string to be searched.

offset indicates the offset position where counting starts. If it is a negative number, counting starts from the end of the character.

length indicates the maximum search length after the specified offset position. If the sum of the offset plus this length is greater than the total length of the haystack, a warning message is printed. The length of a negative number is counted from the end of the haystack.

Note: This function does not calculate overlapping strings.

This article is a detailed introduction to PHP counting the number of occurrences of a specified string. It is very simple and easy to understand. I hope it will be helpful to friends in need!

The above is the detailed content of How to count the number of occurrences of a specified string in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function