PHP determines if a string is included

Source: Internet
Author: User

1. Usage of STRSTR and STRISTR
123456 strstr: 返回一个从被判断字符开始到结束的字符串,如果没有返回值,则不包含. stristr: 它和strstr的使用方法完全一样.唯一的区别是stristr不区分大小写.$email = ‘ [email protected]’;$domain = strstr($email, ‘@’);echo$domain;// prints @example.com
2. Usage of Strpos
123456 strpos: 返回boolean值.FALSE和TRUE不用多说.用 “===”进行判断.strpos在执行速度上都比以上两个函数快,另外strpos有一个参数指定判断的位置,但是默认为空.意思是判断整个字符串.缺点是对中文的支持不好.PHP判断字符串的包含代码如下:    $str= ‘abc’;    $needle= ‘a’;    $pos = strpos($str, $needle);
3. Usage of explode
12345678910 explode进行判断PHP判断字符串的包含代码如下:    function checkstr($str){    $needle = “a”;//判断是否包含a这个字符    $tmparray = explode($needle,$str);    if(count($tmparray)>1){    return true;    } else{    return false;    }    }
4.in_array-Check if there is a value example in the array
123456789101112131415161718192021222324252627282930313233343536373839 1. in_array() 例子<?php$os= array("Mac", "NT", "Irix", "Linux");if (in_array("Irix", $os)) {echo"Got Irix";}if (  in_array("mac", $os)) {echo"Got mac";}?>第二个条件失败,因为 in_array() 是区分大小写的,所以以上程序显示为:Got Irix2. in_array() 严格类型检查例子<?php$a= array(‘1.10‘, 12.4, 1.13);if (in_array(‘12.4‘, $a, true)) {echo"‘12.4‘ found with strict check\n";}if (in_array(1.13, $a, true)) {echo"1.13 found with strict check\n";}?>上例将输出:1.13 found with strict check3. in_array() 中用数组作为 needle<?php$a= array(array(‘p‘, ‘h‘), array(‘p‘, ‘r‘), ‘o‘);if (in_array(array(‘p‘, ‘h‘), $a)) {echo"‘ph‘ was found\n";}if (in_array(array(‘f‘, ‘i‘), $a)) {echo"‘fi‘ was found\n";}if (in_array(‘o‘, $a)) {echo"‘o‘ was found\n";}?>上例将输出:‘ph‘ was found‘o‘ was foundarray_search — 在数组中搜索给定的值,如果成功则返回相应的键名

PHP determines if a string is included

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.