Multiple ways to include a specified string in a PHP string

Source: Internet
Author: User
Tags explode

The content of this article is about whether the PHP string contains the specified string of various methods of the problem, now share to everyone, the need for friends can refer to

When writing a program, often to deal with strings, the most basic is the search string, whether the PHP detection string contains the specified string can use the regular, if you do not understand the regular, then there are several functions can provide you with convenience.

1. Strstr

The Strstr () function searches for the first occurrence of a string in another string.
The function returns the remainder of the string (from the matching point). returns false if the searched string is not found.

The code is as follows:

<?php/* For example in manual */$email = ' user@example.com '; $domain = Strstr ($email, ' @ '); Echo $domain; Prints @example .com?>

2. Stristr

The Stristr () function finds the position of the first occurrence of a string in another string.
If successful, returns the remainder of the string (from the matching point). If the string is not found, false is returned.

It is exactly the same as strstr. The only difference is that the STRISTR is not case sensitive.

3. Strpos

The Strpos function returns a Boolean value. False and true do not say much. use "= = =" to judge. Strpos is faster than the above two functions in execution speed, and Strpos has a parameter to specify the location of the judgment, but the default is empty. Meaning to judge the entire string. The disadvantage is that the support for Chinese is not good.

Example 1

if (Strpos (' www.jb51.net ', ' jb51 ')!== false) {  echo ' contains jb51 ';} else{Echo ' does not contain jb51 ';}

Example 2

$STR = ' abc '; $needle = ' a '; $pos = Strpos ($str, $needle); Returns the position where the string was first found, returned as 1, false if not detected

4. Explode

Using explode to determine the PHP judgment string contains the following code:

function Checkstr ($str) {$needle = ' a ';//Determine if the character "A" is included $tmparray = Explode ($needle, $str); if (count ($tmparray) >1) {  return true;} else{  return false;}}

5, substr For example we need to judge whether the last character is a character set

<?php/* $str 1= "<p> This is a winrar dedicated DLL and then under OH nice DLL file,qlogwin32.dll</p>"; if (substr ($str 1,-8) = = ".dll< /p> ") {echo substr ($str 1,0,-4);}

6, Substr_count statistics "substring" in "The number of occurrences in the original string"

The Substr_count () function is the number of times a small string appears in a large string:
$number = Substr_count (big_string, small_string);
Exactly today need a function to find a string, to implement the Big_string whether the string contains a string small_string, returns True or Fasle;

Checking for a half-day manual did not find a ready-made function, so thought can use the Substr_count function to implement the code as follows:

function Check_str ($STR, $substr) {$nums =substr_count ($str, $substr);  if ($nums >=1)  {   return true;  }  else  {   return false;}  }

Super simple!

Specifically, you can find the relevant functions for advanced applications.


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.