Home > Backend Development > PHP Tutorial > php finds substring in specified string

php finds substring in specified string

WBOY
Release: 2016-07-25 08:43:29
Original
1091 people have browsed it

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.

  1. $haystack1 = "2349534134345w3mentor16504381640386488129";
  2. $haystack2 = "w3mentor2349534134345165043816403864881 29";
  3. $haystack3 = "center234953413434516504381640386488129fyi";
  4. $pos1 = strpos($haystack1, "w3mentor");
  5. $pos2 = strpos($haystack2, "w3mentor");
  6. $pos3 = strpos($haystack3, "w3mentor");
  7. print("pos1 = ($pos1); type is " . gettype($pos1) . "n") ;
  8. print("pos2 = ($pos2); type is " . gettype($pos2) . "n");
  9. print("pos3 = ($pos3); type is " . gettype($pos3) . "n ");
  10. ?>
Copy code

Output result
  1. pos1 = (13); type is integer
  2. pos2 = (0); type is integer
  3. pos3 = (); type is boolean
  4. pos3 returns a bool value, that is, no substring was found
Copy code

php


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template