Home  >  Article  >  Backend Development  >  How to find a substring in a specified string in php

How to find a substring in a specified string in php

墨辰丷
墨辰丷Original
2018-06-12 14:22:303114browse

This article mainly introduces the method of finding substrings in a specified string in PHP. It involves the technique of searching strings with the strpos() function in PHP. It has certain reference value. Friends who need it can refer to it

The example of this article describes the method of finding substrings in a specified string in PHP. The specific analysis is as follows:

The strpos() function can be used to find substrings in PHP. The strpos() function will try to find the first occurrence of a substring in the source string. If found, it returns a non-negative integer indicating the position of the substring. Otherwise it returns a boolean false.

<?php
$haystack1 = "2349534134345w3mentor16504381640386488129";
$haystack2 = "w3mentor234953413434516504381640386488129";
$haystack3 = "center234953413434516504381640386488129fyi";
$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 integer
pos2 = (0); type is integer
pos3 = (); type is boolean

pos3 returns a bool value, that is, the substring is not found

Summary: The above is the summary of this article All content, I hope it will be helpful to everyone's study.

Related recommendations:

php implements recording username and password based on cookies

Brief description of the method of intercepting Chinese characters in php to prevent garbled characters

php operates the color value and converts the color to its inverse color

The above is the detailed content of How to find a substring in 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