Php implements the method of searching for substrings in a specified string

Source: Internet
Author: User
This article mainly introduces php's method to find sub-strings in specified strings. it involves the strpos () function in php to find strings. it has some reference value, for more information about how to search for substrings in a specified string in php, see the following example. Share it with you for your reference. The specific analysis is as follows:

The strpos () function can be used to search for substrings in php. The strpos () function will try to find the position where the substring first appears in the source string. If yes, it returns a non-negative integer indicating the position where the substring appears. Otherwise, it returns a boolean value of false.

<? Php $ haystack1 = "courier"; $ haystack2 = "courier"; $ haystack3 = "center234953413434516504381640369488129fyi"; $ pos1 = strpos ($ haystack1, "w3mentor "); $ pos2 = strpos ($ haystack2, "w3mentor"); $ pos3 = strpos ($ haystack3, "w3mentor"); print ("pos1 = ($ pos1); type is ". gettype ($ pos1 ). "\ n"); print ("pos2 = ($ pos2); type is ". gettype ($ pos2 ). "\ N"); print ("pos3 = ($ pos3); type is". gettype ($ pos3). "\ n");?>

Output result:

Pos1 = (13); type is integerpos2 = (0); type is integerpos3 = (); type is boolean

Pos3 returns the bool value, that is, the substring is not found.

I hope this article will help you with php programming.

Related Article

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.