Ways to find string occurrences in PHP _php tips

Source: Internet
Author: User

This example describes how the PHP lookup string appears. Share to everyone for your reference. The specific methods are as follows:

Looking up the number of occurrences of a string in PHP can be done by the Substr_count () function, and here's a detailed description of these functions.

Substr_count ($haystack, $needle [, $offset [, $length]])

Where parameters:
$haystack represents a parent string, $needl represents the character to find

$offset represents the starting point for the lookup, $length represents the length of the lookup, all optional parameters

The instance code is as follows:

Copy Code code as follows:
$str = "This is a test";
Echo Substr_count ($str, ' is '). ' <br> ';
Echo Substr_count ($str, ' is ', 3). ' <br> ';
Echo Substr_count ($str, ' is ', 3, 3). ' <br> ';

The instance code is as follows:
Copy Code code as follows:
$str = ' HTTP://WWW.JB51.NET Cloud Habitat Community _ Script Programming Introductory Tutorial _ Script Design ac _ character occurrences ';
Echo Substr_count ($str, ' W '). ' <br/> ';
Echo Substr_count ($str, ' t '). ' <br/> ';
Echo Substr_count ($str, ' cloud-dwelling community ');

The output results are:
And then share some string lookup functions:

strstr-the first occurrence of a lookup string
Stristr strstr case-insensitive version
Strpos-Find the position where the string first appears
String substr (string $string, int $start [, int $length])
String STRRCHR (String $haystack, mixed $needle)
Strripos-Calculates where the specified string last appears in the target string (case-insensitive)
Stripos-Find the position of the first occurrence of the string (case-insensitive)
Strrpos-Calculates where the specified string last appears in the target string

I hope this article will help you with your PHP program design.

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.