[Single topic] The results of the following programs are: <?php $STR = "Tomorrow Programming Dictionary"; Echo Substr_count ($str, "word");? >

Source: Internet
Author: User

  1. 1
  2. 3
  3. 2
  4. 4
Instance

Calculate the number of occurrences of "Shanghai" in a string:

<?phpecho substr_count ("I Love Shanghai". Shanghai is the "biggest city", "Shanghai");? >

Running an instance

The Substr_count () function calculates the number of occurrences of a substring in a string.

Note: Substrings are case-sensitive.

Note: This function does not count overlapping substrings (see Example 2).

Note: If the start parameter plus the length parameter is greater than the string size, the function generates a warning (see Example 3).

Grammar
Substr_count (string,substring,start,length)
Parameters Description
String Necessary. Specifies the string to be checked.
Substring Necessary. Specifies the string to search for.
Start Optional. Specifies where to start the search in the string.
Length Optional. Specifies the length of the search.
Technical details
return value: Returns the number of occurrences of a substring in a string.
PHP version: 4 +
Update log: In PHP 5.1, the start and length parameters have been added.
More examples of examples 1

Use all the parameters:

String reduction to "s I"?>

Running an instance

Example 2

Overlapping sub-strings:

This function does not count overlapping substrings?>

Running an instance

Example 3

If the start and length parameters exceed the string length, the function outputs a warning:

<?phpecho $str = "This was nice"; Substr_count ($str, ' is ', 3,9);? >

Because the length value exceeds the length of the string (3 + 9 is greater than 12), the use outputs a warning.

Correct answer:

[Single topic] The results of the following programs are: <?php $STR = "Tomorrow Programming Dictionary"; Echo Substr_count ($str, "word");? >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.