How to use PHP to determine the inclusion of strings

Source: Internet
Author: User
How to flexibly use PHP to determine the inclusion of strings. The following describes how to use PHP to determine the inclusion of a string: 1. strstr: returns a string from the start of the character to the end. If no return value is returned, the code is not included as follows:The following describes how to use PHP to determine the inclusion of a string:

1. strstr: returns a string from the start to the end of the character to be judged. If no return value exists, it does not contain

The code is as follows:

 
  1. <? Php
  2. /* Example in the manual */
  3. $ Email = 'User @ example.com ';
  4. $ Domain = strstr ($ email ,'@');
  5. Echo $ domain;
    // Prints @ example.com
  6. ?>

2. stristr: it is used exactly the same as strstr. The only difference is that stristr is case insensitive.

3. strpos: returns a boolean value. FALSE and TRUE. use "=" to judge. the execution speed of strpos is faster than that of the above two functions. In addition, strpos has a parameter that specifies the location for judgment, but is left blank by default. it indicates determining the entire string. the disadvantage is poor support for Chinese characters.

PHP judges that the string contains the following code:

 
  1. $str= 'abc';
  2. $needle= 'a';
  3. $pos = strpos($str, $needle);

4. use explode for determination

PHP judges that the string contains the following code:

 
  1. Function checkstr ($ str ){
  2. $ Needle = "a"; // determines whether the string contains.
  3. $ Tmparray = explode ($ needle, $ str );
  4. If (count ($ tmparray)> 1 ){
  5. Return true;
  6. } Else {
  7. Return false;
  8. }
  9. }

The above is a sample code used by PHP to determine the inclusion of strings.


Substring 1. strstr: returns a string from the start to the end of the character to be judged. If no return value is returned, the code is not included as follows :...

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.