Home>Article>Backend Development> How to query the number of occurrences of a string in php

How to query the number of occurrences of a string in php

青灯夜游
青灯夜游 Original
2022-08-02 20:55:39 5858browse

Two methods to query the number of occurrences of a string: 1. Use the substr_count() function to calculate the number of times a specified substring appears in a string in a case-sensitive manner. The syntax "substr_count(string, search sub String, start search position, search length)". 2. Use the mb_substr_count() function to count the number of occurrences of a string. The syntax is "mb_substr_count (string, search substring, character encoding)".

How to query the number of occurrences of a string in php

The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer

php query string There are two functions for the number of occurrences

  • substr_count() function

  • mb_substr_count() function

Method 1: Use the substr_count() function to count the number of times

The substr_count() function counts the number of times a substring appears in a string (case-sensitive).

Syntax:

substr_count(string,substring,start,length)
  • string Required. Specifies the string to be checked.

  • #substring Required. Specifies the string to search for.

  • #start Optional. Specifies where in the string to begin the search.

  • #length Optional. Specifies the length of the search.

Note: If the start parameter plus the length parameter is greater than the string length, this function generates a warning.

Example 1:

"; $count=substr_count($str,"Shanghai"); echo "Shanghai 出现了:".$count."次"; ?>

Output result:

How to query the number of occurrences of a string in php

##Example 2:

"; $count=substr_count($str,"上海"); echo "上海 出现了:".$count."次"; ?>

How to query the number of occurrences of a string in php

Method 2: Use the mb_substr_count() function to count the number of times

The mb_substr_count() function counts the number of times a string appears.

Syntax:


mb_substr_count(string,substring,encoding)

  • string Required. Specifies the string to be checked.

  • #substring Required. Specifies the string to search for.

  • encoding is optional. Specifies the character encoding. If omitted or null, the internal character encoding is used.

  • "; $count=mb_substr_count($str,"中国"); echo "中国 出现了:".$count."次"; ?>
Output result:

How to query the number of occurrences of a string in php

"; $count1=mb_substr_count($str,"Shanghai"); echo "Shanghai 出现了:".$count1."次
"; $count2=mb_substr_count($str,"shanghai"); echo "shanghai 出现了:".$count2."次"; ?>

How to query the number of occurrences of a string in php

Recommended learning: "

PHP Video Tutorial

The above is the detailed content of How to query the number of occurrences of a 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