How PHP determines if a string contains another string

Source: Internet
Author: User

From 1:http://blog.sina.com.cn/s/blog_8edc37a801016yha.html

--------------------------------------------------------------------

I think the simplest is:(with this best, strpos efficiency is highest)

Strpos ($a, $b)!== False if $b is present in $ A, false otherwise.

The reason for using!== false (or = = = False) is that if the $b is exactly at the beginning of $ A, then the function returns an int (0), then 0 is false, but $b is indeed in $ A, so be sure to use!== to determine the type, to ensure that it is strictly false.

Other functions that are native to PHP, such as Strstr (), Stristr (), and so on, can be directly judged.

From 2:http://www.cnblogs.com/martin1009/archive/2012/12/26/2833831.html

------------------------------------------------------------------------------------------------

The PHP language is a powerful embedded HTML scripting language, and its ease of use allows many programmers to choose. PHP determines the inclusion of a string, you can use PHP's built-in function strstr,strpos,stristr directly to judge. You can also write a judgment function by the function of explode.

1. Strstr: Returns a string from the beginning of the judged character to the end, if there is no return value, does not contain

The code is as follows:

< PHP

/* For example in manual */

$Email = ' [email protected] ';

$domain = strstr ($email, ' @ ');

Echo $domain;
Prints @example. com

?>

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

3. Strpos: 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.

The PHP judgment string contains the following code:

$str= ' abc ';

$needle= ' a ';

$pos = strpos ($str, $needle);

4. Use Explode to judge

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;

}

}

How PHP determines if a string contains another string

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.